lib: added safety check in Timezone ctor for negative minutes
[barry.git] / src / r_timezone.h
blobcab1aa579f3d5b58b74730d1cd1937a9055e3e75
1 ///
2 /// \file r_timezone.h
3 /// Record parsing class for the timezone database.
4 ///
6 /*
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 #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 Timezone(int hours, int minutes);
82 virtual ~Timezone();
85 // Timezone related utility functions
88 bool IsWest() const { return UTCOffset < 0; }
90 /// Splits UTCOffset minutes into hours and minutes. hours can be
91 /// negative. minutes is always positive.
92 void Split(int *hours, int *minutes) const;
94 /// Splits UTCOffset minutes into absolute values of hours and minutes,
95 /// and sets the west flag appropriately. This is to mimic the
96 /// old behaviour of the Left, Offset and OffsetFraction member
97 /// variables.
98 void SplitAbsolute(bool *west,
99 unsigned int *hours, unsigned int *minutes) const;
101 /// Creates a timezone string suitable for a Unix / POSIX TZ
102 /// environment variable. Expects a time zone prefix.
103 /// For example, New Zealand Standard/Daylight Time is NZST/NZDT, so
104 /// the prefix would be "NZ". Eastern Standard/Daylight Time
105 /// is EST/EDT, so the prefix would be "E".
107 /// Should be able to use this string to achieve time zone conversions
108 /// using the TzWrapper class.
109 std::string GetTz(const std::string &prefix) const;
112 // common Barry record functions
113 const unsigned char* ParseField(const unsigned char *begin,
114 const unsigned char *end, const IConverter *ic = 0);
115 void ParseRecurrenceData(const void *data);
116 void BuildRecurrenceData(void *data);
117 uint8_t GetRecType() const { return RecType; }
118 uint32_t GetUniqueId() const { return RecordId; }
119 void SetIds(uint8_t Type, uint32_t Id) { RecType = Type; RecordId = Id; }
120 void ParseHeader(const Data &data, size_t &offset);
121 void ParseFields(const Data &data, size_t &offset, const IConverter *ic = 0);
122 void BuildHeader(Data &data, size_t &offset) const;
123 void BuildFields(Data &data, size_t &offset, const IConverter *ic = 0) const;
125 // operations (common among record classes)
126 void Clear();
127 void Dump(std::ostream &os) const;
128 std::string GetDescription() const;
130 bool operator<(const Timezone &other) const { return Name < other.Name; }
132 // database name
133 static const char * GetDBName() { return "Time Zones"; }
134 static uint8_t GetDefaultRecType() { return 2; }
136 // Generic Field Handle support
137 static const std::vector<FieldHandle<Timezone> >& GetFieldHandles();
140 BXEXPORT inline std::ostream& operator<<(std::ostream &os, const Timezone &msg) {
141 msg.Dump(os);
142 return os;
144 } // namespace Barry
146 #endif /* __BARRY_RECORD_TIMEZONE_H__*/