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