]> git.tdb.fi Git - libs/core.git/blob - source/time/timezone.cpp
Add missing headers
[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 "units.h"
13 #include "utils.h"
14
15 using namespace std;
16
17 namespace {
18
19 using Msp::Time::TimeZone;
20
21 #ifndef WIN32
22 long get_long(char *&ptr)
23 {
24         long result = 0;
25         for(unsigned i=0; i<4; ++i)
26                 result = (result<<8)+static_cast<unsigned char >(*ptr++);
27         return result;
28 }
29 #endif
30
31 TimeZone get_local_timezone()
32 {
33 #ifdef WIN32
34         TIME_ZONE_INFORMATION tzinfo;
35         DWORD dst = GetTimeZoneInformation(&tzinfo);
36         if(dst==TIME_ZONE_ID_INVALID)
37                 throw Msp::system_error("GetTimeZoneInformation");
38
39         int offset = tzinfo.Bias;
40         if(dst==TIME_ZONE_ID_STANDARD)
41                 offset += tzinfo.StandardBias;
42         else if(dst==TIME_ZONE_ID_DAYLIGHT)
43                 offset += tzinfo.DaylightBias;
44
45         return TimeZone(offset);
46 #else
47         int fd = open("/etc/localtime", O_RDONLY);
48         if(fd!=-1)
49         {
50                 char hdr[44];
51                 int len = read(fd, hdr, sizeof(hdr));
52                 long gmtoff = -1;
53                 string name;
54                 if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
55                 {
56                         char *ptr = hdr+20;
57                         long isgmtcnt = get_long(ptr);
58                         long isstdcnt = get_long(ptr);
59                         long leapcnt = get_long(ptr);
60                         long timecnt = get_long(ptr);
61                         long typecnt = get_long(ptr);
62                         long charcnt = get_long(ptr);
63                         int size = timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
64                         char buf[size];
65                         len = read(fd, buf, size);
66                         if(len==size)
67                         {
68                                 ptr = buf;
69                                 int index = -1;
70                                 time_t cur_time = Msp::Time::now().to_unixtime();
71                                 for(int i=0; i<timecnt; ++i)
72                                         if(get_long(ptr)<=cur_time)
73                                                 index = i;
74
75                                 if(index>0)
76                                         index = ptr[index];
77                                 ptr += timecnt;
78
79                                 int abbrind = 0;
80                                 for(int i=0; i<typecnt; ++i)
81                                 {
82                                         if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
83                                         {
84                                                 gmtoff = get_long(ptr);
85                                                 ++ptr;
86                                                 abbrind = *ptr++;
87                                         }
88                                         else
89                                                 ptr += 6;
90                                 }
91
92                                 name = ptr+abbrind;
93                         }
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