timegm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * aria2 - The high speed download utility
  3. *
  4. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * In addition, as a special exception, the copyright holders give
  21. * permission to link the code of portions of this program with the
  22. * OpenSSL library under certain conditions as described in each
  23. * individual source file, and distribute linked combinations
  24. * including the two.
  25. * You must obey the GNU General Public License in all respects
  26. * for all of the code used other than OpenSSL. If you modify
  27. * file(s) with this exception, you may extend this exception to your
  28. * version of the file(s), but you are not obligated to do so. If you
  29. * do not wish to do so, delete this exception statement from your
  30. * version. If you delete this exception statement from all source
  31. * files in the program, then also delete it here.
  32. */
  33. /* copyright --> */
  34. #include "timegm.h"
  35. #include <stdint.h>
  36. /* Counter the number of leap year in the range [0, y). The |y| is the
  37. year, including century (e.g., 2012) */
  38. static int count_leap_year(int y)
  39. {
  40. y -= 1;
  41. return y / 4 - y / 100 + y / 400;
  42. }
  43. /* Returns nonzero if the |y| is the leap year. The |y| is the year,
  44. including century (e.g., 2012) */
  45. static int is_leap_year(int y)
  46. {
  47. return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
  48. }
  49. /* The number of days before ith month begins */
  50. static int daysum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  51. // Based on the algorithm of Python 2.7 calendar.timegm.
  52. time_t timegm(struct tm* tm)
  53. {
  54. int days;
  55. int num_leap_year;
  56. int64_t t;
  57. if (tm->tm_mon > 11) {
  58. return -1;
  59. }
  60. num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
  61. days = (tm->tm_year - 70) * 365 + num_leap_year + daysum[tm->tm_mon] +
  62. tm->tm_mday - 1;
  63. if (tm->tm_mon >= 2 && is_leap_year(tm->tm_year + 1900)) {
  64. ++days;
  65. }
  66. t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
  67. if (sizeof(time_t) == 4) {
  68. if (t < INT32_MIN || t > INT32_MAX) {
  69. return -1;
  70. }
  71. }
  72. return t;
  73. }