Updated copyright dates for 2009
[barry.git] / src / r_timezone.cc
blob330376db3947b17085974403d8185f9065528308
1 ///
2 /// \file r_timezone.cc
3 /// Record parsing class for the timezone database.
4 ///
6 /*
7 Copyright (C) 2005-2009, 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 static 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,
68 const IConverter *ic)
70 const CommonField *field = (const CommonField *) begin;
72 // advance and check size
73 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
74 if( begin > end ) // if begin==end, we are ok
75 return begin;
77 if( !btohs(field->size) ) // if field has no size, something's up
78 return begin;
80 if( field->type == TZFC_TZTYPE ) {
81 if( ( TZType = field->u.uint32 ) != 1 ) {
82 throw Error("Timezone::ParseField: Timezone Type is not valid");
84 return begin;
87 // cycle through the type table
88 for( FieldLink<Timezone> *b = TimezoneFieldLinks;
89 b->type != TZFC_END;
90 b++ )
92 if( b->type == field->type ) {
93 if( b->strMember ) {
94 std::string &s = this->*(b->strMember);
95 s = ParseFieldString(field);
96 return begin; // done!
101 switch( field->type )
103 case TZFC_INDEX:
104 Index = btohl(field->u.uint32);
105 return begin;
107 case TZFC_OFFSET:
108 Offset = btohs(field->u.int16);
109 if (Offset < 0) {
110 Offset =~ Offset;
111 Offset++;
112 OffsetFraction = Offset % 60;
113 Offset = Offset / 60;
114 Left = true;
115 } else {
116 OffsetFraction = Offset % 60;
117 Offset = Offset / 60;
118 Left = false;
120 return begin;
122 case TZFC_DST:
123 DSTOffset = btohl(field->u.uint32);
124 if (DSTOffset) {
125 UseDST = true;
127 return begin;
129 case TZFC_STARTMONTH:
130 StartMonth = btohl(field->u.uint32);
131 return begin;
133 case TZFC_ENDMONTH:
134 EndMonth = btohl(field->u.uint32);
135 return begin;
138 // if still not handled, add to the Unknowns list
139 UnknownField uf;
140 uf.type = field->type;
141 uf.data.assign((const char*)field->u.raw, btohs(field->size));
142 Unknowns.push_back(uf);
144 // return new pointer for next field
145 return begin;
148 void Timezone::ParseHeader(const Data &data, size_t &offset)
150 // no header in Task records
153 void Timezone::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
155 const unsigned char *finish = ParseCommonFields(*this,
156 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
157 offset += finish - (data.GetData() + offset);
160 void Timezone::Clear()
162 TimeZoneName.clear();
164 Index = 0;
165 Left = false;
166 UseDST = false;
167 Offset = 0;
168 OffsetFraction = 0;
169 DSTOffset = 0;
170 StartMonth = -1;
171 EndMonth = -1;
173 Unknowns.clear();
176 void Timezone::Dump(std::ostream &os) const
178 static const char *month[] = {
179 "Jan", "Feb", "Mar", "Apr", "May",
180 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
183 os << "Task entry: 0x" << setbase(16) << RecordId
184 << " (" << (unsigned int)RecType << ")\n";
186 // cycle through the type table
187 for( const FieldLink<Timezone> *b = TimezoneFieldLinks;
188 b->type != TZFC_END;
189 b++ )
191 if( b->strMember ) {
192 const std::string &s = this->*(b->strMember);
193 if( s.size() )
194 os << " " << b->name << ": " << s << "\n";
198 os << " Index: 0x" <<setw(2) << Index << "\n";
199 os << " Offset: " << (Left ? "-" : "+") << setbase(10) << Offset << "." << OffsetFraction << "\n";
200 os << " Use DST: " << (UseDST ? "true" : "false") << "\n";
201 if (UseDST) {
202 if ((StartMonth > 0) && (StartMonth < 11))
203 os << "Start Month: " << month[StartMonth] << "\n";
204 else
205 os << "Start Month: unknown (" << setbase(10) << StartMonth << ")\n";
206 if ((EndMonth > 0) && (EndMonth < 11))
207 os << " End Month: " << month[EndMonth] << "\n";
208 else
209 os << " End Month: unknown (" << setbase(10) << EndMonth << ")\n";
212 os << Unknowns;
213 os << "\n\n";