summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Kara <jack@suse.cz>2017-06-14 04:42:48 -0400
committerJan Kara <jack@suse.cz>2017-06-14 05:21:02 -0400
commitfd3cfad374d452ebe0a2f87f37c2ceeea7e0d134 (patch)
treecc7dbd6d9baf3769c3f84eab4790db7084d7d53b
parent3c399fa40fd13c5749386695e71f5f747a634f21 (diff)
udf: Convert udf_disk_stamp_to_time() to use mktime64()
Convert udf_disk_stamp_to_time() to use mktime64() to simplify the code. As a bonus we get working timestamp conversion for dates before epoch and after 2038 (both of which are allowed by UDF standard). Signed-off-by: Jan Kara <jack@suse.cz>
-rw-r--r--fs/udf/udftime.c53
1 files changed, 2 insertions, 51 deletions
diff --git a/fs/udf/udftime.c b/fs/udf/udftime.c
index b9dadc7e5c35..14626b34d13e 100644
--- a/fs/udf/udftime.c
+++ b/fs/udf/udftime.c
@@ -40,52 +40,9 @@
40#include <linux/kernel.h> 40#include <linux/kernel.h>
41#include <linux/time.h> 41#include <linux/time.h>
42 42
43#define EPOCH_YEAR 1970
44
45#ifndef __isleap
46/* Nonzero if YEAR is a leap year (every 4 years,
47 except every 100th isn't, and every 400th is). */
48#define __isleap(year) \
49 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
50#endif
51
52/* How many days come before each month (0-12). */
53static const unsigned short int __mon_yday[2][13] = {
54 /* Normal years. */
55 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
56 /* Leap years. */
57 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
58};
59
60#define MAX_YEAR_SECONDS 69
61#define SPD 0x15180 /*3600*24 */
62#define SPY(y, l, s) (SPD * (365 * y + l) + s)
63
64static time_t year_seconds[MAX_YEAR_SECONDS] = {
65/*1970*/ SPY(0, 0, 0), SPY(1, 0, 0), SPY(2, 0, 0), SPY(3, 1, 0),
66/*1974*/ SPY(4, 1, 0), SPY(5, 1, 0), SPY(6, 1, 0), SPY(7, 2, 0),
67/*1978*/ SPY(8, 2, 0), SPY(9, 2, 0), SPY(10, 2, 0), SPY(11, 3, 0),
68/*1982*/ SPY(12, 3, 0), SPY(13, 3, 0), SPY(14, 3, 0), SPY(15, 4, 0),
69/*1986*/ SPY(16, 4, 0), SPY(17, 4, 0), SPY(18, 4, 0), SPY(19, 5, 0),
70/*1990*/ SPY(20, 5, 0), SPY(21, 5, 0), SPY(22, 5, 0), SPY(23, 6, 0),
71/*1994*/ SPY(24, 6, 0), SPY(25, 6, 0), SPY(26, 6, 0), SPY(27, 7, 0),
72/*1998*/ SPY(28, 7, 0), SPY(29, 7, 0), SPY(30, 7, 0), SPY(31, 8, 0),
73/*2002*/ SPY(32, 8, 0), SPY(33, 8, 0), SPY(34, 8, 0), SPY(35, 9, 0),
74/*2006*/ SPY(36, 9, 0), SPY(37, 9, 0), SPY(38, 9, 0), SPY(39, 10, 0),
75/*2010*/ SPY(40, 10, 0), SPY(41, 10, 0), SPY(42, 10, 0), SPY(43, 11, 0),
76/*2014*/ SPY(44, 11, 0), SPY(45, 11, 0), SPY(46, 11, 0), SPY(47, 12, 0),
77/*2018*/ SPY(48, 12, 0), SPY(49, 12, 0), SPY(50, 12, 0), SPY(51, 13, 0),
78/*2022*/ SPY(52, 13, 0), SPY(53, 13, 0), SPY(54, 13, 0), SPY(55, 14, 0),
79/*2026*/ SPY(56, 14, 0), SPY(57, 14, 0), SPY(58, 14, 0), SPY(59, 15, 0),
80/*2030*/ SPY(60, 15, 0), SPY(61, 15, 0), SPY(62, 15, 0), SPY(63, 16, 0),
81/*2034*/ SPY(64, 16, 0), SPY(65, 16, 0), SPY(66, 16, 0), SPY(67, 17, 0),
82/*2038*/ SPY(68, 17, 0)
83};
84
85struct timespec * 43struct timespec *
86udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src) 44udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src)
87{ 45{
88 int yday;
89 u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone); 46 u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
90 u16 year = le16_to_cpu(src.year); 47 u16 year = le16_to_cpu(src.year);
91 uint8_t type = typeAndTimezone >> 12; 48 uint8_t type = typeAndTimezone >> 12;
@@ -100,15 +57,9 @@ udf_disk_stamp_to_time(struct timespec *dest, struct timestamp src)
100 } else 57 } else
101 offset = 0; 58 offset = 0;
102 59
103 if ((year < EPOCH_YEAR) || 60 dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute,
104 (year >= EPOCH_YEAR + MAX_YEAR_SECONDS)) { 61 src.second);
105 return NULL;
106 }
107 dest->tv_sec = year_seconds[year - EPOCH_YEAR];
108 dest->tv_sec -= offset * 60; 62 dest->tv_sec -= offset * 60;
109
110 yday = ((__mon_yday[__isleap(year)][src.month - 1]) + src.day - 1);
111 dest->tv_sec += (((yday * 24) + src.hour) * 60 + src.minute) * 60 + src.second;
112 dest->tv_nsec = 1000 * (src.centiseconds * 10000 + 63 dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
113 src.hundredsOfMicroseconds * 100 + src.microseconds); 64 src.hundredsOfMicroseconds * 100 + src.microseconds);
114 return dest; 65 return dest;