-/* $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 "../core/except.h"
+#include <msp/strings/format.h>
+#include <msp/core/systemerror.h>
#include "timestamp.h"
#include "timezone.h"
#include "units.h"
TIME_ZONE_INFORMATION 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;
if(dst==TIME_ZONE_ID_STANDARD)
return TimeZone(offset);
#else
int fd = open("/etc/localtime", O_RDONLY);
- if(fd>=-1)
+ if(fd!=-1)
{
char hdr[44];
int len = read(fd, hdr, sizeof(hdr));
long typecnt = get_long(ptr);
long charcnt = get_long(ptr);
int size = timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
- char buf[size];
+ char *buf = new char[size];
len = read(fd, buf, size);
if(len==size)
{
name = ptr+abbrind;
}
+ delete[] buf;
}
close(fd);
if(gmtoff!=-1)
- return TimeZone(-gmtoff/60, name);
+ return TimeZone(gmtoff/60, name);
}
return TimeZone();
#endif
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";
}
-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()