5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 date::date() : timestamp(-1) {}
27 int months
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0 };
30 timestamp
= ts
/ (24 * 60 * 60);
33 date::date(int day
, int month
, int year
) {
34 timestamp
= (year
- 1970) * 365;
35 for (; month
- 1 > 0; month
--)
36 timestamp
+= months
[month
- 1];
37 timestamp
+= day
+ (year
- 1970) / 4;
40 date::operator int() {
41 return timestamp
* 24 * 60 * 60;
44 int date::get_year() const {
45 return 1970 + (timestamp
+ timestamp
/ 365 / 4)/ 365;
48 int date::get_month() const {
50 if (get_year() % 4 == 0)
53 for (int i
= 0, ts
= (timestamp
- timestamp
/ 365 / 4) % 365; ts
>= months
[i
]; i
++) {
58 if (get_year() % 4 == 0)
64 int date::get_day() const {
66 if (get_year() % 4 == 0)
68 int ts
= (timestamp
- (timestamp
/ 365 + 2) / 4) % 365;
69 for (int i
= 0; ts
>= months
[i
]; i
++) {
74 if (get_year() % 4 == 0)
81 time::time() : timestamp(-1) {}
84 timestamp
= ts
% (24 * 60 * 60);
87 time::time(int hour
, int minute
, int second
) {
88 timestamp
= hour
* 3600 + minute
* 60 + second
;
91 time::operator int() {
95 int time::get_second() const {
96 return timestamp
% 60;
99 int time::get_minute() const {
100 return timestamp
/ 60 % 60;
103 int time::get_hour() const {
104 return timestamp
/ 3600;