]> git.tdb.fi Git - libs/core.git/blob - source/time/timezone.cpp
7baf4943bec4b436c479d06d4394c9004f91f2d3
[libs/core.git] / source / time / timezone.cpp
1 #include <cstdlib>
2 #ifdef WIN32
3 #include <windows.h>
4 #else
5 #include <unistd.h>
6 #include <fcntl.h>
7 #endif
8 #include <msp/strings/format.h>
9 #include <msp/core/systemerror.h>
10 #include "timestamp.h"
11 #include "timezone.h"
12 #include "utils.h"
13
14 using namespace std;
15
16 namespace {
17
18 using Msp::Time::TimeZone;
19
20 #ifndef WIN32
21 long get_long(char *&ptr)
22 {
23         long result = 0;
24         for(unsigned i=0; i<4; ++i)
25                 result = (result<<8)+static_cast<unsigned char >(*ptr++);
26         return result;
27 }
28 #endif
29
30 TimeZone get_local_timezone()
31 {
32 #ifdef WIN32
33         TIME_ZONE_INFORMATION tzinfo;
34         DWORD dst = GetTimeZoneInformation(&tzinfo);
35         if(dst==TIME_ZONE_ID_INVALID)
36                 throw Msp::system_error("GetTimeZoneInformation");
37
38         int offset = tzinfo.Bias;
39         if(dst==TIME_ZONE_ID_STANDARD)
40                 offset += tzinfo.StandardBias;
41         else if(dst==TIME_ZONE_ID_DAYLIGHT)
42                 offset += tzinfo.DaylightBias;
43
44         return TimeZone(offset);
45 #else
46         int fd = open("/etc/localtime", O_RDONLY);
47         if(fd!=-1)
48         {
49                 char hdr[44];
50                 int len = read(fd, hdr, sizeof(hdr));
51                 long gmtoff = -1;
52                 string name;
53                 if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
54                 {
55                         char *ptr = hdr+20;
56                         long isgmtcnt = get_long(ptr);
57                         long isstdcnt = get_long(ptr);
58                         long leapcnt = get_long(ptr);
59                         long timecnt = get_long(ptr);
60                         long typecnt = get_long(ptr);
61                         long charcnt = get_long(ptr);
62                         int size = timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
63                         char *buf = new char[size];
64                         len = read(fd, buf, size);
65                         if(len==size)
66                         {
67                                 ptr = buf;
68                                 int index = -1;
69                                 time_t cur_time = Msp::Time::now().to_unixtime();
70                                 for(int i=0; i<timecnt; ++i)
71                                         if(get_long(ptr)<=cur_time)
72                                                 index = i;
73
74                                 if(index>0)
75                                         index = ptr[index];
76                                 ptr += timecnt;
77
78                                 int abbrind = 0;
79                                 for(int i=0; i<typecnt; ++i)
80                                 {
81                                         if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
82                                         {
83                                                 gmtoff = get_long(ptr);
84                                                 ++ptr;
85                                                 abbrind = *ptr++;
86                                         }
87                                         else
88                                                 ptr += 6;
89                                 }
90
91                                 name = ptr+abbrind;
92                         }
93                         delete[] buf;
94                 }
95                 close(fd);
96
97                 if(gmtoff!=-1)
98                         return TimeZone(gmtoff/60, name);
99         }
100         return TimeZone();
101 #endif
102 }
103
104 }
105
106 namespace Msp {
107 namespace Time {
108
109 TimeZone::TimeZone():
110         name("UTC")
111 { }
112
113 TimeZone::TimeZone(int minutes):
114         offset(minutes*min)
115 {
116         if(minutes)
117         {
118                 int m = abs(minutes);
119                 name = format("UTC%c%d", (minutes<0 ? '-' : '+'), m/60);
120                 if(m%60)
121                         name += format(":%02d", m%60);
122         }
123         else
124                 name = "UTC";
125 }
126
127 TimeZone::TimeZone(int minutes, const string &n):
128         name(n),
129         offset(minutes*min)
130 { }
131
132 const TimeZone &TimeZone::utc()
133 {
134         static TimeZone tz(0);
135         return tz;
136 }
137
138 const TimeZone &TimeZone::local()
139 {
140         static TimeZone tz = get_local_timezone();
141         return tz;
142 }
143
144 } // namespace Time
145 } // namespace Msp