blob: 77c331f1a77048ef43fddab49f098ac7ca9d4f9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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/*
Marcin Slusarz4b111112008-02-08 04:20:36 -080021 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * blf 09/27/99: ripped out all the old code and inserted new table from
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070026 * John Brockmeyer (without leap second corrections)
27 * rewrote udf_stamp_to_time and fixed timezone accounting in
28 * udf_time_to_stamp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
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:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070034 * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
Joe Perches78ace702011-10-10 01:08:05 -070037#include "udfdecl.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define EPOCH_YEAR 1970
43
44#ifndef __isleap
45/* Nonzero if YEAR is a leap year (every 4 years,
46 except every 100th isn't, and every 400th is). */
47#define __isleap(year) \
48 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
49#endif
50
51/* How many days come before each month (0-12). */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070052static const unsigned short int __mon_yday[2][13] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* Normal years. */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* Leap years. */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070056 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59#define MAX_YEAR_SECONDS 69
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070060#define SPD 0x15180 /*3600*24 */
Marcin Slusarz4b111112008-02-08 04:20:36 -080061#define SPY(y, l, s) (SPD * (365 * y + l) + s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Marcin Slusarz4b111112008-02-08 04:20:36 -080063static time_t year_seconds[MAX_YEAR_SECONDS] = {
64/*1970*/ SPY(0, 0, 0), SPY(1, 0, 0), SPY(2, 0, 0), SPY(3, 1, 0),
65/*1974*/ SPY(4, 1, 0), SPY(5, 1, 0), SPY(6, 1, 0), SPY(7, 2, 0),
66/*1978*/ SPY(8, 2, 0), SPY(9, 2, 0), SPY(10, 2, 0), SPY(11, 3, 0),
67/*1982*/ SPY(12, 3, 0), SPY(13, 3, 0), SPY(14, 3, 0), SPY(15, 4, 0),
68/*1986*/ SPY(16, 4, 0), SPY(17, 4, 0), SPY(18, 4, 0), SPY(19, 5, 0),
69/*1990*/ SPY(20, 5, 0), SPY(21, 5, 0), SPY(22, 5, 0), SPY(23, 6, 0),
70/*1994*/ SPY(24, 6, 0), SPY(25, 6, 0), SPY(26, 6, 0), SPY(27, 7, 0),
71/*1998*/ SPY(28, 7, 0), SPY(29, 7, 0), SPY(30, 7, 0), SPY(31, 8, 0),
72/*2002*/ SPY(32, 8, 0), SPY(33, 8, 0), SPY(34, 8, 0), SPY(35, 9, 0),
73/*2006*/ SPY(36, 9, 0), SPY(37, 9, 0), SPY(38, 9, 0), SPY(39, 10, 0),
74/*2010*/ SPY(40, 10, 0), SPY(41, 10, 0), SPY(42, 10, 0), SPY(43, 11, 0),
75/*2014*/ SPY(44, 11, 0), SPY(45, 11, 0), SPY(46, 11, 0), SPY(47, 12, 0),
76/*2018*/ SPY(48, 12, 0), SPY(49, 12, 0), SPY(50, 12, 0), SPY(51, 13, 0),
77/*2022*/ SPY(52, 13, 0), SPY(53, 13, 0), SPY(54, 13, 0), SPY(55, 14, 0),
78/*2026*/ SPY(56, 14, 0), SPY(57, 14, 0), SPY(58, 14, 0), SPY(59, 15, 0),
79/*2030*/ SPY(60, 15, 0), SPY(61, 15, 0), SPY(62, 15, 0), SPY(63, 16, 0),
80/*2034*/ SPY(64, 16, 0), SPY(65, 16, 0), SPY(66, 16, 0), SPY(67, 17, 0),
81/*2038*/ SPY(68, 17, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define SECS_PER_HOUR (60 * 60)
85#define SECS_PER_DAY (SECS_PER_HOUR * 24)
86
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020087struct timespec *
88udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int yday;
Marcin Slusarz56774802008-02-10 11:25:31 +010091 u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
92 u16 year = le16_to_cpu(src.year);
93 uint8_t type = typeAndTimezone >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int16_t offset;
95
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070096 if (type == 1) {
Marcin Slusarz56774802008-02-10 11:25:31 +010097 offset = typeAndTimezone << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* sign extent offset */
99 offset = (offset >> 4);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 if (offset == -2047) /* unspecified offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 offset = 0;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100102 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 offset = 0;
104
Marcin Slusarz56774802008-02-10 11:25:31 +0100105 if ((year < EPOCH_YEAR) ||
106 (year >= EPOCH_YEAR + MAX_YEAR_SECONDS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return NULL;
108 }
Marcin Slusarz56774802008-02-10 11:25:31 +0100109 dest->tv_sec = year_seconds[year - EPOCH_YEAR];
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100110 dest->tv_sec -= offset * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Marcin Slusarz56774802008-02-10 11:25:31 +0100112 yday = ((__mon_yday[__isleap(year)][src.month - 1]) + src.day - 1);
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100113 dest->tv_sec += (((yday * 24) + src.hour) * 60 + src.minute) * 60 + src.second;
114 dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
115 src.hundredsOfMicroseconds * 100 + src.microseconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return dest;
117}
118
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200119struct timestamp *
120udf_time_to_disk_stamp(struct timestamp *dest, struct timespec ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 long int days, rem, y;
123 const unsigned short int *ip;
124 int16_t offset;
125
126 offset = -sys_tz.tz_minuteswest;
127
128 if (!dest)
129 return NULL;
130
Marcin Slusarz56774802008-02-10 11:25:31 +0100131 dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 ts.tv_sec += offset * 60;
134 days = ts.tv_sec / SECS_PER_DAY;
135 rem = ts.tv_sec % SECS_PER_DAY;
136 dest->hour = rem / SECS_PER_HOUR;
137 rem %= SECS_PER_HOUR;
138 dest->minute = rem / 60;
139 dest->second = rem % 60;
140 y = 1970;
141
Marcin Slusarz4b111112008-02-08 04:20:36 -0800142#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
144
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700145 while (days < 0 || days >= (__isleap(y) ? 366 : 365)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 long int yg = y + days / 365 - (days % 365 < 0);
147
148 /* Adjust DAYS and Y to match the guessed year. */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700149 days -= ((yg - y) * 365
Marcin Slusarz4b111112008-02-08 04:20:36 -0800150 + LEAPS_THRU_END_OF(yg - 1)
151 - LEAPS_THRU_END_OF(y - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 y = yg;
153 }
Marcin Slusarz56774802008-02-10 11:25:31 +0100154 dest->year = cpu_to_le16(y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ip = __mon_yday[__isleap(y)];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156 for (y = 11; days < (long int)ip[y]; --y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 continue;
158 days -= ip[y];
159 dest->month = y + 1;
160 dest->day = days + 1;
161
162 dest->centiseconds = ts.tv_nsec / 10000000;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800163 dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
164 dest->centiseconds * 10000) / 100;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
166 dest->hundredsOfMicroseconds * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return dest;
168}
169
170/* EOF */