timeconv.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  3. * This file is part of the GNU C Library.
  4. * Contributed by Paul Eggert (eggert@twinsun.com).
  5. *
  6. * The GNU C Library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * The GNU C Library 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 GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with the GNU C Library; see the file COPYING.LIB. If not,
  18. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /*
  22. * Converts the calendar time to broken-down time representation
  23. * Based on code from glibc-2.6
  24. *
  25. * 2009-7-14:
  26. * Moved from glibc-2.6 to kernel by Zhaolei<zhaolei@cn.fujitsu.com>
  27. */
  28. #include <linux/time.h>
  29. #include <linux/module.h>
  30. /*
  31. * Nonzero if YEAR is a leap year (every 4 years,
  32. * except every 100th isn't, and every 400th is).
  33. */
  34. static int __isleap(long year)
  35. {
  36. return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0);
  37. }
  38. /* do a mathdiv for long type */
  39. static long math_div(long a, long b)
  40. {
  41. return a / b - (a % b < 0);
  42. }
  43. /* How many leap years between y1 and y2, y1 must less or equal to y2 */
  44. static long leaps_between(long y1, long y2)
  45. {
  46. long leaps1 = math_div(y1 - 1, 4) - math_div(y1 - 1, 100)
  47. + math_div(y1 - 1, 400);
  48. long leaps2 = math_div(y2 - 1, 4) - math_div(y2 - 1, 100)
  49. + math_div(y2 - 1, 400);
  50. return leaps2 - leaps1;
  51. }
  52. /* How many days come before each month (0-12). */
  53. static const unsigned short __mon_yday[2][13] = {
  54. /* Normal years. */
  55. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
  56. /* Leap years. */
  57. {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
  58. };
  59. #define SECS_PER_HOUR (60 * 60)
  60. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  61. /**
  62. * time_to_tm - converts the calendar time to local broken-down time
  63. *
  64. * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
  65. * Coordinated Universal Time (UTC).
  66. * @offset offset seconds adding to totalsecs.
  67. * @result pointer to struct tm variable to receive broken-down time
  68. */
  69. void time_to_tm(time_t totalsecs, int offset, struct tm *result)
  70. {
  71. long days, rem, y;
  72. const unsigned short *ip;
  73. days = totalsecs / SECS_PER_DAY;
  74. rem = totalsecs % SECS_PER_DAY;
  75. rem += offset;
  76. while (rem < 0) {
  77. rem += SECS_PER_DAY;
  78. --days;
  79. }
  80. while (rem >= SECS_PER_DAY) {
  81. rem -= SECS_PER_DAY;
  82. ++days;
  83. }
  84. result->tm_hour = rem / SECS_PER_HOUR;
  85. rem %= SECS_PER_HOUR;
  86. result->tm_min = rem / 60;
  87. result->tm_sec = rem % 60;
  88. /* January 1, 1970 was a Thursday. */
  89. result->tm_wday = (4 + days) % 7;
  90. if (result->tm_wday < 0)
  91. result->tm_wday += 7;
  92. y = 1970;
  93. while (days < 0 || days >= (__isleap(y) ? 366 : 365)) {
  94. /* Guess a corrected year, assuming 365 days per year. */
  95. long yg = y + math_div(days, 365);
  96. /* Adjust DAYS and Y to match the guessed year. */
  97. days -= (yg - y) * 365 + leaps_between(y, yg);
  98. y = yg;
  99. }
  100. result->tm_year = y - 1900;
  101. result->tm_yday = days;
  102. ip = __mon_yday[__isleap(y)];
  103. for (y = 11; days < ip[y]; y--)
  104. continue;
  105. days -= ip[y];
  106. result->tm_mon = y;
  107. result->tm_mday = days + 1;
  108. }
  109. EXPORT_SYMBOL(time_to_tm);