- time_t unix_time = EPOCH_DIFF_SEC;
- uint16_t ndate = le16_to_cpu(date);
- uint16_t ntime = le16_to_cpu(time);
-
- uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */
- uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
- uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */
-
- uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */
- uint16_t min = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
- uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */
-
- if (day == 0 || month == 0 || month > 12)
- {
- exfat_error("bad date %hu-%02hu-%02hu",
- year + EXFAT_EPOCH_YEAR, month, day);
- return 0;
- }
- if (hour > 23 || min > 59 || twosec > 29)
- {
- exfat_error("bad time %hu:%02hu:%02hu",
- hour, min, twosec * 2);
- return 0;
- }
- if (centisec > 199)
- {
- exfat_error("bad centiseconds count %hhu", centisec);
- return 0;
- }
-
- /* every 4th year between 1904 and 2096 is leap */
- unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
- unix_time += days_in_year[month] * SEC_IN_DAY;
- /* if it's leap year and February has passed we should add 1 day */
- if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
- unix_time += SEC_IN_DAY;
- unix_time += (day - 1) * SEC_IN_DAY;
-
- unix_time += hour * SEC_IN_HOUR;
- unix_time += min * SEC_IN_MIN;
- /* exFAT represents time with 2 sec granularity */
- unix_time += twosec * 2;
- unix_time += centisec / 100;
-
- /* exFAT stores timestamps in local time, so we correct it to UTC */
- unix_time += timezone;
-
- return unix_time;