Bumped copyright dates for 2013
[barry.git] / src / r_timezone.h
blobf3986d16ebcf9ecad232d667e4134842b3d8f0bc
1 ///
2 /// \file r_timezone.h
3 /// Record parsing class for the timezone database.
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 #ifndef __BARRY_RECORD_TIMEZONE_H__
24 #define __BARRY_RECORD_TIMEZONE_H__
26 #include "dll.h"
27 #include "record.h"
28 #include <vector>
29 #include <string>
30 #include <stdint.h>
32 namespace Barry {
34 // forward declarations
35 class IConverter;
37 class BXEXPORT TimeZone
39 public:
40 typedef Barry::UnknownsType UnknownsType;
42 uint8_t RecType;
43 uint32_t RecordId;
45 std::string Name; //< name of time zone
47 int32_t Index; //< index of entry in time zone table...
48 //< matches Code in hard coded TimeZone
49 //< table in Barry
51 int32_t UTCOffset; //< Timezone offset from UTC in minutes.
52 //< Will be a negative value for west
53 //< of UTC (North America, etc), and
54 //< a positive value for east (Europe).
55 //< i.e. -210 for St. John's, which is
56 //< -3.5 hours from UTC.
58 bool UseDST; //< true this timezone uses DST
59 uint32_t DSTOffset; //< minutes of DST, if UseDST is true.
60 //< This value will almost always be 60.
61 uint32_t StartMonth; //< index, 0-11, of month to start DST
62 uint32_t EndMonth; //< index, 0-11, of month to end DST
64 uint8_t TZType; //< unknown
66 UnknownsType Unknowns;
68 public:
69 TimeZone();
71 /// Creates a new timezone based on utc_offset minutes.
72 /// Use same semantics as UTCOffset. For example, a -3.5 hour
73 /// timezone (which is west of UTC) would be constructed
74 /// as: TimeZone(-210)
75 explicit TimeZone(int utc_offset);
77 /// Creates a new timezone based on negative/positive hours,
78 /// and positive minutes. For example, a -3.5 hour timezone
79 /// (which is west of UTC) would be constructed as: TimeZone(-3, 30)
80 /// Note that minutes can be negative, and it will be handled
81 /// correctly. i.e. TimeZone(-3, 30) == TimeZone(-3, -30)
82 TimeZone(int hours, int minutes);
84 virtual ~TimeZone();
87 // TimeZone related utility functions
90 bool IsWest() const { return UTCOffset < 0; }
92 /// Splits UTCOffset minutes into hours and minutes. hours can be
93 /// negative. minutes is always positive.
94 void Split(int *hours, int *minutes) const;
96 /// Splits UTCOffset minutes into absolute values of hours and minutes,
97 /// and sets the west flag appropriately. This is to mimic the
98 /// old behaviour of the Left, Offset and OffsetFraction member
99 /// variables.
100 void SplitAbsolute(bool *west,
101 unsigned int *hours, unsigned int *minutes) const;
103 /// Creates a timezone string suitable for a Unix / POSIX TZ
104 /// environment variable. Expects a time zone prefix.
105 /// For example, New Zealand Standard/Daylight Time is NZST/NZDT, so
106 /// the prefix would be "NZ". Eastern Standard/Daylight Time
107 /// is EST/EDT, so the prefix would be "E".
109 /// Should be able to use this string to achieve time zone conversions
110 /// using the TzWrapper class.
111 std::string GetTz(const std::string &prefix) const;
114 // common Barry record functions
115 void Validate() const;
116 const unsigned char* ParseField(const unsigned char *begin,
117 const unsigned char *end, const IConverter *ic = 0);
118 void ParseRecurrenceData(const void *data);
119 void BuildRecurrenceData(void *data);
120 uint8_t GetRecType() const { return RecType; }
121 uint32_t GetUniqueId() const { return RecordId; }
122 void SetIds(uint8_t Type, uint32_t Id) { RecType = Type; RecordId = Id; }
123 void ParseHeader(const Data &data, size_t &offset);
124 void ParseFields(const Data &data, size_t &offset, const IConverter *ic = 0);
125 void BuildHeader(Data &data, size_t &offset) const;
126 void BuildFields(Data &data, size_t &offset, const IConverter *ic = 0) const;
128 // operations (common among record classes)
129 void Clear();
130 void Dump(std::ostream &os) const;
131 std::string GetDescription() const;
133 bool operator<(const TimeZone &other) const { return SortByName(*this, other); }
135 // sort options - suitable for use in std::sort()
136 static bool SortByName(const TimeZone &a, const TimeZone &b)
138 return a.Name < b.Name;
140 static bool SortByZone(const TimeZone &a, const TimeZone &b)
142 return a.UTCOffset < b.UTCOffset;
145 // database name
146 static const char * GetDBName() { return "Time Zones"; }
147 static uint8_t GetDefaultRecType() { return 2; }
149 // Generic Field Handle support
150 static const FieldHandle<TimeZone>::ListT& GetFieldHandles();
153 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const TimeZone &msg) {
154 msg.Dump(os);
155 return os;
159 // forward declarations
160 namespace Mode {
161 class Desktop;
165 // TimeZones
167 /// Creates a vector of TimeZone objects either based on the library's
168 /// hard coded StaticTimeZone list, or by extracting the time zone database
169 /// from a given device.
171 /// After construction, the vector will be sorted according to time zone,
172 /// with west-most first.
174 class BXEXPORT TimeZones
176 public:
177 typedef std::vector<TimeZone> ListType;
178 typedef ListType::iterator iterator;
179 typedef ListType::const_iterator const_iterator;
181 private:
182 ListType m_list;
184 public:
185 /// Creates the list based on the library's hard coded StaticTimeZone
186 /// list.
187 TimeZones();
189 /// Extracts the time zone database from the given device.
190 /// Throws an exception if the device does not have a time zones
191 /// database.
192 explicit TimeZones(Barry::Mode::Desktop &desktop);
194 /// Static helper function that returns true if the device
195 /// referenced by desktop has a time zone database
196 static bool IsLoadable(Barry::Mode::Desktop &desktop);
198 ListType& GetList() { return m_list; }
199 const ListType& GetList() const { return m_list; }
201 iterator begin() { return m_list.begin(); }
202 const_iterator begin() const { return m_list.begin(); }
204 iterator end() { return m_list.end(); }
205 const_iterator end() const { return m_list.end(); }
207 TimeZone& operator[](int i) { return m_list[i]; }
208 const TimeZone& operator[](int i) const { return m_list[i]; }
210 // utility functions - return end() if not found
211 iterator Find(int index);
212 const_iterator Find(int index) const;
214 iterator FindByOffset(int utc_offset);
215 const_iterator FindByOffset(int utc_offset) const;
217 void Dump(std::ostream &os) const;
220 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const TimeZones &l)
222 l.Dump(os);
223 return os;
226 } // namespace Barry
228 #endif /* __BARRY_RECORD_TIMEZONE_H__*/