time.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * time.h - NTFS time conversion functions. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #ifndef _LINUX_NTFS_TIME_H
  22. #define _LINUX_NTFS_TIME_H
  23. #include <linux/time.h> /* For current_kernel_time(). */
  24. #include <asm/div64.h> /* For do_div(). */
  25. #include "endian.h"
  26. #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000)
  27. /**
  28. * utc2ntfs - convert Linux UTC time to NTFS time
  29. * @ts: Linux UTC time to convert to NTFS time
  30. *
  31. * Convert the Linux UTC time @ts to its corresponding NTFS time and return
  32. * that in little endian format.
  33. *
  34. * Linux stores time in a struct timespec consisting of a time_t (long at
  35. * present) tv_sec and a long tv_nsec where tv_sec is the number of 1-second
  36. * intervals since 1st January 1970, 00:00:00 UTC and tv_nsec is the number of
  37. * 1-nano-second intervals since the value of tv_sec.
  38. *
  39. * NTFS uses Microsoft's standard time format which is stored in a s64 and is
  40. * measured as the number of 100-nano-second intervals since 1st January 1601,
  41. * 00:00:00 UTC.
  42. */
  43. static inline sle64 utc2ntfs(const struct timespec ts)
  44. {
  45. /*
  46. * Convert the seconds to 100ns intervals, add the nano-seconds
  47. * converted to 100ns intervals, and then add the NTFS time offset.
  48. */
  49. return cpu_to_sle64((s64)ts.tv_sec * 10000000 + ts.tv_nsec / 100 +
  50. NTFS_TIME_OFFSET);
  51. }
  52. /**
  53. * get_current_ntfs_time - get the current time in little endian NTFS format
  54. *
  55. * Get the current time from the Linux kernel, convert it to its corresponding
  56. * NTFS time and return that in little endian format.
  57. */
  58. static inline sle64 get_current_ntfs_time(void)
  59. {
  60. return utc2ntfs(current_kernel_time());
  61. }
  62. /**
  63. * ntfs2utc - convert NTFS time to Linux time
  64. * @time: NTFS time (little endian) to convert to Linux UTC
  65. *
  66. * Convert the little endian NTFS time @time to its corresponding Linux UTC
  67. * time and return that in cpu format.
  68. *
  69. * Linux stores time in a struct timespec consisting of a time_t (long at
  70. * present) tv_sec and a long tv_nsec where tv_sec is the number of 1-second
  71. * intervals since 1st January 1970, 00:00:00 UTC and tv_nsec is the number of
  72. * 1-nano-second intervals since the value of tv_sec.
  73. *
  74. * NTFS uses Microsoft's standard time format which is stored in a s64 and is
  75. * measured as the number of 100 nano-second intervals since 1st January 1601,
  76. * 00:00:00 UTC.
  77. */
  78. static inline struct timespec ntfs2utc(const sle64 time)
  79. {
  80. struct timespec ts;
  81. /* Subtract the NTFS time offset. */
  82. u64 t = (u64)(sle64_to_cpu(time) - NTFS_TIME_OFFSET);
  83. /*
  84. * Convert the time to 1-second intervals and the remainder to
  85. * 1-nano-second intervals.
  86. */
  87. ts.tv_nsec = do_div(t, 10000000) * 100;
  88. ts.tv_sec = t;
  89. return ts;
  90. }
  91. #endif /* _LINUX_NTFS_TIME_H */