aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/utility/datetimeUtils.cpp
blob: b1a6c55201fcee28c76aca580a77f9c0e81ca078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/utility/datetimeUtils.hpp"

#include <stdexcept>


namespace vmime {
namespace utility {


#ifndef VMIME_BUILDING_DOC

static inline void nextMonth(datetime& d) {

	if (d.getMonth() >= 12) {
		d.setMonth(1);
		d.setYear(d.getYear() + 1);
	} else {
		d.setMonth(d.getMonth() + 1);
	}
}


static inline void prevMonth(datetime& d) {

	if (d.getMonth() <= 1) {
		d.setYear(d.getYear() - 1);
		d.setMonth(12);
	} else {
		d.setMonth(d.getMonth() - 1);
	}
}


static inline void nextDay(datetime& d) {

	if (d.getDay() >= datetimeUtils::getDaysInMonth(d.getYear(), d.getMonth())) {
		d.setDay(1);
		nextMonth(d);
	} else {
		d.setDay(d.getDay() + 1);
	}
}


static inline void prevDay(datetime& d) {

	if (d.getDay() <= 1) {
		prevMonth(d);
		d.setDay(datetimeUtils::getDaysInMonth(d.getYear(), d.getMonth()));
	} else {
		d.setDay(d.getDay() - 1);
	}
}


static inline void nextHour(datetime& d) {

	if (d.getHour() >= 23) {
		d.setHour(0);
		nextDay(d);
	} else {
		d.setHour(d.getHour() + 1);
	}
}


static inline void prevHour(datetime& d) {

	if (d.getHour() <= 0) {
		d.setHour(23);
		prevDay(d);
	} else {
		d.setHour(d.getHour() - 1);
	}
}


static inline void addHoursAndMinutes(datetime& d, const int h, const int m) {

	d.setMinute(d.getMinute() + m);

	if (d.getMinute() >= 60) {
		d.setMinute(d.getMinute() - 60);
		nextHour(d);
	}

	d.setHour(d.getHour() + h);

	if (d.getHour() >= 24) {
		d.setHour(d.getHour() - 24);
		nextDay(d);
	}
}


static inline void substractHoursAndMinutes(datetime& d, const int h, const int m) {

	if (m > d.getMinute()) {
		d.setMinute(60 - (m - d.getMinute()));
		prevHour(d);
	} else {
		d.setMinute(d.getMinute() - m);
	}

	if (h > d.getHour()) {
		d.setHour(24 - (h - d.getHour()));
		prevDay(d);
	} else {
		d.setHour(d.getHour() - h);
	}
}

#endif // VMIME_BUILDING_DOC


const datetime datetimeUtils::toUniversalTime(const datetime& date) {

	if (date.getZone() == datetime::GMT) {
		return date; // no conversion needed
	}

	datetime nd(date);
	nd.setZone(datetime::GMT);

	const int z = date.getZone();
	const int h = (z < 0) ? (-z / 60) : (z / 60);
	const int m = (z < 0) ? (-z - h * 60) : (z - h * 60);

	if (z < 0) {  // GMT-hhmm: add hours and minutes to date
		addHoursAndMinutes(nd, h, m);
	} else {      // GMT+hhmm: substract hours and minutes from date
		substractHoursAndMinutes(nd, h, m);
	}

	return nd;
}


const datetime datetimeUtils::toLocalTime(const datetime& date, const int zone) {

	datetime utcDate(date);

	if (utcDate.getZone() != datetime::GMT) {
		utcDate = toUniversalTime(date); // convert to UT before
	}

	datetime nd(utcDate);
	nd.setZone(zone);

	const int h = (zone < 0) ? (-zone / 60) : (zone / 60);
	const int m = (zone < 0) ? (-zone - h * 60) : (zone - h * 60);

	if (zone < 0) {  // GMT+hhmm: substract hours and minutes from date
		substractHoursAndMinutes(nd, h, m);
	} else {         // GMT-hhmm: add hours and minutes to date
		addHoursAndMinutes(nd, h, m);
	}

	return nd;
}


bool datetimeUtils::isLeapYear(const int year) {

	// From RFC 3339 - Appendix C. Leap Years:
	return ((year % 4) == 0 && (year % 100 != 0 || year % 400 == 0));
}


int datetimeUtils::getDaysInMonth(const int year, const int month) {

	static const int daysInMonth[12] = {
		31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	};
	static const int daysInMonthLeapYear[12] = {
		31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	};

	if (month < 1 || month > 12) {
		throw std::out_of_range("Invalid month number");
	}

	return isLeapYear(year) ? daysInMonthLeapYear[month - 1] : daysInMonth[month - 1];
}


int datetimeUtils::getDayOfWeek(const int year, const int month, const int day) {

	int y = year;
	int m = month;

	if (month < 1 || month > 12) {
		throw std::out_of_range("Invalid month number");
	} else if (day < 1 || day > getDaysInMonth(year, month)) {
		throw std::out_of_range("Invalid day number");
	}

	// From RFC-3339 - Appendix B. Day of the Week

	// Adjust months so February is the last one
	m -= 2;

	if (m < 1) {
		m += 12;
		--y;
	}

	// Split by century
	const int cent = y / 100;
	y %= 100;

	return ((26 * m - 2) / 10 + day + y + (y >> 2) + (cent >> 2) + 5 * cent) % 7;
}


int datetimeUtils::getWeekOfYear(const int year, const int month, const int day, const bool iso) {

	// Algorithm from http://personal.ecu.edu/mccartyr/ISOwdALG.txt

	const bool leapYear = ((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0;
	const bool leapYear_1 = (((year - 1) % 4) == 0 && ((year - 1) % 100) != 0) || ((year - 1) % 400) == 0;

	// 4. Find the DayOfYearNumber for Y M D
	static const int DAY_OF_YEAR_NUMBER_MAP[12] = {
		0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
	};

	int DayOfYearNumber = day + DAY_OF_YEAR_NUMBER_MAP[month - 1];

	if (leapYear && month > 2) {
		DayOfYearNumber += 1;
	}

	// 5. Find the Jan1Weekday for Y (Monday=1, Sunday=7)
	const int YY = (year - 1) % 100;
	const int C = (year - 1) - YY;
	const int G = YY + YY / 4;
	const int Jan1Weekday = 1 + (((((C / 100) % 4) * 5) + G) % 7);

	// 6. Find the Weekday for Y M D
	const int H = DayOfYearNumber + (Jan1Weekday - 1);
	const int Weekday = 1 + ((H - 1) % 7);

	// 7. Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53
	int YearNumber = 0, WeekNumber = 0;

	if (DayOfYearNumber <= (8 - Jan1Weekday) && Jan1Weekday > 4) {

		YearNumber = year - 1;

		if (Jan1Weekday == 5 || (Jan1Weekday == 6 && leapYear_1)) {
			WeekNumber = 53;
		} else {
			WeekNumber = 52;
		}

	} else {

		YearNumber = year;
	}

	// 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1
	if (YearNumber == year) {

		const int I = (leapYear ? 366 : 365);

		if ((I - DayOfYearNumber) < (4 - Weekday)) {
			YearNumber = year + 1;
			WeekNumber = 1;
		}
	}

	// 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
	if (YearNumber == year) {

		const int J = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday - 1);

		WeekNumber = J / 7;

		if (Jan1Weekday > 4) {
			WeekNumber -= 1;
		}
	}

	if (!iso && (WeekNumber == 1 && month == 12)) {
		WeekNumber = 53;
	}

	return WeekNumber;
}


} // utility
} // vmime