3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #define HOUR (60 * MINUTE)
24 #define DAY (24 * HOUR)
25 #define YEAR (365 * DAY)
27 static int is_leap_year (int year
)
30 if (year
% 100 == 0) {
42 static int count_leap_years (int epoch
, int year
)
45 for (i
= epoch
; i
< year
; i
++)
52 static int get_day (int year
, int mon
, int day
)
74 break; /* 5: 120+31 */
77 break; /* 6: 151+30 */
80 break; /* 7: 181+31 */
83 break; /* 8: 212+31 */
86 break; /* 9: 243+30 */
89 break; /* 10: 273+31 */
92 break; /* 11: 304+30 */
97 if (is_leap_year (year
) && mon
> 2)
105 time_t mktime (struct tm
*tm
)
110 unsigned long result
= 0;
113 result
+= tm
->tm_min
* MINUTE
;
114 result
+= tm
->tm_hour
* HOUR
;
115 result
+= get_day (tm
->tm_year
, tm
->tm_mon
- 1, tm
->tm_mday
) * DAY
;
116 result
+= (tm
->tm_year
- EPOCH_YEAR
) * YEAR
;
117 result
+= count_leap_years (EPOCH_YEAR
, tm
->tm_year
) * DAY
;