1 | // |
---|
2 | // Timestamp.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Timestamp.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: DateTime |
---|
8 | // Module: Timestamp |
---|
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/Timestamp.h> |
---|
38 | #include <Poco/Exception.h> |
---|
39 | #include <algorithm> |
---|
40 | #if defined(POCO_OS_FAMILY_UNIX) |
---|
41 | #include <time.h> |
---|
42 | #include <unistd.h> |
---|
43 | #include <sys/time.h> |
---|
44 | #include <sys/times.h> |
---|
45 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
---|
46 | #include <Poco/UnWindows.h> |
---|
47 | #endif |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | |
---|
52 | |
---|
53 | Timestamp::Timestamp() |
---|
54 | { |
---|
55 | update(); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | Timestamp::Timestamp(TimeVal tv) |
---|
60 | { |
---|
61 | _ts = tv; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | Timestamp::Timestamp(const Timestamp& other) |
---|
66 | { |
---|
67 | _ts = other._ts; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | Timestamp::~Timestamp() |
---|
72 | { |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | Timestamp& Timestamp::operator = (const Timestamp& other) |
---|
77 | { |
---|
78 | _ts = other._ts; |
---|
79 | return *this; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | Timestamp& Timestamp::operator = (TimeVal tv) |
---|
84 | { |
---|
85 | _ts = tv; |
---|
86 | return *this; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void Timestamp::swap(Timestamp& timestamp) |
---|
91 | { |
---|
92 | std::swap(_ts, timestamp._ts); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | Timestamp Timestamp::fromEpochTime(std::time_t t) |
---|
97 | { |
---|
98 | return Timestamp(TimeVal(t)*resolution()); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | Timestamp Timestamp::fromUtcTime(UtcTimeVal val) |
---|
103 | { |
---|
104 | val -= (TimeDiff(0x01b21dd2) << 32) + 0x13814000; |
---|
105 | val /= 10; |
---|
106 | return Timestamp(val); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void Timestamp::update() |
---|
111 | { |
---|
112 | #if defined(POCO_OS_FAMILY_WINDOWS) |
---|
113 | |
---|
114 | FILETIME ft; |
---|
115 | GetSystemTimeAsFileTime(&ft); |
---|
116 | ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME |
---|
117 | epoch.LowPart = 0xD53E8000; |
---|
118 | epoch.HighPart = 0x019DB1DE; |
---|
119 | |
---|
120 | ULARGE_INTEGER ts; |
---|
121 | ts.LowPart = ft.dwLowDateTime; |
---|
122 | ts.HighPart = ft.dwHighDateTime; |
---|
123 | ts.QuadPart -= epoch.QuadPart; |
---|
124 | _ts = ts.QuadPart/10; |
---|
125 | |
---|
126 | #else |
---|
127 | |
---|
128 | struct timeval tv; |
---|
129 | if (gettimeofday(&tv, NULL)) |
---|
130 | throw SystemException("cannot get time of day"); |
---|
131 | _ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec; |
---|
132 | |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | #if defined(_WIN32) |
---|
138 | |
---|
139 | |
---|
140 | Timestamp Timestamp::fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh) |
---|
141 | { |
---|
142 | ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME |
---|
143 | epoch.LowPart = 0xD53E8000; |
---|
144 | epoch.HighPart = 0x019DB1DE; |
---|
145 | |
---|
146 | ULARGE_INTEGER ts; |
---|
147 | ts.LowPart = fileTimeLow; |
---|
148 | ts.HighPart = fileTimeHigh; |
---|
149 | ts.QuadPart -= epoch.QuadPart; |
---|
150 | |
---|
151 | return Timestamp(ts.QuadPart/10); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void Timestamp::toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const |
---|
156 | { |
---|
157 | ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME |
---|
158 | epoch.LowPart = 0xD53E8000; |
---|
159 | epoch.HighPart = 0x019DB1DE; |
---|
160 | |
---|
161 | ULARGE_INTEGER ts; |
---|
162 | ts.QuadPart = _ts*10; |
---|
163 | ts.QuadPart += epoch.QuadPart; |
---|
164 | fileTimeLow = ts.LowPart; |
---|
165 | fileTimeHigh = ts.HighPart; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | #endif |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | } // namespace Poco |
---|