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