]> git.tdb.fi Git - libs/core.git/blob - source/time/timezone.cpp
Rewrite Timer to use a heap instead of set to deal with duplicate timeouts
[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 #include <sstream>
10 #include <iomanip>
11 #ifdef WIN32
12 #include <windows.h>
13 #else
14 #include <fcntl.h>
15 #endif
16 #include <msp/core/except.h>
17 #include "timestamp.h"
18 #include "timezone.h"
19 #include "units.h"
20 #include "utils.h"
21
22 using namespace std;
23
24 namespace {
25
26 using Msp::Time::TimeZone;
27
28 #ifndef WIN32
29 long get_long(char *&ptr)
30 {
31         long result=0;
32         for(unsigned i=0; i<4; ++i)
33                 result=(result<<8)+static_cast<unsigned char >(*ptr++);
34         return result;
35 }
36 #endif
37
38 TimeZone get_local_timezone()
39 {
40 #ifdef WIN32
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());
45
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;
51
52         return TimeZone(offset);
53 #else
54         int fd=open("/etc/localtime", O_RDONLY);
55         if(fd>=-1)
56         {
57                 char hdr[44];
58                 int len=read(fd, hdr, sizeof(hdr));
59                 long gmtoff=-1;
60                 string name;
61                 if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
62                 {
63                         char *ptr=hdr+20;
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;
71                         char buf[size];
72                         len=read(fd, buf, size);
73                         if(len==size)
74                         {
75                                 ptr=buf;
76                                 int index=-1;
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)
80                                                 index=i;
81
82                                 if(index>0)
83                                         index=ptr[index];
84                                 ptr+=timecnt;
85
86                                 int abbrind=0;
87                                 for(int i=0; i<typecnt; ++i)
88                                 {
89                                         if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
90                                         {
91                                                 gmtoff=get_long(ptr);
92                                                 ++ptr;
93                                                 abbrind=*ptr++;
94                                         }
95                                         else
96                                                 ptr+=6;
97                                 }
98
99                                 name=ptr+abbrind;
100                         }
101                 }
102                 close(fd);
103
104                 if(gmtoff!=-1)
105                         return TimeZone(-gmtoff/60, name);
106         }
107         return TimeZone();
108 #endif
109 }
110
111 }
112
113 namespace Msp {
114 namespace Time {
115
116 TimeZone::TimeZone():
117         name("UTC")
118 { }
119
120 TimeZone::TimeZone(int minutes_west):
121         offset(minutes_west*min)
122 {
123         if(minutes_west)
124         {
125                 ostringstream ss;
126                 ss.fill('0');
127                 int m=abs(minutes_west);
128                 ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<<m/60;
129                 if(m%60)
130                         ss<<':'<<setw(2)<<m%60;
131         }
132         else
133                 name="UTC";
134 }
135
136 TimeZone::TimeZone(int minutes_west, const string &n):
137         name(n),
138         offset(minutes_west*min)
139 { }
140
141 const TimeZone &TimeZone::utc()
142 {
143         static TimeZone tz(0);
144         return tz;
145 }
146
147 const TimeZone &TimeZone::local()
148 {
149         static TimeZone tz=get_local_timezone();
150         return tz;
151 }
152
153 } // namespace Time
154 } // namespace Msp