PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/lldate.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 338 lines | 237 code | 58 blank | 43 comment | 41 complexity | 5358c21cac189797b00a3c5e8cd71a18 MD5 | raw file
  1/** 
  2 * @file lldate.cpp
  3 * @author Phoenix
  4 * @date 2006-02-05
  5 * @brief Implementation of the date class
  6 *
  7 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#include "linden_common.h"
 30#include "lldate.h"
 31
 32#include "apr_time.h"
 33
 34#include <time.h>
 35#include <locale.h>
 36#include <string>
 37#include <iomanip>
 38#include <sstream>
 39
 40#include "lltimer.h"
 41#include "llstring.h"
 42
 43static const F64 DATE_EPOCH = 0.0;
 44
 45static const F64 LL_APR_USEC_PER_SEC = 1000000.0;
 46	// should be APR_USEC_PER_SEC, but that relies on INT64_C which
 47	// isn't defined in glib under our build set up for some reason
 48
 49
 50LLDate::LLDate() : mSecondsSinceEpoch(DATE_EPOCH)
 51{
 52}
 53
 54LLDate::LLDate(const LLDate& date) :
 55	mSecondsSinceEpoch(date.mSecondsSinceEpoch)
 56{
 57}
 58
 59LLDate::LLDate(F64 seconds_since_epoch) :
 60	mSecondsSinceEpoch(seconds_since_epoch)
 61{
 62}
 63
 64LLDate::LLDate(const std::string& iso8601_date)
 65{
 66	if(!fromString(iso8601_date))
 67	{
 68		llwarns << "date " << iso8601_date << " failed to parse; "
 69			<< "ZEROING IT OUT" << llendl;
 70		mSecondsSinceEpoch = DATE_EPOCH;
 71	}
 72}
 73
 74std::string LLDate::asString() const
 75{
 76	std::ostringstream stream;
 77	toStream(stream);
 78	return stream.str();
 79}
 80
 81//@ brief Converts time in seconds since EPOCH
 82//        to RFC 1123 compliant date format
 83//        E.g. 1184797044.037586 == Wednesday, 18 Jul 2007 22:17:24 GMT
 84//        in RFC 1123. HTTP dates are always in GMT and RFC 1123
 85//        is one of the standards used and the prefered format
 86std::string LLDate::asRFC1123() const
 87{
 88	return toHTTPDateString (std::string ("%A, %d %b %Y %H:%M:%S GMT"));
 89}
 90
 91LLFastTimer::DeclareTimer FT_DATE_FORMAT("Date Format");
 92
 93std::string LLDate::toHTTPDateString (std::string fmt) const
 94{
 95	LLFastTimer ft1(FT_DATE_FORMAT);
 96	
 97	time_t locSeconds = (time_t) mSecondsSinceEpoch;
 98	struct tm * gmt = gmtime (&locSeconds);
 99	return toHTTPDateString(gmt, fmt);
100}
101
102std::string LLDate::toHTTPDateString (tm * gmt, std::string fmt)
103{
104	LLFastTimer ft1(FT_DATE_FORMAT);
105
106	// avoid calling setlocale() unnecessarily - it's expensive.
107	static std::string prev_locale = "";
108	std::string this_locale = LLStringUtil::getLocale();
109	if (this_locale != prev_locale)
110	{
111		setlocale(LC_TIME, this_locale.c_str());
112		prev_locale = this_locale;
113	}
114
115	// use strftime() as it appears to be faster than std::time_put
116	char buffer[128];
117	strftime(buffer, 128, fmt.c_str(), gmt);
118	std::string res(buffer);
119#if LL_WINDOWS
120	// Convert from locale-dependant charset to UTF-8 (EXT-8524).
121	res = ll_convert_string_to_utf8_string(res);
122#endif
123	return res;
124}
125
126void LLDate::toStream(std::ostream& s) const
127{
128	apr_time_t time = (apr_time_t)(mSecondsSinceEpoch * LL_APR_USEC_PER_SEC);
129	
130	apr_time_exp_t exp_time;
131	if (apr_time_exp_gmt(&exp_time, time) != APR_SUCCESS)
132	{
133		s << "1970-01-01T00:00:00Z";
134		return;
135	}
136	
137	s << std::dec << std::setfill('0');
138#if( LL_WINDOWS || __GNUC__ > 2)
139	s << std::right;
140#else
141	s.setf(ios::right);
142#endif
143	s		 << std::setw(4) << (exp_time.tm_year + 1900)
144	  << '-' << std::setw(2) << (exp_time.tm_mon + 1)
145	  << '-' << std::setw(2) << (exp_time.tm_mday)
146	  << 'T' << std::setw(2) << (exp_time.tm_hour)
147	  << ':' << std::setw(2) << (exp_time.tm_min)
148	  << ':' << std::setw(2) << (exp_time.tm_sec);
149	if (exp_time.tm_usec > 0)
150	{
151		s << '.' << std::setw(2)
152		  << (int)(exp_time.tm_usec / (LL_APR_USEC_PER_SEC / 100));
153	}
154	s << 'Z'
155	  << std::setfill(' ');
156}
157
158bool LLDate::split(S32 *year, S32 *month, S32 *day, S32 *hour, S32 *min, S32 *sec) const
159{
160	apr_time_t time = (apr_time_t)(mSecondsSinceEpoch * LL_APR_USEC_PER_SEC);
161	
162	apr_time_exp_t exp_time;
163	if (apr_time_exp_gmt(&exp_time, time) != APR_SUCCESS)
164	{
165		return false;
166	}
167
168	if (year)
169		*year = exp_time.tm_year + 1900;
170
171	if (month)
172		*month = exp_time.tm_mon + 1;
173
174	if (day)
175		*day = exp_time.tm_mday;
176
177	if (hour)
178		*hour = exp_time.tm_hour;
179
180	if (min)
181		*min = exp_time.tm_min;
182
183	if (sec)
184		*sec = exp_time.tm_sec;
185
186	return true;
187}
188
189bool LLDate::fromString(const std::string& iso8601_date)
190{
191	std::istringstream stream(iso8601_date);
192	return fromStream(stream);
193}
194
195bool LLDate::fromStream(std::istream& s)
196{
197	struct apr_time_exp_t exp_time;
198	apr_int32_t tm_part;
199	int c;
200	
201	s >> tm_part;
202	exp_time.tm_year = tm_part - 1900;
203	c = s.get(); // skip the hypen
204	if (c != '-') { return false; }
205	s >> tm_part;
206	exp_time.tm_mon = tm_part - 1;
207	c = s.get(); // skip the hypen
208	if (c != '-') { return false; }
209	s >> tm_part;
210	exp_time.tm_mday = tm_part;
211	
212	c = s.get(); // skip the T
213	if (c != 'T') { return false; }
214	
215	s >> tm_part;
216	exp_time.tm_hour = tm_part;
217	c = s.get(); // skip the :
218	if (c != ':') { return false; }
219	s >> tm_part;
220	exp_time.tm_min = tm_part;
221	c = s.get(); // skip the :
222	if (c != ':') { return false; }
223	s >> tm_part;
224	exp_time.tm_sec = tm_part;
225
226	// zero out the unused fields
227	exp_time.tm_usec = 0;
228	exp_time.tm_wday = 0;
229	exp_time.tm_yday = 0;
230	exp_time.tm_isdst = 0;
231	exp_time.tm_gmtoff = 0;
232
233	// generate a time_t from that
234	apr_time_t time;
235	if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
236	{
237		return false;
238	}
239	
240	F64 seconds_since_epoch = time / LL_APR_USEC_PER_SEC;
241
242	// check for fractional
243	c = s.peek();
244	if(c == '.')
245	{
246		F64 fractional = 0.0;
247		s >> fractional;
248		seconds_since_epoch += fractional;
249	}
250
251	c = s.peek(); // check for offset
252	if (c == '+' || c == '-')
253	{
254		S32 offset_sign = (c == '+') ? 1 : -1;
255		S32 offset_hours = 0;
256		S32 offset_minutes = 0;
257		S32 offset_in_seconds = 0;
258
259		s >> offset_hours;
260
261		c = s.get(); // skip the colon a get the minutes if there are any
262		if (c == ':')
263		{		
264			s >> offset_minutes;
265		}
266		
267		offset_in_seconds =  (offset_hours * 60 + offset_sign * offset_minutes) * 60;
268		seconds_since_epoch -= offset_in_seconds;
269	}
270	else if (c != 'Z') { return false; } // skip the Z
271
272	mSecondsSinceEpoch = seconds_since_epoch;
273	return true;
274}
275
276bool LLDate::fromYMDHMS(S32 year, S32 month, S32 day, S32 hour, S32 min, S32 sec)
277{
278	struct apr_time_exp_t exp_time;
279	
280	exp_time.tm_year = year - 1900;
281	exp_time.tm_mon = month - 1;
282	exp_time.tm_mday = day;
283	exp_time.tm_hour = hour;
284	exp_time.tm_min = min;
285	exp_time.tm_sec = sec;
286
287	// zero out the unused fields
288	exp_time.tm_usec = 0;
289	exp_time.tm_wday = 0;
290	exp_time.tm_yday = 0;
291	exp_time.tm_isdst = 0;
292	exp_time.tm_gmtoff = 0;
293
294	// generate a time_t from that
295	apr_time_t time;
296	if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
297	{
298		return false;
299	}
300	
301	mSecondsSinceEpoch = time / LL_APR_USEC_PER_SEC;
302
303	return true;
304}
305
306F64 LLDate::secondsSinceEpoch() const
307{
308	return mSecondsSinceEpoch;
309}
310
311void LLDate::secondsSinceEpoch(F64 seconds)
312{
313	mSecondsSinceEpoch = seconds;
314}
315
316/* static */ LLDate LLDate::now()
317{
318	// time() returns seconds, we want fractions of a second, which LLTimer provides --RN
319	return LLDate(LLTimer::getTotalSeconds());
320}
321
322bool LLDate::operator<(const LLDate& rhs) const
323{
324    return mSecondsSinceEpoch < rhs.mSecondsSinceEpoch;
325}
326
327std::ostream& operator<<(std::ostream& s, const LLDate& date)
328{
329	date.toStream(s);
330	return s;
331}
332
333std::istream& operator>>(std::istream& s, LLDate& date)
334{
335	date.fromStream(s);
336	return s;
337}
338