Linux Audio

Check our new training course

Linux kernel drivers training

Mar 31-Apr 9, 2025, special US time zones
Register
Loading...
v6.13.7
 1// SPDX-License-Identifier: LGPL-2.0+
 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/*
 7 * dgb 10/02/98: ripped this from glibc source to help convert timestamps
 8 *               to unix time
 9 *     10/04/98: added new table-based lookup after seeing how ugly
10 *               the gnu code is
11 * blf 09/27/99: ripped out all the old code and inserted new table from
12 *		 John Brockmeyer (without leap second corrections)
13 *		 rewrote udf_stamp_to_time and fixed timezone accounting in
14 *		 udf_time_to_stamp.
15 */
16
17/*
18 * We don't take into account leap seconds. This may be correct or incorrect.
19 * For more NIST information (especially dealing with leap seconds), see:
20 * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
21 */
22
23#include "udfdecl.h"
24
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/time.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
29void
30udf_disk_stamp_to_time(struct timespec64 *dest, struct timestamp src)
 
 
 
31{
 
32	u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
33	u16 year = le16_to_cpu(src.year);
34	uint8_t type = typeAndTimezone >> 12;
35	int16_t offset;
36
37	if (type == 1) {
38		offset = typeAndTimezone << 4;
39		/* sign extent offset */
40		offset = (offset >> 4);
41		if (offset == -2047) /* unspecified offset */
42			offset = 0;
43	} else
44		offset = 0;
45
46	dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute,
47			src.second);
 
 
 
48	dest->tv_sec -= offset * 60;
49
50	/*
51	 * Sanitize nanosecond field since reportedly some filesystems are
52	 * recorded with bogus sub-second values.
53	 */
54	if (src.centiseconds < 100 && src.hundredsOfMicroseconds < 100 &&
55	    src.microseconds < 100) {
56		dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
57			src.hundredsOfMicroseconds * 100 + src.microseconds);
58	} else {
59		dest->tv_nsec = 0;
60	}
61}
62
63void
64udf_time_to_disk_stamp(struct timestamp *dest, struct timespec64 ts)
65{
66	time64_t seconds;
 
67	int16_t offset;
68	struct tm tm;
69
70	offset = -sys_tz.tz_minuteswest;
71
 
 
 
72	dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
73
74	seconds = ts.tv_sec + offset * 60;
75	time64_to_tm(seconds, 0, &tm);
76	dest->year = cpu_to_le16(tm.tm_year + 1900);
77	dest->month = tm.tm_mon + 1;
78	dest->day = tm.tm_mday;
79	dest->hour = tm.tm_hour;
80	dest->minute = tm.tm_min;
81	dest->second = tm.tm_sec;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82	dest->centiseconds = ts.tv_nsec / 10000000;
83	dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
84					dest->centiseconds * 10000) / 100;
85	dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
86			      dest->hundredsOfMicroseconds * 100);
 
87}
88
89/* EOF */
v3.1
 
  1/* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  2   This file is part of the GNU C Library.
  3   Contributed by Paul Eggert (eggert@twinsun.com).
  4
  5   The GNU C Library is free software; you can redistribute it and/or
  6   modify it under the terms of the GNU Library General Public License as
  7   published by the Free Software Foundation; either version 2 of the
  8   License, or (at your option) any later version.
  9
 10   The GNU C Library is distributed in the hope that it will be useful,
 11   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13   Library General Public License for more details.
 14
 15   You should have received a copy of the GNU Library General Public
 16   License along with the GNU C Library; see the file COPYING.LIB.  If not,
 17   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 18   Boston, MA 02111-1307, USA.  */
 19
 20/*
 21 * dgb 10/02/98: ripped this from glibc source to help convert timestamps
 22 *               to unix time
 23 *     10/04/98: added new table-based lookup after seeing how ugly
 24 *               the gnu code is
 25 * blf 09/27/99: ripped out all the old code and inserted new table from
 26 *		 John Brockmeyer (without leap second corrections)
 27 *		 rewrote udf_stamp_to_time and fixed timezone accounting in
 28 *		 udf_time_to_stamp.
 29 */
 30
 31/*
 32 * We don't take into account leap seconds. This may be correct or incorrect.
 33 * For more NIST information (especially dealing with leap seconds), see:
 34 * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
 35 */
 36
 
 
 37#include <linux/types.h>
 38#include <linux/kernel.h>
 39#include "udfdecl.h"
 40
 41#define EPOCH_YEAR 1970
 42
 43#ifndef __isleap
 44/* Nonzero if YEAR is a leap year (every 4 years,
 45   except every 100th isn't, and every 400th is).  */
 46#define	__isleap(year)	\
 47  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 48#endif
 49
 50/* How many days come before each month (0-12).  */
 51static const unsigned short int __mon_yday[2][13] = {
 52	/* Normal years.  */
 53	{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
 54	/* Leap years.  */
 55	{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
 56};
 57
 58#define MAX_YEAR_SECONDS	69
 59#define SPD			0x15180	/*3600*24 */
 60#define SPY(y, l, s)		(SPD * (365 * y + l) + s)
 61
 62static time_t year_seconds[MAX_YEAR_SECONDS] = {
 63/*1970*/ SPY(0,   0, 0), SPY(1,   0, 0), SPY(2,   0, 0), SPY(3,   1, 0),
 64/*1974*/ SPY(4,   1, 0), SPY(5,   1, 0), SPY(6,   1, 0), SPY(7,   2, 0),
 65/*1978*/ SPY(8,   2, 0), SPY(9,   2, 0), SPY(10,  2, 0), SPY(11,  3, 0),
 66/*1982*/ SPY(12,  3, 0), SPY(13,  3, 0), SPY(14,  3, 0), SPY(15,  4, 0),
 67/*1986*/ SPY(16,  4, 0), SPY(17,  4, 0), SPY(18,  4, 0), SPY(19,  5, 0),
 68/*1990*/ SPY(20,  5, 0), SPY(21,  5, 0), SPY(22,  5, 0), SPY(23,  6, 0),
 69/*1994*/ SPY(24,  6, 0), SPY(25,  6, 0), SPY(26,  6, 0), SPY(27,  7, 0),
 70/*1998*/ SPY(28,  7, 0), SPY(29,  7, 0), SPY(30,  7, 0), SPY(31,  8, 0),
 71/*2002*/ SPY(32,  8, 0), SPY(33,  8, 0), SPY(34,  8, 0), SPY(35,  9, 0),
 72/*2006*/ SPY(36,  9, 0), SPY(37,  9, 0), SPY(38,  9, 0), SPY(39, 10, 0),
 73/*2010*/ SPY(40, 10, 0), SPY(41, 10, 0), SPY(42, 10, 0), SPY(43, 11, 0),
 74/*2014*/ SPY(44, 11, 0), SPY(45, 11, 0), SPY(46, 11, 0), SPY(47, 12, 0),
 75/*2018*/ SPY(48, 12, 0), SPY(49, 12, 0), SPY(50, 12, 0), SPY(51, 13, 0),
 76/*2022*/ SPY(52, 13, 0), SPY(53, 13, 0), SPY(54, 13, 0), SPY(55, 14, 0),
 77/*2026*/ SPY(56, 14, 0), SPY(57, 14, 0), SPY(58, 14, 0), SPY(59, 15, 0),
 78/*2030*/ SPY(60, 15, 0), SPY(61, 15, 0), SPY(62, 15, 0), SPY(63, 16, 0),
 79/*2034*/ SPY(64, 16, 0), SPY(65, 16, 0), SPY(66, 16, 0), SPY(67, 17, 0),
 80/*2038*/ SPY(68, 17, 0)
 81};
 82
 83extern struct timezone sys_tz;
 84
 85#define SECS_PER_HOUR	(60 * 60)
 86#define SECS_PER_DAY	(SECS_PER_HOUR * 24)
 87
 88struct timespec *
 89udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src)
 90{
 91	int yday;
 92	u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
 93	u16 year = le16_to_cpu(src.year);
 94	uint8_t type = typeAndTimezone >> 12;
 95	int16_t offset;
 96
 97	if (type == 1) {
 98		offset = typeAndTimezone << 4;
 99		/* sign extent offset */
100		offset = (offset >> 4);
101		if (offset == -2047) /* unspecified offset */
102			offset = 0;
103	} else
104		offset = 0;
105
106	if ((year < EPOCH_YEAR) ||
107	    (year >= EPOCH_YEAR + MAX_YEAR_SECONDS)) {
108		return NULL;
109	}
110	dest->tv_sec = year_seconds[year - EPOCH_YEAR];
111	dest->tv_sec -= offset * 60;
112
113	yday = ((__mon_yday[__isleap(year)][src.month - 1]) + src.day - 1);
114	dest->tv_sec += (((yday * 24) + src.hour) * 60 + src.minute) * 60 + src.second;
115	dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
 
 
 
 
116			src.hundredsOfMicroseconds * 100 + src.microseconds);
117	return dest;
 
 
118}
119
120struct timestamp *
121udf_time_to_disk_stamp(struct timestamp *dest, struct timespec ts)
122{
123	long int days, rem, y;
124	const unsigned short int *ip;
125	int16_t offset;
 
126
127	offset = -sys_tz.tz_minuteswest;
128
129	if (!dest)
130		return NULL;
131
132	dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
133
134	ts.tv_sec += offset * 60;
135	days = ts.tv_sec / SECS_PER_DAY;
136	rem = ts.tv_sec % SECS_PER_DAY;
137	dest->hour = rem / SECS_PER_HOUR;
138	rem %= SECS_PER_HOUR;
139	dest->minute = rem / 60;
140	dest->second = rem % 60;
141	y = 1970;
142
143#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
144#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
145
146	while (days < 0 || days >= (__isleap(y) ? 366 : 365)) {
147		long int yg = y + days / 365 - (days % 365 < 0);
148
149		/* Adjust DAYS and Y to match the guessed year.  */
150		days -= ((yg - y) * 365
151			 + LEAPS_THRU_END_OF(yg - 1)
152			 - LEAPS_THRU_END_OF(y - 1));
153		y = yg;
154	}
155	dest->year = cpu_to_le16(y);
156	ip = __mon_yday[__isleap(y)];
157	for (y = 11; days < (long int)ip[y]; --y)
158		continue;
159	days -= ip[y];
160	dest->month = y + 1;
161	dest->day = days + 1;
162
163	dest->centiseconds = ts.tv_nsec / 10000000;
164	dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
165					dest->centiseconds * 10000) / 100;
166	dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
167			      dest->hundredsOfMicroseconds * 100);
168	return dest;
169}
170
171/* EOF */