aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/lldate.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/lldate.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/lldate.cpp')
-rw-r--r--linden/indra/llcommon/lldate.cpp193
1 files changed, 193 insertions, 0 deletions
diff --git a/linden/indra/llcommon/lldate.cpp b/linden/indra/llcommon/lldate.cpp
new file mode 100644
index 0000000..211d350
--- /dev/null
+++ b/linden/indra/llcommon/lldate.cpp
@@ -0,0 +1,193 @@
1/**
2 * @file lldate.cpp
3 * @author Phoenix
4 * @date 2006-02-05
5 * @brief Implementation of the date class
6 *
7 * Copyright (c) 2006-2007, Linden Research, Inc.
8 *
9 * The source code in this file ("Source Code") is provided by Linden Lab
10 * to you under the terms of the GNU General Public License, version 2.0
11 * ("GPL"), unless you have obtained a separate licensing agreement
12 * ("Other License"), formally executed by you and Linden Lab. Terms of
13 * the GPL can be found in doc/GPL-license.txt in this distribution, or
14 * online at http://secondlife.com/developers/opensource/gplv2
15 *
16 * There are special exceptions to the terms and conditions of the GPL as
17 * it is applied to this Source Code. View the full text of the exception
18 * in the file doc/FLOSS-exception.txt in this software distribution, or
19 * online at http://secondlife.com/developers/opensource/flossexception
20 *
21 * By copying, modifying or distributing this software, you acknowledge
22 * that you have read and understood your obligations described above,
23 * and agree to abide by those obligations.
24 *
25 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
26 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
27 * COMPLETENESS OR PERFORMANCE.
28 */
29
30#include "linden_common.h"
31#include "lldate.h"
32
33#include "apr-1/apr_time.h"
34
35#include <iomanip>
36#include <sstream>
37
38static const F64 DATE_EPOCH = 0.0;
39
40static const F64 LL_APR_USEC_PER_SEC = 1000000.0;
41 // should be APR_USEC_PER_SEC, but that relies on INT64_C which
42 // isn't defined in glib under our build set up for some reason
43
44
45LLDate::LLDate() : mSecondsSinceEpoch(DATE_EPOCH)
46{
47}
48
49LLDate::LLDate(const LLDate& date) :
50 mSecondsSinceEpoch(date.mSecondsSinceEpoch)
51{
52}
53
54LLDate::LLDate(F64 seconds_since_epoch) :
55 mSecondsSinceEpoch(seconds_since_epoch)
56{
57}
58
59LLDate::LLDate(const std::string& iso8601_date)
60{
61 if(!fromString(iso8601_date))
62 {
63 mSecondsSinceEpoch = DATE_EPOCH;
64 }
65}
66
67std::string LLDate::asString() const
68{
69 std::ostringstream stream;
70 toStream(stream);
71 return stream.str();
72}
73
74void LLDate::toStream(std::ostream& s) const
75{
76 apr_time_t time = (apr_time_t)(mSecondsSinceEpoch * LL_APR_USEC_PER_SEC);
77
78 apr_time_exp_t exp_time;
79 if (apr_time_exp_gmt(&exp_time, time) != APR_SUCCESS)
80 {
81 s << "1970-01-01T00:00:00Z";
82 return;
83 }
84
85 s << std::dec << std::setfill('0');
86#if( LL_WINDOWS || __GNUC__ > 2)
87 s << std::right;
88#else
89 s.setf(ios::right);
90#endif
91 s << std::setw(4) << (exp_time.tm_year + 1900)
92 << '-' << std::setw(2) << (exp_time.tm_mon + 1)
93 << '-' << std::setw(2) << (exp_time.tm_mday)
94 << 'T' << std::setw(2) << (exp_time.tm_hour)
95 << ':' << std::setw(2) << (exp_time.tm_min)
96 << ':' << std::setw(2) << (exp_time.tm_sec);
97 if (exp_time.tm_usec > 0)
98 {
99 s << '.' << std::setw(2)
100 << (int)(exp_time.tm_usec / (LL_APR_USEC_PER_SEC / 100));
101 }
102 s << 'Z';
103}
104
105bool LLDate::fromString(const std::string& iso8601_date)
106{
107 std::istringstream stream(iso8601_date);
108 return fromStream(stream);
109}
110
111bool LLDate::fromStream(std::istream& s)
112{
113 struct apr_time_exp_t exp_time;
114 apr_int32_t tm_part;
115 int c;
116
117 s >> tm_part;
118 exp_time.tm_year = tm_part - 1900;
119 c = s.get(); // skip the hypen
120 if (c != '-') { return false; }
121 s >> tm_part;
122 exp_time.tm_mon = tm_part - 1;
123 c = s.get(); // skip the hypen
124 if (c != '-') { return false; }
125 s >> tm_part;
126 exp_time.tm_mday = tm_part;
127
128 c = s.get(); // skip the T
129 if (c != 'T') { return false; }
130
131 s >> tm_part;
132 exp_time.tm_hour = tm_part;
133 c = s.get(); // skip the :
134 if (c != ':') { return false; }
135 s >> tm_part;
136 exp_time.tm_min = tm_part;
137 c = s.get(); // skip the :
138 if (c != ':') { return false; }
139 s >> tm_part;
140 exp_time.tm_sec = tm_part;
141
142 // zero out the unused fields
143 exp_time.tm_usec = 0;
144 exp_time.tm_wday = 0;
145 exp_time.tm_yday = 0;
146 exp_time.tm_isdst = 0;
147 exp_time.tm_gmtoff = 0;
148
149 // generate a time_t from that
150 apr_time_t time;
151 if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
152 {
153 return false;
154 }
155
156 F64 seconds_since_epoch = time / LL_APR_USEC_PER_SEC;
157
158 // check for fractional
159 c = s.peek();
160 if(c == '.')
161 {
162 F64 fractional = 0.0;
163 s >> fractional;
164 seconds_since_epoch += fractional;
165 }
166 s.get(); // skip the Z
167 if (c != 'Z') { return false; }
168
169 mSecondsSinceEpoch = seconds_since_epoch;
170 return true;
171}
172
173F64 LLDate::secondsSinceEpoch() const
174{
175 return mSecondsSinceEpoch;
176}
177
178void LLDate::secondsSinceEpoch(F64 seconds)
179{
180 mSecondsSinceEpoch = seconds;
181}
182
183std::ostream& operator<<(std::ostream& s, const LLDate& date)
184{
185 date.toStream(s);
186 return s;
187}
188
189std::istream& operator>>(std::istream& s, LLDate& date)
190{
191 date.fromStream(s);
192 return s;
193}