1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library.  Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23 
24 #ifndef VMIME_DATETIME_HPP_INCLUDED
25 #define VMIME_DATETIME_HPP_INCLUDED
26 
27 
28 #include "vmime/base.hpp"
29 #include "vmime/headerFieldValue.hpp"
30 
31 #include <ctime>
32 
33 
34 namespace vmime
35 {
36 
37 
38 /** Date and time (basic type).
39   */
40 
41 class VMIME_EXPORT datetime : public headerFieldValue
42 {
43 public:
44 
45 	// Constructors
46 	datetime();
47 	datetime(const int year, const int month, const int day);
48 	datetime(const int year, const int month, const int day, const int hour, const int minute, const int second, const int zone = GMT);
49 	datetime(const datetime& d);
50 	datetime(const string& date);
51 	datetime(const time_t t, const int zone = GMT);
52 
53 	// Destructor
54 	~datetime();
55 
56 	// Some time zones (in minutes)
57 	enum TimeZones
58 	{
59 		GMT_12 = -720,   // GMT-12h
60 		GMT_11 = -660,   // GMT-11h
61 		GMT_10 = -600,   // GMT-10h
62 		GMT_9 = -540,    // GMT-9h
63 		GMT_8 = -480,    // GMT-8h
64 		GMT_7 = -420,    // GMT-7h
65 		GMT_6 = -360,    // GMT-6h
66 		GMT_5 = -300,    // GMT-5h
67 		GMT_4 = -240,    // GMT-4h
68 		GMT_3 = -180,    // GMT-3h
69 		GMT_2 = -120,    // GMT-2h
70 		GMT_1 = -60,     // GMT-1h
71 		GMT = 0,         // GMT
72 		GMT1 = 60,       // GMT+1h
73 		GMT2 = 120,      // GMT+2h
74 		GMT3 = 180,      // GMT+3h
75 		GMT4 = 240,      // GMT+4h
76 		GMT5 = 300,      // GMT+5h
77 		GMT6 = 360,      // GMT+6h
78 		GMT7 = 420,      // GMT+7h
79 		GMT8 = 480,      // GMT+8h
80 		GMT9 = 540,      // GMT+9h
81 		GMT10 = 600,     // GMT+10h
82 		GMT11 = 660,     // GMT+11h
83 		GMT12 = 720,     // GMT+12h
84 
85 		UT = GMT,        // Universal Time
86 
87 		EST = GMT_5,     // Eastern
88 		EDT = GMT_4,
89 		CST = GMT_6,     // Central
90 		CDT = GMT_5,
91 		MST = GMT_7,     // Mountain
92 		MDT = GMT_6,
93 		PST = GMT_8,     // Pacific
94 		PDT = GMT_7,
95 
96 		// Military time zones
97 		A = GMT_1,
98 		B = GMT_2,
99 		C = GMT_3,
100 		D = GMT_4,
101 		E = GMT_5,
102 		F = GMT_6,
103 		G = GMT_7,
104 		H = GMT_8,
105 		I = GMT_9,       // J not used
106 		K = GMT_10,
107 		L = GMT_11,
108 		M = GMT_12,
109 
110 		N = GMT1,
111 		O = GMT2,
112 		P = GMT3,
113 		Q = GMT4,
114 		R = GMT5,
115 		S = GMT6,
116 		T = GMT7,
117 		U = GMT8,
118 		V = GMT9,
119 		W = GMT10,
120 		X = GMT11,
121 		Y = GMT12,
122 
123 		Z = GMT
124 	};
125 
126 	// Months list
127 	enum Months
128 	{
129 		// Long
130 		JANUARY = 1,
131 		FEBRUARY = 2,
132 		MARCH = 3,
133 		APRIL = 4,
134 		MAY = 5,
135 		JUNE = 6,
136 		JULY = 7,
137 		AUGUST = 8,
138 		SEPTEMBER = 9,
139 		OCTOBER = 10,
140 		NOVEMBER = 11,
141 		DECEMBER = 12,
142 
143 		// Short
144 		JAN = 1,
145 		FEB = 2,
146 		MAR = 3,
147 		APR = 4,
148 		JUN = 6,
149 		JUL = 7,
150 		AUG = 8,
151 		SEP = 9,
152 		OCT = 10,
153 		NOV = 11,
154 		DEC = 12
155 	};
156 
157 	// Days of week list
158 	enum DaysOfWeek
159 	{
160 		// Long
161 		SUNDAY = 0,
162 		MONDAY = 1,
163 		TUESDAY = 2,
164 		WEDNESDAY = 3,
165 		THURSDAY = 4,
166 		FRIDAY = 5,
167 		SATURDAY = 6,
168 
169 		// Short
170 		SUN = 0,
171 		MON = 1,
172 		TUE = 2,
173 		WED = 3,
174 		THU = 4,
175 		FRI = 5,
176 		SAT = 6
177 	};
178 
179 private:
180 
181 	// Date components
182 	int m_year;
183 	int m_month;
184 	int m_day;
185 
186 	// Time components
187 	int m_hour;
188 	int m_minute;
189 	int m_second;
190 	int m_zone;
191 
192 public:
193 
194 	// Get
195 	int getYear() const;
196 	int getMonth() const;
197 	int getDay() const;
198 	int getHour() const;
199 	int getMinute() const;
200 	int getSecond() const;
201 	int getZone() const;
202 	int getWeekDay() const;
203 	int getWeek() const;
204 
205 	void getTime(int& hour, int& minute, int& second, int& zone) const;
206 	void getTime(int& hour, int& minute, int& second) const;
207 	void getDate(int& year, int& month, int& day) const;
208 
209 	// Set
210 	void setYear(const int year);
211 	void setMonth(const int month);
212 	void setDay(const int day);
213 	void setHour(const int hour);
214 	void setMinute(const int minute);
215 	void setSecond(const int second);
216 	void setZone(const int zone);
217 
218 	void setTime(const int hour = 0, const int minute = 0, const int second = 0, const int zone = GMT);
219 	void setDate(const int year, const int month, const int day);
220 
221 	// Assignment
222 	datetime& operator=(const datetime& other);
223 	datetime& operator=(const string& s);
224 
225 	void copyFrom(const component& other);
226 
227 	shared_ptr <component> clone() const;
228 
229 	// Comparison
230 	bool operator==(const datetime& other) const;
231 	bool operator!=(const datetime& other) const;
232 	bool operator<(const datetime& other) const;
233 	bool operator<=(const datetime& other) const;
234 	bool operator>(const datetime& other) const;
235 	bool operator>=(const datetime& other) const;
236 
237 	// Current date and time
238 	static const datetime now();
239 
240 	const std::vector <shared_ptr <component> > getChildComponents();
241 
242 protected:
243 
244 	// Component parsing & assembling
245 	void parseImpl
246 		(const parsingContext& ctx,
247 		 const string& buffer,
248 		 const size_t position,
249 		 const size_t end,
250 		 size_t* newPosition = NULL);
251 
252 	void generateImpl
253 		(const generationContext& ctx,
254 		 utility::outputStream& os,
255 		 const size_t curLinePos = 0,
256 		 size_t* newLinePos = NULL) const;
257 };
258 
259 
260 } // vmime
261 
262 
263 #endif // VMIME_DATETIME_HPP_INCLUDED
264