Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * blf 09/27/99: ripped out all the old code and inserted new table from |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 26 | * John Brockmeyer (without leap second corrections) |
| 27 | * rewrote udf_stamp_to_time and fixed timezone accounting in |
| 28 | * udf_time_to_stamp. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 34 | * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
Joe Perches | 78ace70 | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 37 | #include "udfdecl.h" |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/types.h> |
| 40 | #include <linux/kernel.h> |
Jan Kara | 3c399fa | 2017-06-14 09:51:20 +0200 | [diff] [blame] | 41 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 43 | void |
Arnd Bergmann | c3b9cec | 2018-06-20 10:15:13 +0200 | [diff] [blame] | 44 | udf_disk_stamp_to_time(struct timespec64 *dest, struct timestamp src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Marcin Slusarz | 5677480 | 2008-02-10 11:25:31 +0100 | [diff] [blame] | 46 | u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone); |
| 47 | u16 year = le16_to_cpu(src.year); |
| 48 | uint8_t type = typeAndTimezone >> 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int16_t offset; |
| 50 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 51 | if (type == 1) { |
Marcin Slusarz | 5677480 | 2008-02-10 11:25:31 +0100 | [diff] [blame] | 52 | offset = typeAndTimezone << 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* sign extent offset */ |
| 54 | offset = (offset >> 4); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 55 | if (offset == -2047) /* unspecified offset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | offset = 0; |
marcin.slusarz@gmail.com | cbf5676 | 2008-02-27 22:50:14 +0100 | [diff] [blame] | 57 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | offset = 0; |
| 59 | |
Jan Kara | fd3cfad | 2017-06-14 10:42:48 +0200 | [diff] [blame] | 60 | dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute, |
| 61 | src.second); |
marcin.slusarz@gmail.com | cbf5676 | 2008-02-27 22:50:14 +0100 | [diff] [blame] | 62 | dest->tv_sec -= offset * 60; |
marcin.slusarz@gmail.com | cbf5676 | 2008-02-27 22:50:14 +0100 | [diff] [blame] | 63 | dest->tv_nsec = 1000 * (src.centiseconds * 10000 + |
| 64 | src.hundredsOfMicroseconds * 100 + src.microseconds); |
Jan Kara | d5bd8213 | 2017-12-19 08:11:01 +0100 | [diff] [blame] | 65 | /* |
| 66 | * Sanitize nanosecond field since reportedly some filesystems are |
| 67 | * recorded with bogus sub-second values. |
| 68 | */ |
| 69 | dest->tv_nsec %= NSEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 72 | void |
Arnd Bergmann | c3b9cec | 2018-06-20 10:15:13 +0200 | [diff] [blame] | 73 | udf_time_to_disk_stamp(struct timestamp *dest, struct timespec64 ts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Arnd Bergmann | c3b9cec | 2018-06-20 10:15:13 +0200 | [diff] [blame] | 75 | time64_t seconds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | int16_t offset; |
Jan Kara | 3c399fa | 2017-06-14 09:51:20 +0200 | [diff] [blame] | 77 | struct tm tm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | offset = -sys_tz.tz_minuteswest; |
| 80 | |
Marcin Slusarz | 5677480 | 2008-02-10 11:25:31 +0100 | [diff] [blame] | 81 | dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Jan Kara | 3c399fa | 2017-06-14 09:51:20 +0200 | [diff] [blame] | 83 | seconds = ts.tv_sec + offset * 60; |
| 84 | time64_to_tm(seconds, 0, &tm); |
| 85 | dest->year = cpu_to_le16(tm.tm_year + 1900); |
| 86 | dest->month = tm.tm_mon + 1; |
| 87 | dest->day = tm.tm_mday; |
| 88 | dest->hour = tm.tm_hour; |
| 89 | dest->minute = tm.tm_min; |
| 90 | dest->second = tm.tm_sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | dest->centiseconds = ts.tv_nsec / 10000000; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 92 | dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 - |
| 93 | dest->centiseconds * 10000) / 100; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 94 | dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 - |
| 95 | dest->hundredsOfMicroseconds * 100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* EOF */ |