14 using namespace Gaudi;
64 # define SECS_1601_TO_1970 ((369 * 365 + 89) * 86400ui64)
68 static time_t timegm (
struct tm *t) {
70 time_t t1 = mktime (t);
73 time_t t2 = mktime(&gmt);
74 return t1 + (t1 - t2);
81 Time::Time(
int year,
int month,
int day,
82 int hour,
int min,
int sec,
86 memset (&val, 0,
sizeof (val));
92 val.tm_year = year > 1900 ? year - 1900 : year;
95 m_nsecs =
build (local, val, nsecs).m_nsecs;
101 Time Time::from (
const FILETIME *systime) {
102 ValueType t = ((ValueType) systime->dwHighDateTime << 32)
103 + (ValueType) systime->dwLowDateTime;
107 t = (t - SECS_1601_TO_1970 * (SEC_NSECS/100)) * 100;
117 GetSystemTimeAsFileTime (&ftime);
118 return from (&ftime);
121 if (gettimeofday (&tv, 0) != 0) {
124 tag <<
"errno=" <<
errno;
125 if( strerror_r(errno, buf, 256) == 0 ) {
128 msg <<
"Unknown error retrieving current time";
132 return Time (tv.tv_sec, tv.tv_usec * 1000);
139 return Time (local ? mktime(&tmp) : timegm(&tmp), 0) + diff;
146 tm Time::split (
bool local,
int *nsecpart )
const {
148 *nsecpart = (
int)(m_nsecs % SEC_NSECS);
150 time_t val = (time_t)(m_nsecs / SEC_NSECS);
154 localtime_r(&val, &retval);
156 gmtime_r(&val, &retval);
164 tm Time::utc (
int *nsecpart )
const {
165 return split (
false, nsecpart);
171 tm Time::local (
int *nsecpart )
const {
172 return split (
true, nsecpart);
176 int Time::year (
bool local)
const {
177 return split (local).tm_year + 1900;
181 int Time::month (
bool local)
const {
182 return split (local).tm_mon;
186 int Time::day (
bool local)
const {
187 return split (local).tm_mday;
191 int Time::hour (
bool local)
const {
192 return split (local).tm_hour;
196 int Time::minute (
bool local)
const {
197 return split (local).tm_min;
204 return split (local).tm_sec;
210 int Time::nsecond (
void)
const {
211 return (
int)(m_nsecs % SEC_NSECS);
215 int Time::weekday (
bool local)
const {
216 return split (local).tm_wday;
222 bool Time::isdst (
bool local)
const {
223 return split (local).tm_isdst > 0;
236 tm localtm = local ();
237 n = localtm.tm_gmtoff;
238 if (daylight) *daylight = localtm.tm_isdst;
241 time_t utctime = (time_t)(m_nsecs / SEC_NSECS);
243 localtime_s(&localtm, &utctime);
244 int savedaylight = localtm.tm_isdst;
246 gmtime_s(&gmt, &utctime);
248 gmt.tm_isdst = savedaylight;
249 n = utctime - mktime (&gmt);
251 if (daylight) *daylight = savedaylight;
253 return n * SEC_NSECS;
259 #pragma warning(push)
260 #pragma warning(disable:4996)
266 const char * Time::timezone (
int *daylight )
const {
267 tm localtm = local ();
268 if (daylight) *daylight = localtm.tm_isdst;
270 return tzname [localtm.tm_isdst > 0 ? 1 : 0];
283 tm time = split (local);
288 if (std::string::npos != pos) {
292 while (std::string::npos != pos) {
293 if (pos != 0 && spec[pos-1] !=
'%') {
294 spec.replace(pos, 2, ms);
296 pos = spec.find(
"%f", pos + 1);
299 const int MIN_BUF_SIZE = 128;
303 result.resize(std::max<std::string::size_type>(result.size()*2,
304 std::max<std::string::size_type>(spec.size()*2, MIN_BUF_SIZE))
306 length = ::strftime (&result[0], result.size(), spec.c_str(), &time);
309 result.resize (length);
323 std::string Time::nanoformat (
size_t minwidth ,
size_t maxwidth )
const {
324 TimeAssert( (minwidth >= 1) && (minwidth <= maxwidth) && (maxwidth <= 9),
325 "nanoformat options do not satisfy: 1 <= minwidth <= maxwidth <= 9");
328 int value = (
int)(m_nsecs % SEC_NSECS);
340 out.resize(
std::max(len, minwidth));
346 unsigned Time::toDosDate (
Time time) {
348 struct tm localtm = time.
local ();
350 unsigned mday = localtm.tm_mday;
351 unsigned mon = localtm.tm_mon + 1;
352 unsigned year = (localtm.tm_year > 80 ? localtm.tm_year - 80 : 0);
353 unsigned sec = localtm.tm_sec / 2;
354 unsigned min = localtm.tm_min;
355 unsigned hour = localtm.tm_hour;
356 return (mday << 16 | mon << 21 | year << 25
357 | sec | min << 5 | hour << 11);
361 Time Time::fromDosDate (
unsigned dosDate) {
367 memset (&localtm, 0,
sizeof (localtm));
368 localtm.tm_mday = (dosDate >> 16) & 0x1f;
369 localtm.tm_mon = ((dosDate >> 21) & 0xf) - 1;
370 localtm.tm_year = ((dosDate >> 25) & 0x7f) + 80;
371 localtm.tm_hour = (dosDate >> 11) & 0x1f;
372 localtm.tm_min = (dosDate >> 5) & 0x3f;
373 localtm.tm_sec = (dosDate & 0x1f) * 2;
374 localtm.tm_isdst = -1;
376 return Time (mktime (&localtm), 0);