]> git.tdb.fi Git - libs/core.git/blobdiff - 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
index 541514f8b8272c1573b4a361c85675a3cf92ce09..3910c24bbf3a88bd615c1a8bc7ccfd1700c65719 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspcore
-Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -11,11 +11,13 @@ Distributed under the LGPL
 #ifdef WIN32
 #include <windows.h>
 #else
-#include <time.h>
+#include <fcntl.h>
 #endif
 #include <msp/core/except.h>
+#include "timestamp.h"
 #include "timezone.h"
 #include "units.h"
+#include "utils.h"
 
 using namespace std;
 
@@ -23,6 +25,16 @@ namespace {
 
 using Msp::Time::TimeZone;
 
+#ifndef WIN32
+long get_long(char *&ptr)
+{
+       long result=0;
+       for(unsigned i=0; i<4; ++i)
+               result=(result<<8)+static_cast<unsigned char >(*ptr++);
+       return result;
+}
+#endif
+
 TimeZone get_local_timezone()
 {
 #ifdef WIN32
@@ -39,8 +51,60 @@ TimeZone get_local_timezone()
 
        return TimeZone(offset);
 #else
-       tzset();
-       return TimeZone(timezone/60);
+       int fd=open("/etc/localtime", O_RDONLY);
+       if(fd>=-1)
+       {
+               char hdr[44];
+               int len=read(fd, hdr, sizeof(hdr));
+               long gmtoff=-1;
+               string name;
+               if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
+               {
+                       char *ptr=hdr+20;
+                       long isgmtcnt=get_long(ptr);
+                       long isstdcnt=get_long(ptr);
+                       long leapcnt=get_long(ptr);
+                       long timecnt=get_long(ptr);
+                       long typecnt=get_long(ptr);
+                       long charcnt=get_long(ptr);
+                       int size=timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
+                       char buf[size];
+                       len=read(fd, buf, size);
+                       if(len==size)
+                       {
+                               ptr=buf;
+                               int index=-1;
+                               time_t cur_time=Msp::Time::now().to_unixtime();
+                               for(int i=0; i<timecnt; ++i)
+                                       if(get_long(ptr)<=cur_time)
+                                               index=i;
+
+                               if(index>0)
+                                       index=ptr[index];
+                               ptr+=timecnt;
+
+                               int abbrind=0;
+                               for(int i=0; i<typecnt; ++i)
+                               {
+                                       if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
+                                       {
+                                               gmtoff=get_long(ptr);
+                                               ++ptr;
+                                               abbrind=*ptr++;
+                                       }
+                                       else
+                                               ptr+=6;
+                               }
+
+                               name=ptr+abbrind;
+                       }
+               }
+               close(fd);
+
+               if(gmtoff!=-1)
+                       return TimeZone(-gmtoff/60, name);
+       }
+       return TimeZone();
 #endif
 }
 
@@ -69,6 +133,11 @@ TimeZone::TimeZone(int minutes_west):
                name="UTC";
 }
 
+TimeZone::TimeZone(int minutes_west, const string &n):
+       name(n),
+       offset(minutes_west*min)
+{ }
+
 const TimeZone &TimeZone::utc()
 {
        static TimeZone tz(0);