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