- applied Time Zone parsing patches from Brian Edginton
[barry.git] / src / r_timezone.cc
blob8a4c377c5b7bfcbfce6073c6eb477faae7225986
1 ///
2 /// \file r_timezone.cc
3 /// Record parsing class for the timezone database.
4 ///
6 /*
7 Copyright (C) 2005-2008, 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"
26 #include "data.h"
27 #include "time.h"
28 #include "debug.h"
29 #include <ostream>
30 #include <iomanip>
32 using namespace std;
33 using namespace Barry::Protocol;
35 namespace Barry
38 ///////////////////////////////////////////////////////////////////////////////
39 // Timezone Class
41 // Timezone Field Codes
42 #define TZFC_INDEX 0x01
43 #define TZFC_NAME 0x02
44 #define TZFC_OFFSET 0x03
45 #define TZFC_DST 0x04
46 #define TZFC_STARTMONTH 0x06
47 #define TZFC_ENDMONTH 0x0B
48 #define TZFC_TZTYPE 0x64
50 #define TZFC_END 0xffff
52 FieldLink<Timezone> TimezoneFieldLinks[] = {
53 { TZFC_NAME, "Name", 0, 0, &Timezone::TimeZoneName, 0, 0 },
54 { TZFC_END, "End of List", 0, 0, 0, 0, 0 },
57 Timezone::Timezone()
59 Clear();
62 Timezone::~Timezone()
66 const unsigned char* Timezone::ParseField(const unsigned char *begin,
67 const unsigned char *end)
69 const CommonField *field = (const CommonField *) begin;
71 // advance and check size
72 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
73 if( begin > end ) // if begin==end, we are ok
74 return begin;
76 if( !btohs(field->size) ) // if field has no size, something's up
77 return begin;
79 if( field->type == TZFC_TZTYPE ) {
80 if( ( TZType = field->u.uint32 ) != 1 ) {
81 throw Error("Timezone::ParseField: Timezone Type is not valid");
83 return begin;
86 // cycle through the type table
87 for( FieldLink<Timezone> *b = TimezoneFieldLinks;
88 b->type != TZFC_END;
89 b++ )
91 if( b->type == field->type ) {
92 if( b->strMember ) {
93 std::string &s = this->*(b->strMember);
94 s = ParseFieldString(field);
95 return begin; // done!
100 switch( field->type )
102 case TZFC_INDEX:
103 Index = btohl(field->u.uint32);
104 return begin;
106 case TZFC_OFFSET:
107 Offset = btohs(field->u.int16);
108 if (Offset < 0) {
109 Offset =~ Offset;
110 Offset++;
111 OffsetFraction = Offset % 60;
112 Offset = Offset / 60;
113 Left = true;
114 } else {
115 OffsetFraction = Offset % 60;
116 Offset = Offset / 60;
117 Left = false;
119 return begin;
121 case TZFC_DST:
122 DSTOffset = btohl(field->u.uint32);
123 if (DSTOffset) {
124 UseDST = true;
126 return begin;
128 case TZFC_STARTMONTH:
129 StartMonth = btohl(field->u.uint32);
130 return begin;
132 case TZFC_ENDMONTH:
133 EndMonth = btohl(field->u.uint32);
134 return begin;
137 // if still not handled, add to the Unknowns list
138 UnknownField uf;
139 uf.type = field->type;
140 uf.data.assign((const char*)field->u.raw, btohs(field->size));
141 Unknowns.push_back(uf);
143 // return new pointer for next field
144 return begin;
147 void Timezone::ParseHeader(const Data &data, size_t &offset)
149 // no header in Task records
152 void Timezone::ParseFields(const Data &data, size_t &offset)
154 const unsigned char *finish = ParseCommonFields(*this,
155 data.GetData() + offset, data.GetData() + data.GetSize());
156 offset += finish - (data.GetData() + offset);
159 void Timezone::Clear()
161 TimeZoneName.clear();
163 Index = 0;
164 Left = false;
165 UseDST = false;
166 Offset = 0;
167 OffsetFraction = 0;
168 DSTOffset = 0;
169 StartMonth = -1;
170 EndMonth = -1;
172 Unknowns.clear();
175 void Timezone::Dump(std::ostream &os) const
177 static const char *month[] = {
178 "Jan", "Feb", "Mar", "Apr", "May",
179 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
182 os << "Task entry: 0x" << setbase(16) << RecordId
183 << " (" << (unsigned int)RecType << ")\n";
185 // cycle through the type table
186 for( const FieldLink<Timezone> *b = TimezoneFieldLinks;
187 b->type != TZFC_END;
188 b++ )
190 if( b->strMember ) {
191 const std::string &s = this->*(b->strMember);
192 if( s.size() )
193 os << " " << b->name << ": " << s << "\n";
197 os << " Index: 0x" <<setw(2) << Index << "\n";
198 os << " Offset: " << (Left ? "-" : "+") << setbase(10) << Offset << "." << OffsetFraction << "\n";
199 os << " Use DST: " << (UseDST ? "true" : "false") << "\n";
200 if (UseDST) {
201 if ((StartMonth > 0) && (StartMonth < 11))
202 os << "Start Month: " << month[StartMonth] << "\n";
203 else
204 os << "Start Month: unknown (" << setbase(10) << StartMonth << ")\n";
205 if ((EndMonth > 0) && (EndMonth < 11))
206 os << " End Month: " << month[EndMonth] << "\n";
207 else
208 os << " End Month: unknown (" << setbase(10) << EndMonth << ")\n";
211 os << Unknowns;
212 os << "\n\n";