X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimezone.cpp;h=a2cd7a3f772743c7fe9ac06fde3042fd6ab28d98;hp=fd2487b6475600412ca42700aa59cf36d58b7299;hb=699670a264c7fc5a0b05c2d258a7552f5c594bd1;hpb=3fd9d04e84cdd72aabe8f9878f9e8ff006275bb6 diff --git a/source/time/timezone.cpp b/source/time/timezone.cpp index fd2487b..a2cd7a3 100644 --- a/source/time/timezone.cpp +++ b/source/time/timezone.cpp @@ -1,16 +1,16 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include -#include -#include -#include +#ifdef WIN32 +#include +#else +#include +#include +#endif +#include +#include +#include "timestamp.h" #include "timezone.h" #include "units.h" +#include "utils.h" using namespace std; @@ -18,24 +18,87 @@ 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(*ptr++); + return result; +} +#endif + TimeZone get_local_timezone() { #ifdef WIN32 TIME_ZONE_INFORMATION tzinfo; - DWORD dst=GetTimeZoneInformation(&tzinfo); + DWORD dst = GetTimeZoneInformation(&tzinfo); if(dst==TIME_ZONE_ID_INVALID) - throw SystemError("Failed to get time zone information", GetLastError()); + throw Msp::system_error("GetTimeZoneInformation"); - int offset=tzinfo.Bias; + int offset = tzinfo.Bias; if(dst==TIME_ZONE_ID_STANDARD) - offset+=tzinfo.StandardBias; + offset += tzinfo.StandardBias; else if(dst==TIME_ZONE_ID_DAYLIGHT) - offset+=tzinfo.DaylightBias; + offset += tzinfo.DaylightBias; 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 = new char[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; i0) + index = ptr[index]; + ptr += timecnt; + + int abbrind = 0; + for(int i=0; i=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1)) + { + gmtoff = get_long(ptr); + ++ptr; + abbrind = *ptr++; + } + else + ptr += 6; + } + + name = ptr+abbrind; + } + delete[] buf; + } + close(fd); + + if(gmtoff!=-1) + return TimeZone(gmtoff/60, name); + } + return TimeZone(); #endif } @@ -48,22 +111,25 @@ TimeZone::TimeZone(): name("UTC") { } -TimeZone::TimeZone(int minutes_west): - offset(minutes_west*min) +TimeZone::TimeZone(int minutes): + offset(minutes*min) { - if(minutes_west) + if(minutes) { - ostringstream ss; - ss.fill('0'); - int m=abs(minutes_west); - ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<