Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * time.h - NTFS time conversion functions.  Part of the Linux-NTFS project.
0004  *
0005  * Copyright (c) 2001-2005 Anton Altaparmakov
0006  */
0007 
0008 #ifndef _LINUX_NTFS_TIME_H
0009 #define _LINUX_NTFS_TIME_H
0010 
0011 #include <linux/time.h>     /* For current_kernel_time(). */
0012 #include <asm/div64.h>      /* For do_div(). */
0013 
0014 #include "endian.h"
0015 
0016 #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000)
0017 
0018 /**
0019  * utc2ntfs - convert Linux UTC time to NTFS time
0020  * @ts:     Linux UTC time to convert to NTFS time
0021  *
0022  * Convert the Linux UTC time @ts to its corresponding NTFS time and return
0023  * that in little endian format.
0024  *
0025  * Linux stores time in a struct timespec64 consisting of a time64_t tv_sec
0026  * and a long tv_nsec where tv_sec is the number of 1-second intervals since
0027  * 1st January 1970, 00:00:00 UTC and tv_nsec is the number of 1-nano-second
0028  * intervals since the value of tv_sec.
0029  *
0030  * NTFS uses Microsoft's standard time format which is stored in a s64 and is
0031  * measured as the number of 100-nano-second intervals since 1st January 1601,
0032  * 00:00:00 UTC.
0033  */
0034 static inline sle64 utc2ntfs(const struct timespec64 ts)
0035 {
0036     /*
0037      * Convert the seconds to 100ns intervals, add the nano-seconds
0038      * converted to 100ns intervals, and then add the NTFS time offset.
0039      */
0040     return cpu_to_sle64((s64)ts.tv_sec * 10000000 + ts.tv_nsec / 100 +
0041             NTFS_TIME_OFFSET);
0042 }
0043 
0044 /**
0045  * get_current_ntfs_time - get the current time in little endian NTFS format
0046  *
0047  * Get the current time from the Linux kernel, convert it to its corresponding
0048  * NTFS time and return that in little endian format.
0049  */
0050 static inline sle64 get_current_ntfs_time(void)
0051 {
0052     struct timespec64 ts;
0053 
0054     ktime_get_coarse_real_ts64(&ts);
0055     return utc2ntfs(ts);
0056 }
0057 
0058 /**
0059  * ntfs2utc - convert NTFS time to Linux time
0060  * @time:   NTFS time (little endian) to convert to Linux UTC
0061  *
0062  * Convert the little endian NTFS time @time to its corresponding Linux UTC
0063  * time and return that in cpu format.
0064  *
0065  * Linux stores time in a struct timespec64 consisting of a time64_t tv_sec
0066  * and a long tv_nsec where tv_sec is the number of 1-second intervals since
0067  * 1st January 1970, 00:00:00 UTC and tv_nsec is the number of 1-nano-second
0068  * intervals since the value of tv_sec.
0069  *
0070  * NTFS uses Microsoft's standard time format which is stored in a s64 and is
0071  * measured as the number of 100 nano-second intervals since 1st January 1601,
0072  * 00:00:00 UTC.
0073  */
0074 static inline struct timespec64 ntfs2utc(const sle64 time)
0075 {
0076     struct timespec64 ts;
0077 
0078     /* Subtract the NTFS time offset. */
0079     u64 t = (u64)(sle64_to_cpu(time) - NTFS_TIME_OFFSET);
0080     /*
0081      * Convert the time to 1-second intervals and the remainder to
0082      * 1-nano-second intervals.
0083      */
0084     ts.tv_nsec = do_div(t, 10000000) * 100;
0085     ts.tv_sec = t;
0086     return ts;
0087 }
0088 
0089 #endif /* _LINUX_NTFS_TIME_H */