xref: /haiku/src/kits/locale/TimeZone.cpp (revision 1345706a9ff6ad0dc041339a02d4259998b0765d)
1 /*
2  * Copyright (c) 2010, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
7  * 		Oliver Tappe <zooey@hirschkaefer.de>
8  */
9 
10 
11 #include <TimeZone.h>
12 
13 #include <unicode/timezone.h>
14 #include <ICUWrapper.h>
15 
16 
17 const char* BTimeZone::kNameOfGmtZone = "GMT";
18 
19 
20 BTimeZone::BTimeZone(const char* zoneCode)
21 {
22 	SetTo(zoneCode);
23 }
24 
25 
26 BTimeZone::~BTimeZone()
27 {
28 }
29 
30 
31 const BString&
32 BTimeZone::Name() const
33 {
34 	return fName;
35 }
36 
37 
38 const BString&
39 BTimeZone::DaylightSavingName() const
40 {
41 	return fDaylightSavingName;
42 }
43 
44 
45 const BString&
46 BTimeZone::ShortName() const
47 {
48 	return fShortName;
49 }
50 
51 
52 const BString&
53 BTimeZone::ShortDaylightSavingName() const
54 {
55 	return fShortDaylightSavingName;
56 }
57 
58 
59 const BString&
60 BTimeZone::Code() const
61 {
62 	return fCode;
63 }
64 
65 
66 int
67 BTimeZone::OffsetFromGMT() const
68 {
69 	return fOffsetFromGMT;
70 }
71 
72 
73 bool
74 BTimeZone::SupportsDaylightSaving() const
75 {
76 	return fSupportsDaylightSaving;
77 }
78 
79 
80 status_t
81 BTimeZone::InitCheck() const
82 {
83 	return fInitStatus;
84 }
85 
86 
87 status_t
88 BTimeZone::SetTo(const char* zoneCode)
89 {
90 	TimeZone* icuTimeZone;
91 	if (zoneCode == NULL || zoneCode[0] == '\0')
92 		icuTimeZone = TimeZone::createDefault();
93 	else
94 		icuTimeZone = TimeZone::createTimeZone(zoneCode);
95 
96 	UnicodeString unicodeString;
97 	icuTimeZone->getID(unicodeString);
98 	BStringByteSink converter(&fCode);
99 	unicodeString.toUTF8(converter);
100 
101 	unicodeString.remove();
102 	icuTimeZone->getDisplayName(false, TimeZone::LONG, unicodeString);
103 	converter.SetTo(&fName);
104 	unicodeString.toUTF8(converter);
105 
106 	unicodeString.remove();
107 	icuTimeZone->getDisplayName(true, TimeZone::LONG, unicodeString);
108 	converter.SetTo(&fDaylightSavingName);
109 	unicodeString.toUTF8(converter);
110 
111 	unicodeString.remove();
112 	icuTimeZone->getDisplayName(false, TimeZone::SHORT, unicodeString);
113 	converter.SetTo(&fShortName);
114 	unicodeString.toUTF8(converter);
115 
116 	unicodeString.remove();
117 	icuTimeZone->getDisplayName(true, TimeZone::SHORT, unicodeString);
118 	converter.SetTo(&fShortDaylightSavingName);
119 	unicodeString.toUTF8(converter);
120 
121 	fSupportsDaylightSaving = icuTimeZone->useDaylightTime();
122 
123 	int32_t rawOffset;
124 	int32_t dstOffset;
125 	UDate nowMillis = 1000 * (double)time(NULL);
126 
127 	UErrorCode error = U_ZERO_ERROR;
128 	icuTimeZone->getOffset(nowMillis, FALSE, rawOffset, dstOffset, error);
129 	if (!U_SUCCESS(error)) {
130 		fOffsetFromGMT = 0;
131 		fInitStatus = B_ERROR;
132 	} else {
133 		fOffsetFromGMT = (rawOffset + dstOffset) / 1000;
134 			// we want seconds, not ms (which ICU gives us)
135 		fInitStatus = B_OK;
136 	}
137 
138 	delete icuTimeZone;
139 
140 	return fInitStatus;
141 }
142