From eeb8552f6c24a4be39173008c9b75faf73ebe214 Mon Sep 17 00:00:00 2001 From: resver Date: Fri, 2 Oct 2009 15:45:08 +0000 Subject: [PATCH] Added macro that calculates leap years count passed from exFAT epoch. git-svn-id: http://exfat.googlecode.com/svn/trunk@20 60bc1c72-a15a-11de-b98f-4500b42dc123 --- libexfat/utils.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libexfat/utils.c b/libexfat/utils.c index b078c8c..9fef909 100644 --- a/libexfat/utils.c +++ b/libexfat/utils.c @@ -106,6 +106,10 @@ void exfat_stat(const struct exfat_node* node, struct stat *stbuf) #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4) /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */ #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY) +/* number of leap years passed from exFAT epoch to the specified year + (excluding the specified year itself) */ +#define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \ + - (EXFAT_EPOCH_YEAR - 1) / 4) static const time_t days_in_year[] = { @@ -158,9 +162,7 @@ time_t exfat_exfat2unix(le16_t date, le16_t time) } /* every 4th year between 1904 and 2096 is leap */ - unix_time += edate.year * SEC_IN_YEAR - + ((EXFAT_EPOCH_YEAR + edate.year - 1) / 4 - (EXFAT_EPOCH_YEAR - 1) / 4) - * SEC_IN_DAY; + unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY; unix_time += days_in_year[edate.month] * SEC_IN_DAY; /* if it's leap year and February has passed we should add 1 day */ if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2) -- 2.11.0