timeconv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ([email protected]).
  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. *
  25. * 2009-7-14:
  26. * Moved from glibc-2.6 to kernel by Zhaolei<[email protected]>
  27. * 2021-06-02:
  28. * Reimplemented by Cassio Neri <[email protected]>
  29. */
  30. #include <linux/time.h>
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #define SECS_PER_HOUR (60 * 60)
  34. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  35. /**
  36. * time64_to_tm - converts the calendar time to local broken-down time
  37. *
  38. * @totalsecs: the number of seconds elapsed since 00:00:00 on January 1, 1970,
  39. * Coordinated Universal Time (UTC).
  40. * @offset: offset seconds adding to totalsecs.
  41. * @result: pointer to struct tm variable to receive broken-down time
  42. */
  43. void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
  44. {
  45. u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day;
  46. u64 u64tmp, udays, century, year;
  47. bool is_Jan_or_Feb, is_leap_year;
  48. long days, rem;
  49. int remainder;
  50. days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
  51. rem = remainder;
  52. rem += offset;
  53. while (rem < 0) {
  54. rem += SECS_PER_DAY;
  55. --days;
  56. }
  57. while (rem >= SECS_PER_DAY) {
  58. rem -= SECS_PER_DAY;
  59. ++days;
  60. }
  61. result->tm_hour = rem / SECS_PER_HOUR;
  62. rem %= SECS_PER_HOUR;
  63. result->tm_min = rem / 60;
  64. result->tm_sec = rem % 60;
  65. /* January 1, 1970 was a Thursday. */
  66. result->tm_wday = (4 + days) % 7;
  67. if (result->tm_wday < 0)
  68. result->tm_wday += 7;
  69. /*
  70. * The following algorithm is, basically, Proposition 6.3 of Neri
  71. * and Schneider [1]. In a few words: it works on the computational
  72. * (fictitious) calendar where the year starts in March, month = 2
  73. * (*), and finishes in February, month = 13. This calendar is
  74. * mathematically convenient because the day of the year does not
  75. * depend on whether the year is leap or not. For instance:
  76. *
  77. * March 1st 0-th day of the year;
  78. * ...
  79. * April 1st 31-st day of the year;
  80. * ...
  81. * January 1st 306-th day of the year; (Important!)
  82. * ...
  83. * February 28th 364-th day of the year;
  84. * February 29th 365-th day of the year (if it exists).
  85. *
  86. * After having worked out the date in the computational calendar
  87. * (using just arithmetics) it's easy to convert it to the
  88. * corresponding date in the Gregorian calendar.
  89. *
  90. * [1] "Euclidean Affine Functions and Applications to Calendar
  91. * Algorithms". https://arxiv.org/abs/2102.06959
  92. *
  93. * (*) The numbering of months follows tm more closely and thus,
  94. * is slightly different from [1].
  95. */
  96. udays = ((u64) days) + 2305843009213814918ULL;
  97. u64tmp = 4 * udays + 3;
  98. century = div64_u64_rem(u64tmp, 146097, &u64tmp);
  99. day_of_century = (u32) (u64tmp / 4);
  100. u32tmp = 4 * day_of_century + 3;
  101. u64tmp = 2939745ULL * u32tmp;
  102. year_of_century = upper_32_bits(u64tmp);
  103. day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
  104. year = 100 * century + year_of_century;
  105. is_leap_year = year_of_century ? !(year_of_century % 4) : !(century % 4);
  106. u32tmp = 2141 * day_of_year + 132377;
  107. month = u32tmp >> 16;
  108. day = ((u16) u32tmp) / 2141;
  109. /*
  110. * Recall that January 1st is the 306-th day of the year in the
  111. * computational (not Gregorian) calendar.
  112. */
  113. is_Jan_or_Feb = day_of_year >= 306;
  114. /* Convert to the Gregorian calendar and adjust to Unix time. */
  115. year = year + is_Jan_or_Feb - 6313183731940000ULL;
  116. month = is_Jan_or_Feb ? month - 12 : month;
  117. day = day + 1;
  118. day_of_year += is_Jan_or_Feb ? -306 : 31 + 28 + is_leap_year;
  119. /* Convert to tm's format. */
  120. result->tm_year = (long) (year - 1900);
  121. result->tm_mon = (int) month;
  122. result->tm_mday = (int) day;
  123. result->tm_yday = (int) day_of_year;
  124. }
  125. EXPORT_SYMBOL(time64_to_tm);