Thomas Gleixner | a1d312d | 2019-05-22 09:51:42 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * time.h - NTFS time conversion functions. Part of the Linux-NTFS project. |
| 4 | * |
Anton Altaparmakov | d8ec785 | 2005-02-18 09:23:39 +0000 | [diff] [blame] | 5 | * Copyright (c) 2001-2005 Anton Altaparmakov |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _LINUX_NTFS_TIME_H |
| 9 | #define _LINUX_NTFS_TIME_H |
| 10 | |
| 11 | #include <linux/time.h> /* For current_kernel_time(). */ |
| 12 | #include <asm/div64.h> /* For do_div(). */ |
| 13 | |
| 14 | #include "endian.h" |
| 15 | |
| 16 | #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000) |
| 17 | |
| 18 | /** |
| 19 | * utc2ntfs - convert Linux UTC time to NTFS time |
| 20 | * @ts: Linux UTC time to convert to NTFS time |
| 21 | * |
| 22 | * Convert the Linux UTC time @ts to its corresponding NTFS time and return |
| 23 | * that in little endian format. |
| 24 | * |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 25 | * Linux stores time in a struct timespec64 consisting of a time64_t tv_sec |
| 26 | * and a long tv_nsec where tv_sec is the number of 1-second intervals since |
| 27 | * 1st January 1970, 00:00:00 UTC and tv_nsec is the number of 1-nano-second |
| 28 | * intervals since the value of tv_sec. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * |
| 30 | * NTFS uses Microsoft's standard time format which is stored in a s64 and is |
| 31 | * measured as the number of 100-nano-second intervals since 1st January 1601, |
| 32 | * 00:00:00 UTC. |
| 33 | */ |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 34 | static inline sle64 utc2ntfs(const struct timespec64 ts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | /* |
| 37 | * Convert the seconds to 100ns intervals, add the nano-seconds |
| 38 | * converted to 100ns intervals, and then add the NTFS time offset. |
| 39 | */ |
| 40 | return cpu_to_sle64((s64)ts.tv_sec * 10000000 + ts.tv_nsec / 100 + |
| 41 | NTFS_TIME_OFFSET); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * get_current_ntfs_time - get the current time in little endian NTFS format |
| 46 | * |
| 47 | * Get the current time from the Linux kernel, convert it to its corresponding |
| 48 | * NTFS time and return that in little endian format. |
| 49 | */ |
| 50 | static inline sle64 get_current_ntfs_time(void) |
| 51 | { |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 52 | struct timespec64 ts; |
| 53 | |
| 54 | ktime_get_coarse_real_ts64(&ts); |
| 55 | return utc2ntfs(ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * ntfs2utc - convert NTFS time to Linux time |
| 60 | * @time: NTFS time (little endian) to convert to Linux UTC |
| 61 | * |
| 62 | * Convert the little endian NTFS time @time to its corresponding Linux UTC |
| 63 | * time and return that in cpu format. |
| 64 | * |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 65 | * Linux stores time in a struct timespec64 consisting of a time64_t tv_sec |
| 66 | * and a long tv_nsec where tv_sec is the number of 1-second intervals since |
| 67 | * 1st January 1970, 00:00:00 UTC and tv_nsec is the number of 1-nano-second |
| 68 | * intervals since the value of tv_sec. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | * |
| 70 | * NTFS uses Microsoft's standard time format which is stored in a s64 and is |
| 71 | * measured as the number of 100 nano-second intervals since 1st January 1601, |
| 72 | * 00:00:00 UTC. |
| 73 | */ |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 74 | static inline struct timespec64 ntfs2utc(const sle64 time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Arnd Bergmann | bcf451e | 2018-08-17 15:43:50 -0700 | [diff] [blame] | 76 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | /* Subtract the NTFS time offset. */ |
Anton Altaparmakov | d8ec785 | 2005-02-18 09:23:39 +0000 | [diff] [blame] | 79 | u64 t = (u64)(sle64_to_cpu(time) - NTFS_TIME_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Convert the time to 1-second intervals and the remainder to |
| 82 | * 1-nano-second intervals. |
| 83 | */ |
| 84 | ts.tv_nsec = do_div(t, 10000000) * 100; |
| 85 | ts.tv_sec = t; |
| 86 | return ts; |
| 87 | } |
| 88 | |
| 89 | #endif /* _LINUX_NTFS_TIME_H */ |