3 This file is part of libmspcore
4 Copyright © 2008-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
16 #include "../core/except.h"
17 #include "timestamp.h"
26 using Msp::Time::TimeZone;
29 long get_long(char *&ptr)
32 for(unsigned i=0; i<4; ++i)
33 result = (result<<8)+static_cast<unsigned char >(*ptr++);
38 TimeZone get_local_timezone()
41 TIME_ZONE_INFORMATION tzinfo;
42 DWORD dst = GetTimeZoneInformation(&tzinfo);
43 if(dst==TIME_ZONE_ID_INVALID)
44 throw Msp::SystemError("Failed to get time zone information", GetLastError());
46 int offset = tzinfo.Bias;
47 if(dst==TIME_ZONE_ID_STANDARD)
48 offset += tzinfo.StandardBias;
49 else if(dst==TIME_ZONE_ID_DAYLIGHT)
50 offset += tzinfo.DaylightBias;
52 return TimeZone(offset);
54 int fd = open("/etc/localtime", O_RDONLY);
58 int len = read(fd, hdr, sizeof(hdr));
61 if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
64 long isgmtcnt = get_long(ptr);
65 long isstdcnt = get_long(ptr);
66 long leapcnt = get_long(ptr);
67 long timecnt = get_long(ptr);
68 long typecnt = get_long(ptr);
69 long charcnt = get_long(ptr);
70 int size = timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
72 len = read(fd, buf, size);
77 time_t cur_time = Msp::Time::now().to_unixtime();
78 for(int i=0; i<timecnt; ++i)
79 if(get_long(ptr)<=cur_time)
87 for(int i=0; i<typecnt; ++i)
89 if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
91 gmtoff = get_long(ptr);
105 return TimeZone(-gmtoff/60, name);
116 TimeZone::TimeZone():
120 TimeZone::TimeZone(int minutes_west):
121 offset(minutes_west*min)
127 int m = abs(minutes_west);
128 ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<<m/60;
130 ss<<':'<<setw(2)<<m%60;
136 TimeZone::TimeZone(int minutes_west, const string &n):
138 offset(minutes_west*min)
141 const TimeZone &TimeZone::utc()
143 static TimeZone tz(0);
147 const TimeZone &TimeZone::local()
149 static TimeZone tz = get_local_timezone();