PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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
 28#include "linden_common.h"
 29#include "lltut.h"
 30
 31#include "lldate.h"
 32#include "llframetimer.h"
 33
 34#include <time.h>
 35#include <locale.h>
 36
 37namespace tut
 38{
 39    struct httpdate_data
 40    {
 41        LLDate some_date;
 42    };
 43    typedef test_group<httpdate_data> httpdate_test;
 44    typedef httpdate_test::object httpdate_object;
 45    tut::httpdate_test httpdate("httpdate");
 46
 47    template<> template<>
 48    void httpdate_object::test<1>()
 49    {
 50
 51        static std::string epoch_expected = "Thursday, 01 Jan 1970 00:00:00 GMT" ;
 52        ensure("Check Epoch in RFC 1123", ( epoch_expected == some_date.asRFC1123()));
 53    }
 54
 55    template<> template<>
 56    void httpdate_object::test<2>()
 57    {
 58
 59        static std::string expected = "Wednesday, 18 Jul 2007 22:17:24 GMT" ;
 60        some_date = LLDate(1184797044.037586);
 61        ensure("Check some timestamp in RFC 1123", ( expected == some_date.asRFC1123()));
 62    }
 63
 64    // This test of course most generic.. runs off current time
 65    template<> template<>
 66    void httpdate_object::test<3>()
 67    {
 68
 69        //F64 sometime = LLFrameTimer::getTotalSeconds();
 70        time_t sometime;
 71        time(&sometime);
 72        some_date = LLDate((F64) sometime);
 73        struct tm *result;
 74        char expected[255];
 75		std::string actual;
 76        
 77        result = gmtime(&sometime);
 78        /*
 79        std::cout << " seconds: "<< result->tm_sec 
 80                  << ", minutes: " << result->tm_min
 81                  << ", hours: " << result->tm_hour
 82                  << ", day of the month: " << result->tm_mday
 83                  << ", month: " << result->tm_mon
 84                  << ", year: " << result->tm_year
 85                  << ", day of the week: " << result->tm_wday
 86                  << ", day in the year: " << result->tm_yday
 87                  << ", DST: " << result->tm_isdst << std::endl;
 88        */
 89        strftime(expected, 255, "%A, %d %b %Y %H:%M:%S GMT", result);
 90        actual = some_date.asRFC1123();
 91        // probably not a good idea to use strcmp but this is just a unit test
 92        ensure("Current time in RFC 1123", (strcmp(expected, actual.c_str()) == 0));
 93    }
 94
 95	void test_date_string(const std::string &locale, struct tm *t,
 96						  const std::string &fmt, const std::string &expected)
 97	{
 98		std::string result = LLDate::toHTTPDateString(t, fmt);
 99		LLStringUtil::toLower(result);
100		std::string label = std::string("toHTTPDateString - ") + locale;
101		ensure_equals(label.c_str(), result, expected);
102	}
103
104	template<> template<>
105	void httpdate_object::test<4>()
106	{
107		// test localization of http dates
108#if LL_WINDOWS
109		const char *en_locale = "english";
110		const char *fr_locale = "french";
111#else
112		const char *en_locale = "en_GB.UTF-8";
113		const char *fr_locale = "fr_FR.UTF-8";
114#endif
115
116		std::string prev_locale = LLStringUtil::getLocale();
117		std::string prev_clocale = std::string(setlocale(LC_TIME, NULL));
118		time_t test_time = 1252374030;  // 8 Sep 2009 01:40:01
119		struct tm *t = gmtime(&test_time);
120
121		setlocale(LC_TIME, en_locale);
122		if (strcmp(setlocale(LC_TIME, NULL), en_locale) != 0)
123		{
124			setlocale(LC_TIME, prev_clocale.c_str());
125			skip("Cannot set English locale");
126		}
127
128		LLStringUtil::setLocale(en_locale);
129		test_date_string(en_locale, t, "%d %B %Y - %H:%M", "08 september 2009 - 01:40");
130		test_date_string(en_locale, t, "%H", "01");
131		test_date_string(en_locale, t, "%M", "40");
132		test_date_string(en_locale, t, "%I", "01");
133		test_date_string(en_locale, t, "%d", "08");
134		test_date_string(en_locale, t, "%Y", "2009");
135		test_date_string(en_locale, t, "%p", "am");
136		test_date_string(en_locale, t, "%A", "tuesday");
137		test_date_string(en_locale, t, "%B", "september");
138
139		setlocale(LC_TIME, fr_locale);
140		if (strcmp(setlocale(LC_TIME, NULL), fr_locale) != 0)
141		{
142			LLStringUtil::setLocale(prev_locale);
143			setlocale(LC_TIME, prev_clocale.c_str());
144			skip("Cannot set French locale");
145		}
146
147		LLStringUtil::setLocale(fr_locale);
148		test_date_string(fr_locale, t, "%d %B %Y - %H:%M", "08 septembre 2009 - 01:40");
149		test_date_string(fr_locale, t, "%H", "01");
150		test_date_string(fr_locale, t, "%M", "40");
151		test_date_string(fr_locale, t, "%I", "01");
152		test_date_string(fr_locale, t, "%d", "08");
153		test_date_string(fr_locale, t, "%Y", "2009");
154		test_date_string(fr_locale, t, "%A", "mardi");
155		test_date_string(fr_locale, t, "%B", "septembre");
156
157		LLStringUtil::setLocale(prev_locale);
158		setlocale(LC_TIME, prev_clocale.c_str());
159	}
160}