]> git.tdb.fi Git - libs/core.git/blob - source/time/timezone.cpp
Fix some win32 compile errors
[libs/core.git] / source / time / timezone.cpp
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cstdlib>
9 #include <sstream>
10 #include <iomanip>
11 #ifdef WIN32
12 #include <windows.h>
13 #else
14 #include <time.h>
15 #endif
16 #include <msp/core/except.h>
17 #include "timezone.h"
18 #include "units.h"
19
20 using namespace std;
21
22 namespace {
23
24 using Msp::Time::TimeZone;
25
26 TimeZone get_local_timezone()
27 {
28 #ifdef WIN32
29         TIME_ZONE_INFORMATION tzinfo;
30         DWORD dst=GetTimeZoneInformation(&tzinfo);
31         if(dst==TIME_ZONE_ID_INVALID)
32                 throw Msp::SystemError("Failed to get time zone information", GetLastError());
33
34         int offset=tzinfo.Bias;
35         if(dst==TIME_ZONE_ID_STANDARD)
36                 offset+=tzinfo.StandardBias;
37         else if(dst==TIME_ZONE_ID_DAYLIGHT)
38                 offset+=tzinfo.DaylightBias;
39
40         return TimeZone(offset);
41 #else
42         tzset();
43         return TimeZone(timezone/60);
44 #endif
45 }
46
47 }
48
49 namespace Msp {
50 namespace Time {
51
52 TimeZone::TimeZone():
53         name("UTC")
54 { }
55
56 TimeZone::TimeZone(int minutes_west):
57         offset(minutes_west*min)
58 {
59         if(minutes_west)
60         {
61                 ostringstream ss;
62                 ss.fill('0');
63                 int m=abs(minutes_west);
64                 ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<<m/60;
65                 if(m%60)
66                         ss<<':'<<setw(2)<<m%60;
67         }
68         else
69                 name="UTC";
70 }
71
72 const TimeZone &TimeZone::utc()
73 {
74         static TimeZone tz(0);
75         return tz;
76 }
77
78 const TimeZone &TimeZone::local()
79 {
80         static TimeZone tz=get_local_timezone();
81         return tz;
82 }
83
84 } // namespace Time
85 } // namespace Msp