xref: /openbmc/linux/fs/udf/udftime.c (revision 0220edda)
11da177e4SLinus Torvalds /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
21da177e4SLinus Torvalds    This file is part of the GNU C Library.
31da177e4SLinus Torvalds    Contributed by Paul Eggert (eggert@twinsun.com).
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds    The GNU C Library is free software; you can redistribute it and/or
61da177e4SLinus Torvalds    modify it under the terms of the GNU Library General Public License as
71da177e4SLinus Torvalds    published by the Free Software Foundation; either version 2 of the
81da177e4SLinus Torvalds    License, or (at your option) any later version.
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds    The GNU C Library is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds    but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131da177e4SLinus Torvalds    Library General Public License for more details.
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds    You should have received a copy of the GNU Library General Public
161da177e4SLinus Torvalds    License along with the GNU C Library; see the file COPYING.LIB.  If not,
171da177e4SLinus Torvalds    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
181da177e4SLinus Torvalds    Boston, MA 02111-1307, USA.  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /*
214b11111aSMarcin Slusarz  * dgb 10/02/98: ripped this from glibc source to help convert timestamps
224b11111aSMarcin Slusarz  *               to unix time
234b11111aSMarcin Slusarz  *     10/04/98: added new table-based lookup after seeing how ugly
244b11111aSMarcin Slusarz  *               the gnu code is
251da177e4SLinus Torvalds  * blf 09/27/99: ripped out all the old code and inserted new table from
261da177e4SLinus Torvalds  *		 John Brockmeyer (without leap second corrections)
271da177e4SLinus Torvalds  *		 rewrote udf_stamp_to_time and fixed timezone accounting in
2828de7948SCyrill Gorcunov  *		 udf_time_to_stamp.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  * We don't take into account leap seconds. This may be correct or incorrect.
331da177e4SLinus Torvalds  * For more NIST information (especially dealing with leap seconds), see:
341da177e4SLinus Torvalds  * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
3778ace70cSJoe Perches #include "udfdecl.h"
3878ace70cSJoe Perches 
391da177e4SLinus Torvalds #include <linux/types.h>
401da177e4SLinus Torvalds #include <linux/kernel.h>
413c399fa4SJan Kara #include <linux/time.h>
421da177e4SLinus Torvalds 
430220eddaSDeepa Dinamani void
445ca4e4beSPekka Enberg udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src)
451da177e4SLinus Torvalds {
4656774805SMarcin Slusarz 	u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
4756774805SMarcin Slusarz 	u16 year = le16_to_cpu(src.year);
4856774805SMarcin Slusarz 	uint8_t type = typeAndTimezone >> 12;
491da177e4SLinus Torvalds 	int16_t offset;
501da177e4SLinus Torvalds 
51cb00ea35SCyrill Gorcunov 	if (type == 1) {
5256774805SMarcin Slusarz 		offset = typeAndTimezone << 4;
531da177e4SLinus Torvalds 		/* sign extent offset */
541da177e4SLinus Torvalds 		offset = (offset >> 4);
551da177e4SLinus Torvalds 		if (offset == -2047) /* unspecified offset */
561da177e4SLinus Torvalds 			offset = 0;
57cbf5676aSmarcin.slusarz@gmail.com 	} else
581da177e4SLinus Torvalds 		offset = 0;
591da177e4SLinus Torvalds 
60fd3cfad3SJan Kara 	dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute,
61fd3cfad3SJan Kara 			src.second);
62cbf5676aSmarcin.slusarz@gmail.com 	dest->tv_sec -= offset * 60;
63cbf5676aSmarcin.slusarz@gmail.com 	dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
64cbf5676aSmarcin.slusarz@gmail.com 			src.hundredsOfMicroseconds * 100 + src.microseconds);
65d5bd8213SJan Kara 	/*
66d5bd8213SJan Kara 	 * Sanitize nanosecond field since reportedly some filesystems are
67d5bd8213SJan Kara 	 * recorded with bogus sub-second values.
68d5bd8213SJan Kara 	 */
69d5bd8213SJan Kara 	dest->tv_nsec %= NSEC_PER_SEC;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
720220eddaSDeepa Dinamani void
735ca4e4beSPekka Enberg udf_time_to_disk_stamp(struct timestamp *dest, struct timespec ts)
741da177e4SLinus Torvalds {
753c399fa4SJan Kara 	long seconds;
761da177e4SLinus Torvalds 	int16_t offset;
773c399fa4SJan Kara 	struct tm tm;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	offset = -sys_tz.tz_minuteswest;
801da177e4SLinus Torvalds 
8156774805SMarcin Slusarz 	dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
821da177e4SLinus Torvalds 
833c399fa4SJan Kara 	seconds = ts.tv_sec + offset * 60;
843c399fa4SJan Kara 	time64_to_tm(seconds, 0, &tm);
853c399fa4SJan Kara 	dest->year = cpu_to_le16(tm.tm_year + 1900);
863c399fa4SJan Kara 	dest->month = tm.tm_mon + 1;
873c399fa4SJan Kara 	dest->day = tm.tm_mday;
883c399fa4SJan Kara 	dest->hour = tm.tm_hour;
893c399fa4SJan Kara 	dest->minute = tm.tm_min;
903c399fa4SJan Kara 	dest->second = tm.tm_sec;
911da177e4SLinus Torvalds 	dest->centiseconds = ts.tv_nsec / 10000000;
924b11111aSMarcin Slusarz 	dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
934b11111aSMarcin Slusarz 					dest->centiseconds * 10000) / 100;
9428de7948SCyrill Gorcunov 	dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
951da177e4SLinus Torvalds 			      dest->hundredsOfMicroseconds * 100);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* EOF */
99