PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/test/llhttpdate_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 160 lines | 100 code | 19 blank | 41 comment | 7 complexity | c4434352abf3bdef73bae353468cd51a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llhttpdate_tut.cpp
  3. * @author Kartic Krishnamurthy
  4. * @date Wednesday, 18 Jul 2007 17:00:00 GMT :)
  5. *
  6. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include "linden_common.h"
  28. #include "lltut.h"
  29. #include "lldate.h"
  30. #include "llframetimer.h"
  31. #include <time.h>
  32. #include <locale.h>
  33. namespace tut
  34. {
  35. struct httpdate_data
  36. {
  37. LLDate some_date;
  38. };
  39. typedef test_group<httpdate_data> httpdate_test;
  40. typedef httpdate_test::object httpdate_object;
  41. tut::httpdate_test httpdate("httpdate");
  42. template<> template<>
  43. void httpdate_object::test<1>()
  44. {
  45. static std::string epoch_expected = "Thursday, 01 Jan 1970 00:00:00 GMT" ;
  46. ensure("Check Epoch in RFC 1123", ( epoch_expected == some_date.asRFC1123()));
  47. }
  48. template<> template<>
  49. void httpdate_object::test<2>()
  50. {
  51. static std::string expected = "Wednesday, 18 Jul 2007 22:17:24 GMT" ;
  52. some_date = LLDate(1184797044.037586);
  53. ensure("Check some timestamp in RFC 1123", ( expected == some_date.asRFC1123()));
  54. }
  55. // This test of course most generic.. runs off current time
  56. template<> template<>
  57. void httpdate_object::test<3>()
  58. {
  59. //F64 sometime = LLFrameTimer::getTotalSeconds();
  60. time_t sometime;
  61. time(&sometime);
  62. some_date = LLDate((F64) sometime);
  63. struct tm *result;
  64. char expected[255];
  65. std::string actual;
  66. result = gmtime(&sometime);
  67. /*
  68. std::cout << " seconds: "<< result->tm_sec
  69. << ", minutes: " << result->tm_min
  70. << ", hours: " << result->tm_hour
  71. << ", day of the month: " << result->tm_mday
  72. << ", month: " << result->tm_mon
  73. << ", year: " << result->tm_year
  74. << ", day of the week: " << result->tm_wday
  75. << ", day in the year: " << result->tm_yday
  76. << ", DST: " << result->tm_isdst << std::endl;
  77. */
  78. strftime(expected, 255, "%A, %d %b %Y %H:%M:%S GMT", result);
  79. actual = some_date.asRFC1123();
  80. // probably not a good idea to use strcmp but this is just a unit test
  81. ensure("Current time in RFC 1123", (strcmp(expected, actual.c_str()) == 0));
  82. }
  83. void test_date_string(const std::string &locale, struct tm *t,
  84. const std::string &fmt, const std::string &expected)
  85. {
  86. std::string result = LLDate::toHTTPDateString(t, fmt);
  87. LLStringUtil::toLower(result);
  88. std::string label = std::string("toHTTPDateString - ") + locale;
  89. ensure_equals(label.c_str(), result, expected);
  90. }
  91. template<> template<>
  92. void httpdate_object::test<4>()
  93. {
  94. // test localization of http dates
  95. #if LL_WINDOWS
  96. const char *en_locale = "english";
  97. const char *fr_locale = "french";
  98. #else
  99. const char *en_locale = "en_GB.UTF-8";
  100. const char *fr_locale = "fr_FR.UTF-8";
  101. #endif
  102. std::string prev_locale = LLStringUtil::getLocale();
  103. std::string prev_clocale = std::string(setlocale(LC_TIME, NULL));
  104. time_t test_time = 1252374030; // 8 Sep 2009 01:40:01
  105. struct tm *t = gmtime(&test_time);
  106. setlocale(LC_TIME, en_locale);
  107. if (strcmp(setlocale(LC_TIME, NULL), en_locale) != 0)
  108. {
  109. setlocale(LC_TIME, prev_clocale.c_str());
  110. skip("Cannot set English locale");
  111. }
  112. LLStringUtil::setLocale(en_locale);
  113. test_date_string(en_locale, t, "%d %B %Y - %H:%M", "08 september 2009 - 01:40");
  114. test_date_string(en_locale, t, "%H", "01");
  115. test_date_string(en_locale, t, "%M", "40");
  116. test_date_string(en_locale, t, "%I", "01");
  117. test_date_string(en_locale, t, "%d", "08");
  118. test_date_string(en_locale, t, "%Y", "2009");
  119. test_date_string(en_locale, t, "%p", "am");
  120. test_date_string(en_locale, t, "%A", "tuesday");
  121. test_date_string(en_locale, t, "%B", "september");
  122. setlocale(LC_TIME, fr_locale);
  123. if (strcmp(setlocale(LC_TIME, NULL), fr_locale) != 0)
  124. {
  125. LLStringUtil::setLocale(prev_locale);
  126. setlocale(LC_TIME, prev_clocale.c_str());
  127. skip("Cannot set French locale");
  128. }
  129. LLStringUtil::setLocale(fr_locale);
  130. test_date_string(fr_locale, t, "%d %B %Y - %H:%M", "08 septembre 2009 - 01:40");
  131. test_date_string(fr_locale, t, "%H", "01");
  132. test_date_string(fr_locale, t, "%M", "40");
  133. test_date_string(fr_locale, t, "%I", "01");
  134. test_date_string(fr_locale, t, "%d", "08");
  135. test_date_string(fr_locale, t, "%Y", "2009");
  136. test_date_string(fr_locale, t, "%A", "mardi");
  137. test_date_string(fr_locale, t, "%B", "septembre");
  138. LLStringUtil::setLocale(prev_locale);
  139. setlocale(LC_TIME, prev_clocale.c_str());
  140. }
  141. }