X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Funix%2Ftimezone.cpp;fp=source%2Ftime%2Funix%2Ftimezone.cpp;h=e55969c8854711e0e23a1dff3b510e755422126d;hp=0000000000000000000000000000000000000000;hb=609c9a508cfdc7b42c46c4f21d17639204165a00;hpb=b4806214e905752617691f851717033fd3f266c2 diff --git a/source/time/unix/timezone.cpp b/source/time/unix/timezone.cpp new file mode 100644 index 0000000..e55969c --- /dev/null +++ b/source/time/unix/timezone.cpp @@ -0,0 +1,85 @@ +#include +#include +#include "timestamp.h" +#include "timezone.h" +#include "utils.h" + +using namespace std; + +namespace { + +long get_long(char *&ptr) +{ + long result = 0; + for(unsigned i=0; i<4; ++i) + result = (result<<8)+static_cast(*ptr++); + return result; +} + +} + + +namespace Msp { +namespace Time { + +TimeZone TimeZone::platform_get_local_timezone() +{ + 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(); +} + +} // namespace Time +} // namespace Msp