]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timezone.cpp
Variable length arrays on stack are not valid C++
[libs/core.git] / source / time / timezone.cpp
index 3910c24bbf3a88bd615c1a8bc7ccfd1700c65719..a2cd7a3f772743c7fe9ac06fde3042fd6ab28d98 100644 (file)
@@ -1,19 +1,12 @@
-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <cstdlib>
-#include <sstream>
-#include <iomanip>
 #ifdef WIN32
 #include <windows.h>
 #else
+#include <unistd.h>
 #include <fcntl.h>
 #endif
-#include <msp/core/except.h>
+#include <msp/strings/format.h>
+#include <msp/core/systemerror.h>
 #include "timestamp.h"
 #include "timezone.h"
 #include "units.h"
@@ -28,9 +21,9 @@ using Msp::Time::TimeZone;
 #ifndef WIN32
 long get_long(char *&ptr)
 {
-       long result=0;
+       long result = 0;
        for(unsigned i=0; i<4; ++i)
-               result=(result<<8)+static_cast<unsigned char >(*ptr++);
+               result = (result<<8)+static_cast<unsigned char >(*ptr++);
        return result;
 }
 #endif
@@ -39,70 +32,71 @@ 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 Msp::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
-       int fd=open("/etc/localtime", O_RDONLY);
-       if(fd>=-1)
+       int fd = open("/etc/localtime", O_RDONLY);
+       if(fd!=-1)
        {
                char hdr[44];
-               int len=read(fd, hdr, sizeof(hdr));
-               long gmtoff=-1;
+               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);
+                       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();
+                               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;
+                                               index = i;
 
                                if(index>0)
-                                       index=ptr[index];
-                               ptr+=timecnt;
+                                       index = ptr[index];
+                               ptr += timecnt;
 
-                               int abbrind=0;
+                               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);
+                                               gmtoff = get_long(ptr);
                                                ++ptr;
-                                               abbrind=*ptr++;
+                                               abbrind = *ptr++;
                                        }
                                        else
-                                               ptr+=6;
+                                               ptr += 6;
                                }
 
-                               name=ptr+abbrind;
+                               name = ptr+abbrind;
                        }
+                       delete[] buf;
                }
                close(fd);
 
                if(gmtoff!=-1)
-                       return TimeZone(-gmtoff/60, name);
+                       return TimeZone(gmtoff/60, name);
        }
        return TimeZone();
 #endif
@@ -117,25 +111,23 @@ 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 ? '-' : '+')<<m/60;
+               int m = abs(minutes);
+               name = format("UTC%c%d", (minutes<0 ? '-' : '+'), m/60);
                if(m%60)
-                       ss<<':'<<setw(2)<<m%60;
+                       name += format(":%02d", m%60);
        }
        else
-               name="UTC";
+               name = "UTC";
 }
 
-TimeZone::TimeZone(int minutes_west, const string &n):
+TimeZone::TimeZone(int minutes, const string &n):
        name(n),
-       offset(minutes_west*min)
+       offset(minutes*min)
 { }
 
 const TimeZone &TimeZone::utc()
@@ -146,7 +138,7 @@ const TimeZone &TimeZone::utc()
 
 const TimeZone &TimeZone::local()
 {
-       static TimeZone tz=get_local_timezone();
+       static TimeZone tz = get_local_timezone();
        return tz;
 }