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