12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2013 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#include "timegm.h"
262c593315Sopenharmony_ci
272c593315Sopenharmony_ci#include <inttypes.h>
282c593315Sopenharmony_ci
292c593315Sopenharmony_ci/* Counter the number of leap year in the range [0, y). The |y| is the
302c593315Sopenharmony_ci   year, including century (e.g., 2012) */
312c593315Sopenharmony_cistatic int count_leap_year(int y) {
322c593315Sopenharmony_ci  y -= 1;
332c593315Sopenharmony_ci  return y / 4 - y / 100 + y / 400;
342c593315Sopenharmony_ci}
352c593315Sopenharmony_ci
362c593315Sopenharmony_ci/* Based on the algorithm of Python 2.7 calendar.timegm. */
372c593315Sopenharmony_citime_t nghttp2_timegm(struct tm *tm) {
382c593315Sopenharmony_ci  int days;
392c593315Sopenharmony_ci  int num_leap_year;
402c593315Sopenharmony_ci  int64_t t;
412c593315Sopenharmony_ci  if (tm->tm_mon > 11) {
422c593315Sopenharmony_ci    return -1;
432c593315Sopenharmony_ci  }
442c593315Sopenharmony_ci  num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
452c593315Sopenharmony_ci  days = (tm->tm_year - 70) * 365 + num_leap_year + tm->tm_yday;
462c593315Sopenharmony_ci  t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
472c593315Sopenharmony_ci
482c593315Sopenharmony_ci#if SIZEOF_TIME_T == 4
492c593315Sopenharmony_ci  if (t < INT32_MIN || t > INT32_MAX) {
502c593315Sopenharmony_ci    return -1;
512c593315Sopenharmony_ci  }
522c593315Sopenharmony_ci#endif /* SIZEOF_TIME_T == 4 */
532c593315Sopenharmony_ci
542c593315Sopenharmony_ci  return (time_t)t;
552c593315Sopenharmony_ci}
562c593315Sopenharmony_ci
572c593315Sopenharmony_ci/* Returns nonzero if the |y| is the leap year. The |y| is the year,
582c593315Sopenharmony_ci   including century (e.g., 2012) */
592c593315Sopenharmony_cistatic int is_leap_year(int y) {
602c593315Sopenharmony_ci  return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
612c593315Sopenharmony_ci}
622c593315Sopenharmony_ci
632c593315Sopenharmony_ci/* The number of days before ith month begins */
642c593315Sopenharmony_cistatic int daysum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
652c593315Sopenharmony_ci
662c593315Sopenharmony_citime_t nghttp2_timegm_without_yday(struct tm *tm) {
672c593315Sopenharmony_ci  int days;
682c593315Sopenharmony_ci  int num_leap_year;
692c593315Sopenharmony_ci  int64_t t;
702c593315Sopenharmony_ci  if (tm->tm_mon > 11) {
712c593315Sopenharmony_ci    return -1;
722c593315Sopenharmony_ci  }
732c593315Sopenharmony_ci  num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
742c593315Sopenharmony_ci  days = (tm->tm_year - 70) * 365 + num_leap_year + daysum[tm->tm_mon] +
752c593315Sopenharmony_ci         tm->tm_mday - 1;
762c593315Sopenharmony_ci  if (tm->tm_mon >= 2 && is_leap_year(tm->tm_year + 1900)) {
772c593315Sopenharmony_ci    ++days;
782c593315Sopenharmony_ci  }
792c593315Sopenharmony_ci  t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
802c593315Sopenharmony_ci
812c593315Sopenharmony_ci#if SIZEOF_TIME_T == 4
822c593315Sopenharmony_ci  if (t < INT32_MIN || t > INT32_MAX) {
832c593315Sopenharmony_ci    return -1;
842c593315Sopenharmony_ci  }
852c593315Sopenharmony_ci#endif /* SIZEOF_TIME_T == 4 */
862c593315Sopenharmony_ci
872c593315Sopenharmony_ci  return (time_t)t;
882c593315Sopenharmony_ci}
89