xref: /openbmc/linux/kernel/time/timeconv.c (revision 27601055)
135728b82SThomas Gleixner // SPDX-License-Identifier: LGPL-2.0+
257f1f087SZhaolei /*
357f1f087SZhaolei  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
457f1f087SZhaolei  * This file is part of the GNU C Library.
557f1f087SZhaolei  * Contributed by Paul Eggert (eggert@twinsun.com).
657f1f087SZhaolei  *
757f1f087SZhaolei  * The GNU C Library is free software; you can redistribute it and/or
857f1f087SZhaolei  * modify it under the terms of the GNU Library General Public License as
957f1f087SZhaolei  * published by the Free Software Foundation; either version 2 of the
1057f1f087SZhaolei  * License, or (at your option) any later version.
1157f1f087SZhaolei  *
1257f1f087SZhaolei  * The GNU C Library is distributed in the hope that it will be useful,
1357f1f087SZhaolei  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1457f1f087SZhaolei  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1557f1f087SZhaolei  * Library General Public License for more details.
1657f1f087SZhaolei  *
1757f1f087SZhaolei  * You should have received a copy of the GNU Library General Public
1857f1f087SZhaolei  * License along with the GNU C Library; see the file COPYING.LIB.  If not,
1957f1f087SZhaolei  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
2057f1f087SZhaolei  * Boston, MA 02111-1307, USA.
2157f1f087SZhaolei  */
2257f1f087SZhaolei 
2357f1f087SZhaolei /*
2457f1f087SZhaolei  * Converts the calendar time to broken-down time representation
2557f1f087SZhaolei  *
2657f1f087SZhaolei  * 2009-7-14:
2757f1f087SZhaolei  *   Moved from glibc-2.6 to kernel by Zhaolei<zhaolei@cn.fujitsu.com>
28*27601055SCassio Neri  * 2021-06-02:
29*27601055SCassio Neri  *   Reimplemented by Cassio Neri <cassio.neri@gmail.com>
3057f1f087SZhaolei  */
3157f1f087SZhaolei 
3257f1f087SZhaolei #include <linux/time.h>
3357f1f087SZhaolei #include <linux/module.h>
34*27601055SCassio Neri #include <linux/kernel.h>
3557f1f087SZhaolei 
3657f1f087SZhaolei #define SECS_PER_HOUR	(60 * 60)
3757f1f087SZhaolei #define SECS_PER_DAY	(SECS_PER_HOUR * 24)
3857f1f087SZhaolei 
3957f1f087SZhaolei /**
40e6c2682aSDeepa Dinamani  * time64_to_tm - converts the calendar time to local broken-down time
4157f1f087SZhaolei  *
42a0f5a65fSAlex Shi  * @totalsecs:	the number of seconds elapsed since 00:00:00 on January 1, 1970,
4357f1f087SZhaolei  *		Coordinated Universal Time (UTC).
44a0f5a65fSAlex Shi  * @offset:	offset seconds adding to totalsecs.
45a0f5a65fSAlex Shi  * @result:	pointer to struct tm variable to receive broken-down time
4657f1f087SZhaolei  */
time64_to_tm(time64_t totalsecs,int offset,struct tm * result)47e6c2682aSDeepa Dinamani void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
4857f1f087SZhaolei {
49*27601055SCassio Neri 	u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day;
50*27601055SCassio Neri 	u64 u64tmp, udays, century, year;
51*27601055SCassio Neri 	bool is_Jan_or_Feb, is_leap_year;
52*27601055SCassio Neri 	long days, rem;
53e6c2682aSDeepa Dinamani 	int remainder;
5457f1f087SZhaolei 
55e6c2682aSDeepa Dinamani 	days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
56e6c2682aSDeepa Dinamani 	rem = remainder;
5757f1f087SZhaolei 	rem += offset;
5857f1f087SZhaolei 	while (rem < 0) {
5957f1f087SZhaolei 		rem += SECS_PER_DAY;
6057f1f087SZhaolei 		--days;
6157f1f087SZhaolei 	}
6257f1f087SZhaolei 	while (rem >= SECS_PER_DAY) {
6357f1f087SZhaolei 		rem -= SECS_PER_DAY;
6457f1f087SZhaolei 		++days;
6557f1f087SZhaolei 	}
6657f1f087SZhaolei 
6757f1f087SZhaolei 	result->tm_hour = rem / SECS_PER_HOUR;
6857f1f087SZhaolei 	rem %= SECS_PER_HOUR;
6957f1f087SZhaolei 	result->tm_min = rem / 60;
7057f1f087SZhaolei 	result->tm_sec = rem % 60;
7157f1f087SZhaolei 
7257f1f087SZhaolei 	/* January 1, 1970 was a Thursday. */
7357f1f087SZhaolei 	result->tm_wday = (4 + days) % 7;
7457f1f087SZhaolei 	if (result->tm_wday < 0)
7557f1f087SZhaolei 		result->tm_wday += 7;
7657f1f087SZhaolei 
77*27601055SCassio Neri 	/*
78*27601055SCassio Neri 	 * The following algorithm is, basically, Proposition 6.3 of Neri
79*27601055SCassio Neri 	 * and Schneider [1]. In a few words: it works on the computational
80*27601055SCassio Neri 	 * (fictitious) calendar where the year starts in March, month = 2
81*27601055SCassio Neri 	 * (*), and finishes in February, month = 13. This calendar is
82*27601055SCassio Neri 	 * mathematically convenient because the day of the year does not
83*27601055SCassio Neri 	 * depend on whether the year is leap or not. For instance:
84*27601055SCassio Neri 	 *
85*27601055SCassio Neri 	 * March 1st		0-th day of the year;
86*27601055SCassio Neri 	 * ...
87*27601055SCassio Neri 	 * April 1st		31-st day of the year;
88*27601055SCassio Neri 	 * ...
89*27601055SCassio Neri 	 * January 1st		306-th day of the year; (Important!)
90*27601055SCassio Neri 	 * ...
91*27601055SCassio Neri 	 * February 28th	364-th day of the year;
92*27601055SCassio Neri 	 * February 29th	365-th day of the year (if it exists).
93*27601055SCassio Neri 	 *
94*27601055SCassio Neri 	 * After having worked out the date in the computational calendar
95*27601055SCassio Neri 	 * (using just arithmetics) it's easy to convert it to the
96*27601055SCassio Neri 	 * corresponding date in the Gregorian calendar.
97*27601055SCassio Neri 	 *
98*27601055SCassio Neri 	 * [1] "Euclidean Affine Functions and Applications to Calendar
99*27601055SCassio Neri 	 * Algorithms". https://arxiv.org/abs/2102.06959
100*27601055SCassio Neri 	 *
101*27601055SCassio Neri 	 * (*) The numbering of months follows tm more closely and thus,
102*27601055SCassio Neri 	 * is slightly different from [1].
103*27601055SCassio Neri 	 */
10457f1f087SZhaolei 
105*27601055SCassio Neri 	udays	= ((u64) days) + 2305843009213814918ULL;
10657f1f087SZhaolei 
107*27601055SCassio Neri 	u64tmp		= 4 * udays + 3;
108*27601055SCassio Neri 	century		= div64_u64_rem(u64tmp, 146097, &u64tmp);
109*27601055SCassio Neri 	day_of_century	= (u32) (u64tmp / 4);
11057f1f087SZhaolei 
111*27601055SCassio Neri 	u32tmp		= 4 * day_of_century + 3;
112*27601055SCassio Neri 	u64tmp		= 2939745ULL * u32tmp;
113*27601055SCassio Neri 	year_of_century	= upper_32_bits(u64tmp);
114*27601055SCassio Neri 	day_of_year	= lower_32_bits(u64tmp) / 2939745 / 4;
11557f1f087SZhaolei 
116*27601055SCassio Neri 	year		= 100 * century + year_of_century;
117*27601055SCassio Neri 	is_leap_year	= year_of_century ? !(year_of_century % 4) : !(century % 4);
11857f1f087SZhaolei 
119*27601055SCassio Neri 	u32tmp		= 2141 * day_of_year + 132377;
120*27601055SCassio Neri 	month		= u32tmp >> 16;
121*27601055SCassio Neri 	day		= ((u16) u32tmp) / 2141;
12257f1f087SZhaolei 
123*27601055SCassio Neri 	/*
124*27601055SCassio Neri 	 * Recall that January 1st is the 306-th day of the year in the
125*27601055SCassio Neri 	 * computational (not Gregorian) calendar.
126*27601055SCassio Neri 	 */
127*27601055SCassio Neri 	is_Jan_or_Feb	= day_of_year >= 306;
128*27601055SCassio Neri 
129*27601055SCassio Neri 	/* Convert to the Gregorian calendar and adjust to Unix time. */
130*27601055SCassio Neri 	year		= year + is_Jan_or_Feb - 6313183731940000ULL;
131*27601055SCassio Neri 	month		= is_Jan_or_Feb ? month - 12 : month;
132*27601055SCassio Neri 	day		= day + 1;
133*27601055SCassio Neri 	day_of_year	+= is_Jan_or_Feb ? -306 : 31 + 28 + is_leap_year;
134*27601055SCassio Neri 
135*27601055SCassio Neri 	/* Convert to tm's format. */
136*27601055SCassio Neri 	result->tm_year = (long) (year - 1900);
137*27601055SCassio Neri 	result->tm_mon  = (int) month;
138*27601055SCassio Neri 	result->tm_mday = (int) day;
139*27601055SCassio Neri 	result->tm_yday = (int) day_of_year;
14057f1f087SZhaolei }
141e6c2682aSDeepa Dinamani EXPORT_SYMBOL(time64_to_tm);
142