time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. time.c (03.02.12)
  3. exFAT file system implementation library.
  4. Copyright (C) 2010-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "exfat.h"
  17. /* timezone offset from UTC in seconds; positive for western timezones,
  18. negative for eastern ones */
  19. static long exfat_timezone;
  20. #define SEC_IN_MIN 60ll
  21. #define SEC_IN_HOUR (60 * SEC_IN_MIN)
  22. #define SEC_IN_DAY (24 * SEC_IN_HOUR)
  23. #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
  24. /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
  25. #define UNIX_EPOCH_YEAR 1970
  26. /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
  27. #define EXFAT_EPOCH_YEAR 1980
  28. /* number of years from Unix epoch to exFAT epoch */
  29. #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
  30. /* number of days from Unix epoch to exFAT epoch (considering leap days) */
  31. #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
  32. /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
  33. #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
  34. /* number of leap years passed from exFAT epoch to the specified year
  35. (excluding the specified year itself) */
  36. #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
  37. - (EXFAT_EPOCH_YEAR - 1) / 4)
  38. /* checks whether the specified year is leap */
  39. #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
  40. static const time_t days_in_year[] =
  41. {
  42. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  43. 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  44. };
  45. time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
  46. {
  47. time_t unix_time = EPOCH_DIFF_SEC;
  48. uint16_t ndate = le16_to_cpu(date);
  49. uint16_t ntime = le16_to_cpu(time);
  50. uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */
  51. uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
  52. uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */
  53. uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */
  54. uint16_t min = ntime >> 5 & 0x3f; /* 6 bits, 0-59 */
  55. uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */
  56. if (day == 0 || month == 0 || month > 12)
  57. {
  58. exfat_error("bad date %u-%02hu-%02hu",
  59. year + EXFAT_EPOCH_YEAR, month, day);
  60. return 0;
  61. }
  62. if (hour > 23 || min > 59 || twosec > 29)
  63. {
  64. exfat_error("bad time %hu:%02hu:%02u",
  65. hour, min, twosec * 2);
  66. return 0;
  67. }
  68. if (centisec > 199)
  69. {
  70. exfat_error("bad centiseconds count %hhu", centisec);
  71. return 0;
  72. }
  73. /* every 4th year between 1904 and 2096 is leap */
  74. unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
  75. unix_time += days_in_year[month] * SEC_IN_DAY;
  76. /* if it's leap year and February has passed we should add 1 day */
  77. if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
  78. unix_time += SEC_IN_DAY;
  79. unix_time += (day - 1) * SEC_IN_DAY;
  80. unix_time += hour * SEC_IN_HOUR;
  81. unix_time += min * SEC_IN_MIN;
  82. /* exFAT represents time with 2 sec granularity */
  83. unix_time += twosec * 2;
  84. unix_time += centisec / 100;
  85. /* exFAT stores timestamps in local time, so we correct it to UTC */
  86. unix_time += exfat_timezone;
  87. return unix_time;
  88. }
  89. void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
  90. uint8_t* centisec)
  91. {
  92. time_t shift = EPOCH_DIFF_SEC + exfat_timezone;
  93. uint16_t day, month, year;
  94. uint16_t twosec, min, hour;
  95. int days;
  96. int i;
  97. /* time before exFAT epoch cannot be represented */
  98. if (unix_time < shift)
  99. unix_time = shift;
  100. unix_time -= shift;
  101. days = unix_time / SEC_IN_DAY;
  102. year = (4 * days) / (4 * 365 + 1);
  103. days -= year * 365 + LEAP_YEARS(year);
  104. month = 0;
  105. for (i = 1; i <= 12; i++)
  106. {
  107. int leap_day = (IS_LEAP_YEAR(year) && i == 2);
  108. int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
  109. if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
  110. {
  111. month = i;
  112. days -= days_in_year[i] + leap_sub;
  113. break;
  114. }
  115. }
  116. day = days + 1;
  117. hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
  118. min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
  119. twosec = (unix_time % SEC_IN_MIN) / 2;
  120. *date = cpu_to_le16(day | (month << 5) | (year << 9));
  121. *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
  122. if (centisec)
  123. *centisec = (unix_time % 2) * 100;
  124. }
  125. void exfat_tzset(void)
  126. {
  127. time_t now;
  128. tzset();
  129. now = time(NULL);
  130. exfat_timezone = mktime(gmtime(&now)) - now;
  131. }