1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "timegm.h"
26
27 #include <inttypes.h>
28
29 /* Counter the number of leap year in the range [0, y). The |y| is the
30 year, including century (e.g., 2012) */
count_leap_year(int y)31 static int count_leap_year(int y) {
32 y -= 1;
33 return y / 4 - y / 100 + y / 400;
34 }
35
36 /* Based on the algorithm of Python 2.7 calendar.timegm. */
nghttp2_timegm(struct tm * tm)37 time_t nghttp2_timegm(struct tm *tm) {
38 int days;
39 int num_leap_year;
40 int64_t t;
41 if (tm->tm_mon > 11) {
42 return -1;
43 }
44 num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
45 days = (tm->tm_year - 70) * 365 + num_leap_year + tm->tm_yday;
46 t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
47
48 #if SIZEOF_TIME_T == 4
49 if (t < INT32_MIN || t > INT32_MAX) {
50 return -1;
51 }
52 #endif /* SIZEOF_TIME_T == 4 */
53
54 return (time_t)t;
55 }
56
57 /* Returns nonzero if the |y| is the leap year. The |y| is the year,
58 including century (e.g., 2012) */
is_leap_year(int y)59 static int is_leap_year(int y) {
60 return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
61 }
62
63 /* The number of days before ith month begins */
64 static int daysum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
65
nghttp2_timegm_without_yday(struct tm * tm)66 time_t nghttp2_timegm_without_yday(struct tm *tm) {
67 int days;
68 int num_leap_year;
69 int64_t t;
70 if (tm->tm_mon > 11) {
71 return -1;
72 }
73 num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
74 days = (tm->tm_year - 70) * 365 + num_leap_year + daysum[tm->tm_mon] +
75 tm->tm_mday - 1;
76 if (tm->tm_mon >= 2 && is_leap_year(tm->tm_year + 1900)) {
77 ++days;
78 }
79 t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
80
81 #if SIZEOF_TIME_T == 4
82 if (t < INT32_MIN || t > INT32_MAX) {
83 return -1;
84 }
85 #endif /* SIZEOF_TIME_T == 4 */
86
87 return (time_t)t;
88 }
89