source: XMLIO_V2/external/src/POCO/Foundation.save/LocalDateTime.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1//
2// LocalDateTime.cpp
3//
4// $Id: //poco/1.3/Foundation/src/LocalDateTime.cpp#6 $
5//
6// Library: Foundation
7// Package: DateTime
8// Module:  LocalDateTime
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include "Poco/LocalDateTime.h"
38#include "Poco/Timezone.h"
39#include "Poco/Timespan.h"
40#include "Poco/Exception.h"
41#include <algorithm>
42#include <ctime>
43
44
45namespace Poco {
46
47
48LocalDateTime::LocalDateTime()
49{
50        determineTzd(true);
51}
52
53
54LocalDateTime::LocalDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond):
55        _dateTime(year, month, day, hour, minute, second, millisecond, microsecond)
56{
57        determineTzd();
58}
59
60
61LocalDateTime::LocalDateTime(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond):
62        _dateTime(year, month, day, hour, minute, second, millisecond, microsecond),
63        _tzd(tzd)
64{
65}
66
67
68LocalDateTime::LocalDateTime(double julianDay):
69        _dateTime(julianDay)
70{
71        determineTzd(true);
72}
73
74
75LocalDateTime::LocalDateTime(int tzd, double julianDay):
76        _dateTime(julianDay),
77        _tzd(tzd)
78{
79        adjustForTzd();
80}
81
82
83LocalDateTime::LocalDateTime(const DateTime& dateTime):
84        _dateTime(dateTime)
85{
86        determineTzd(true);
87}
88
89
90LocalDateTime::LocalDateTime(int tzd, const DateTime& dateTime):
91        _dateTime(dateTime),
92        _tzd(tzd)
93{
94        adjustForTzd();
95}
96
97
98LocalDateTime::LocalDateTime(int tzd, const DateTime& dateTime, bool adjust):
99        _dateTime(dateTime),
100        _tzd(tzd)
101{
102        if (adjust)
103                adjustForTzd();
104}
105
106
107LocalDateTime::LocalDateTime(const LocalDateTime& dateTime):
108        _dateTime(dateTime._dateTime),
109        _tzd(dateTime._tzd)
110{
111}
112
113
114LocalDateTime::LocalDateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff, int tzd):
115        _dateTime(utcTime, diff),
116        _tzd(tzd)
117{
118        adjustForTzd();
119}
120
121       
122LocalDateTime::~LocalDateTime()
123{
124}
125
126
127LocalDateTime& LocalDateTime::operator = (const LocalDateTime& dateTime)
128{
129        if (&dateTime != this)
130        {
131                _dateTime = dateTime._dateTime;
132                _tzd      = dateTime._tzd;
133        }
134        return *this;
135}
136
137
138LocalDateTime& LocalDateTime::operator = (const Timestamp& timestamp)
139{
140        if (timestamp != this->timestamp())
141        {
142                _dateTime = timestamp;
143                determineTzd(true);
144        }
145        return *this;
146}
147
148
149LocalDateTime& LocalDateTime::operator = (double julianDay)
150{
151        _dateTime = julianDay;
152        determineTzd(true);
153        return *this;
154}
155
156
157LocalDateTime& LocalDateTime::assign(int year, int month, int day, int hour, int minute, int second, int millisecond, int microseconds)
158{
159        _dateTime.assign(year, month, day, hour, minute, second, millisecond, microseconds);
160        determineTzd(false);
161        return *this;
162}
163
164
165LocalDateTime& LocalDateTime::assign(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microseconds)
166{
167        _dateTime.assign(year, month, day, hour, minute, second, millisecond, microseconds);
168        _tzd = tzd;
169        return *this;
170}
171
172
173LocalDateTime& LocalDateTime::assign(int tzd, double julianDay)
174{
175        _tzd      = tzd;
176        _dateTime = julianDay;
177        adjustForTzd();
178        return *this;
179}
180
181
182void LocalDateTime::swap(LocalDateTime& dateTime)
183{
184        _dateTime.swap(dateTime._dateTime);
185        std::swap(_tzd, dateTime._tzd);
186}
187
188
189DateTime LocalDateTime::utc() const
190{
191        return DateTime(_dateTime.utcTime(), -((Timestamp::TimeDiff) _tzd)*Timespan::SECONDS);
192}
193
194
195bool LocalDateTime::operator == (const LocalDateTime& dateTime) const
196{
197        return utcTime() == dateTime.utcTime();
198}
199
200
201bool LocalDateTime::operator != (const LocalDateTime& dateTime) const   
202{
203        return utcTime() != dateTime.utcTime();
204}
205
206
207bool LocalDateTime::operator <  (const LocalDateTime& dateTime) const   
208{
209        return utcTime() < dateTime.utcTime();
210}
211
212
213bool LocalDateTime::operator <= (const LocalDateTime& dateTime) const   
214{
215        return utcTime() <= dateTime.utcTime();
216}
217
218
219bool LocalDateTime::operator >  (const LocalDateTime& dateTime) const   
220{
221        return utcTime() > dateTime.utcTime();
222}
223
224
225bool LocalDateTime::operator >= (const LocalDateTime& dateTime) const   
226{
227        return utcTime() >= dateTime.utcTime();
228}
229
230
231LocalDateTime LocalDateTime::operator + (const Timespan& span) const
232{
233        // First calculate the adjusted UTC time, then calculate the
234        // locally adjusted time by constructing a new LocalDateTime.
235        DateTime tmp(utcTime(), span.totalMicroseconds());
236        return LocalDateTime(tmp);
237}
238
239
240LocalDateTime LocalDateTime::operator - (const Timespan& span) const
241{
242        // First calculate the adjusted UTC time, then calculate the
243        // locally adjusted time by constructing a new LocalDateTime.
244        DateTime tmp(utcTime(), -span.totalMicroseconds());
245        return LocalDateTime(tmp);
246}
247
248
249Timespan LocalDateTime::operator - (const LocalDateTime& dateTime) const
250{
251        return Timespan((utcTime() - dateTime.utcTime())/10);
252}
253
254
255LocalDateTime& LocalDateTime::operator += (const Timespan& span)
256{
257        // Use the same trick as in operator+. Create a UTC time, adjust
258        // it for the span, and convert back to LocalDateTime. This will
259        // recalculate the tzd correctly in the case where the addition
260        // crosses a DST boundary.
261        *this = DateTime(utcTime(), span.totalMicroseconds());
262        return *this;
263}
264
265
266LocalDateTime& LocalDateTime::operator -= (const Timespan& span)
267{
268        // Use the same trick as in operator-. Create a UTC time, adjust
269        // it for the span, and convert back to LocalDateTime. This will
270        // recalculate the tzd correctly in the case where the subtraction
271        // crosses a DST boundary.
272        *this = DateTime(utcTime(), -span.totalMicroseconds());
273        return *this;
274}
275
276
277void LocalDateTime::determineTzd(bool adjust)
278{
279        if (adjust)
280        {
281                std::time_t epochTime = _dateTime.timestamp().epochTime();
282#if defined(_WIN32) || defined(POCO_NO_POSIX_TSF)
283                std::tm* broken = std::localtime(&epochTime);
284                if (!broken) throw Poco::SystemException("cannot get local time");
285                _tzd = (Timezone::utcOffset() + ((broken->tm_isdst == 1) ? 3600 : 0));
286#else
287                std::tm broken;
288                if (!localtime_r(&epochTime, &broken))
289                        throw Poco::SystemException("cannot get local time");
290                _tzd = (Timezone::utcOffset() + ((broken.tm_isdst == 1) ? 3600 : 0));
291#endif
292                adjustForTzd();
293        }
294        else
295        {
296                int dst;
297                dstOffset(dst);
298                _tzd = (Timezone::utcOffset() + dst);
299        }
300}
301
302
303std::time_t LocalDateTime::dstOffset(int& dstOffset) const
304{
305        std::time_t local;
306        std::tm     broken;
307
308        broken.tm_year  = (_dateTime.year() - 1900);
309        broken.tm_mon   = (_dateTime.month() - 1);
310        broken.tm_mday  = _dateTime.day();
311        broken.tm_hour  = _dateTime.hour();
312        broken.tm_min   = _dateTime.minute();
313        broken.tm_sec   = _dateTime.second();
314        broken.tm_isdst = -1;
315        local = std::mktime(&broken);
316       
317        dstOffset = (broken.tm_isdst == 1) ? 3600 : 0;
318        return local;
319}
320
321
322} // namespace Poco
323
Note: See TracBrowser for help on using the repository browser.