2 /// \file r_timezone.cc
3 /// Record parsing class for the timezone database.
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2008, Brian Edginton
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "r_timezone.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
33 #include "ios_state.h"
36 using namespace Barry::Protocol
;
41 ///////////////////////////////////////////////////////////////////////////////
44 // TimeZone Field Codes
45 #define TZFC_INDEX 0x01
46 #define TZFC_NAME 0x02
47 #define TZFC_OFFSET 0x03
49 #define TZFC_STARTMONTH 0x06
50 #define TZFC_ENDMONTH 0x0B
51 #define TZFC_TZTYPE 0x64
53 #define TZFC_END 0xffff
55 static FieldLink
<TimeZone
> TimeZoneFieldLinks
[] = {
56 { TZFC_NAME
, "Name", 0, 0, &TimeZone::Name
, 0, 0, 0, 0, true },
57 { TZFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
65 TimeZone::TimeZone(int utc_offset
)
69 UTCOffset
= utc_offset
;
72 TimeZone::TimeZone(int hours
, int minutes
)
76 // make sure that minutes are always positive, for our logic
80 // calculate offset in total minutes
81 UTCOffset
= hours
* 60;
92 const unsigned char* TimeZone::ParseField(const unsigned char *begin
,
93 const unsigned char *end
,
96 const CommonField
*field
= (const CommonField
*) begin
;
98 // advance and check size
99 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
100 if( begin
> end
) // if begin==end, we are ok
103 if( !btohs(field
->size
) ) // if field has no size, something's up
106 if( field
->type
== TZFC_TZTYPE
) {
107 if( ( TZType
= field
->u
.uint32
) != 1 ) {
108 throw Error("TimeZone::ParseField: TimeZone Type is not valid");
113 // cycle through the type table
114 for( FieldLink
<TimeZone
> *b
= TimeZoneFieldLinks
;
118 if( b
->type
== field
->type
) {
120 std::string
&s
= this->*(b
->strMember
);
121 s
= ParseFieldString(field
);
122 if( b
->iconvNeeded
&& ic
)
124 return begin
; // done!
129 switch( field
->type
)
132 Index
= btohl(field
->u
.uint32
);
136 UTCOffset
= btohs(field
->u
.int16
);
140 DSTOffset
= btohl(field
->u
.uint32
);
146 case TZFC_STARTMONTH
:
147 StartMonth
= btohl(field
->u
.uint32
);
151 EndMonth
= btohl(field
->u
.uint32
);
155 // if still not handled, add to the Unknowns list
157 uf
.type
= field
->type
;
158 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
159 Unknowns
.push_back(uf
);
161 // return new pointer for next field
165 void TimeZone::ParseHeader(const Data
&data
, size_t &offset
)
167 // no header in Task records
170 void TimeZone::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
172 const unsigned char *finish
= ParseCommonFields(*this,
173 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
174 offset
+= finish
- (data
.GetData() + offset
);
177 void TimeZone::Validate() const
181 void TimeZone::BuildHeader(Data
&data
, size_t &offset
) const
183 // not yet implemented
186 void TimeZone::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
188 // not yet implemented
191 void TimeZone::Clear()
193 RecType
= GetDefaultRecType();
210 const FieldHandle
<TimeZone
>::ListT
& TimeZone::GetFieldHandles()
212 static FieldHandle
<TimeZone
>::ListT fhv
;
217 #undef CONTAINER_OBJECT_NAME
218 #define CONTAINER_OBJECT_NAME fhv
220 #undef RECORD_CLASS_NAME
221 #define RECORD_CLASS_NAME TimeZone
223 FHP(RecType
, "Record Type Code");
224 FHP(RecordId
, "Unique Record ID");
226 FHD(Name
, "TimeZone Name", TZFC_NAME
, true);
227 FHD(Index
, "Index", TZFC_INDEX
, false);
228 FHD(UTCOffset
, "TimeZone Offset in Minutes", TZFC_OFFSET
, false);
229 FHD(UseDST
, "Use DST?", TZFC_DST
, false);
230 FHD(DSTOffset
, "DST Offset", TZFC_DST
, false);
231 FHD(StartMonth
, "Start Month", TZFC_STARTMONTH
, false);
232 FHD(EndMonth
, "End Month", TZFC_ENDMONTH
, false);
233 FHD(TZType
, "TimeZone Type", TZFC_TZTYPE
, false);
235 FHP(Unknowns
, "Unknown Fields");
240 std::string
TimeZone::GetDescription() const
243 oss
<< Name
<< " (" << dec
<< (UTCOffset
/ 60.0) << ")";
247 void TimeZone::Dump(std::ostream
&os
) const
249 ios_format_state
state(os
);
251 static const char *month
[] = {
252 "Jan", "Feb", "Mar", "Apr", "May",
253 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
256 os
<< "TimeZone entry: 0x" << setbase(16) << RecordId
257 << " (" << (unsigned int)RecType
<< ")\n";
259 // cycle through the type table
260 for( const FieldLink
<TimeZone
> *b
= TimeZoneFieldLinks
;
265 const std::string
&s
= this->*(b
->strMember
);
267 os
<< " " << b
->name
<< ": " << s
<< "\n";
272 Split(&hours
, &minutes
);
274 os
<< " Desc: " << GetDescription() << "\n";
275 os
<< " Index: 0x" <<setw(2) << Index
<< "\n";
276 os
<< " Type: 0x" <<setw(2) << (unsigned int)TZType
<< "\n";
277 os
<< " Offset: " << setbase(10) << UTCOffset
<< " minutes ("
278 << dec
<< (UTCOffset
/ 60.0) << ")\n";
279 os
<< " Split Offset: hours: "
280 << dec
<< hours
<< ", minutes: " << minutes
<< "\n";
281 os
<< " Sample TZ: " << GetTz("E") << "\n";
282 os
<< " Use DST: " << (UseDST
? "true" : "false") << "\n";
284 os
<< " DST Offset: " << setbase(10) << DSTOffset
<< "\n";
285 if ((StartMonth
> 0) && (StartMonth
< 11))
286 os
<< "Start Month: " << month
[StartMonth
] << "\n";
288 os
<< "Start Month: unknown (" << setbase(10) << StartMonth
<< ")\n";
289 if ((EndMonth
> 0) && (EndMonth
< 11))
290 os
<< " End Month: " << month
[EndMonth
] << "\n";
292 os
<< " End Month: unknown (" << setbase(10) << EndMonth
<< ")\n";
300 void TimeZone::Split(int *hours
, int *minutes
) const
302 *hours
= UTCOffset
/ 60;
303 *minutes
= UTCOffset
% 60;
305 *minutes
= -*minutes
;
308 void TimeZone::SplitAbsolute(bool *west
,
310 unsigned int *minutes
) const
312 int tmphours
, tmpminutes
;
313 Split(&tmphours
, &tmpminutes
);
317 tmphours
= -tmphours
;
324 *minutes
= tmpminutes
;
327 std::string
TimeZone::GetTz(const std::string
&prefix
) const
330 Split(&hours
, &minutes
);
332 // TZ variable uses positive for west, and negative for east,
333 // so swap the hour value. Minutes is always positive for
334 // our purposes, so leave it alone.
337 // standard time first
339 oss
<< prefix
<< "ST"
341 << setfill('0') << setw(2) << minutes
344 // daylight saving time next, if available
346 TimeZone
dst(UTCOffset
+ DSTOffset
);
348 dst
.Split(&hours
, &minutes
);
350 // swap again for TZ semantics...
353 oss
<< prefix
<< "DT"
355 << setfill('0') << setw(2) << minutes
358 // assume second Sunday of start month to first Sunday of
359 // end month, since we don't have enough data to be more
361 oss
<< ",M" << (StartMonth
+ 1) << ".2.0";
362 oss
<< ",M" << (EndMonth
+ 1) << ".1.0";