time.h 2.8 KB

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