]> git.tdb.fi Git - libs/core.git/blob - source/time/timezone.cpp
fd2487b6475600412ca42700aa59cf36d58b7299
[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 #include <time.h>
12 #include "timezone.h"
13 #include "units.h"
14
15 using namespace std;
16
17 namespace {
18
19 using Msp::Time::TimeZone;
20
21 TimeZone get_local_timezone()
22 {
23 #ifdef WIN32
24         TIME_ZONE_INFORMATION tzinfo;
25         DWORD dst=GetTimeZoneInformation(&tzinfo);
26         if(dst==TIME_ZONE_ID_INVALID)
27                 throw SystemError("Failed to get time zone information", GetLastError());
28
29         int offset=tzinfo.Bias;
30         if(dst==TIME_ZONE_ID_STANDARD)
31                 offset+=tzinfo.StandardBias;
32         else if(dst==TIME_ZONE_ID_DAYLIGHT)
33                 offset+=tzinfo.DaylightBias;
34
35         return TimeZone(offset);
36 #else
37         tzset();
38         return TimeZone(timezone/60);
39 #endif
40 }
41
42 }
43
44 namespace Msp {
45 namespace Time {
46
47 TimeZone::TimeZone():
48         name("UTC")
49 { }
50
51 TimeZone::TimeZone(int minutes_west):
52         offset(minutes_west*min)
53 {
54         if(minutes_west)
55         {
56                 ostringstream ss;
57                 ss.fill('0');
58                 int m=abs(minutes_west);
59                 ss<<"UTC"<<(minutes_west<0 ? '-' : '+')<<m/60;
60                 if(m%60)
61                         ss<<':'<<setw(2)<<m%60;
62         }
63         else
64                 name="UTC";
65 }
66
67 const TimeZone &TimeZone::utc()
68 {
69         static TimeZone tz(0);
70         return tz;
71 }
72
73 const TimeZone &TimeZone::local()
74 {
75         static TimeZone tz=get_local_timezone();
76         return tz;
77 }
78
79 } // namespace Time
80 } // namespace Msp