timeconv.c 4.0 KB

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