diff options
Diffstat (limited to 'src/time64.c')
-rw-r--r-- | src/time64.c | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/src/time64.c b/src/time64.c index f1cffd4..07639df 100644 --- a/src/time64.c +++ b/src/time64.c @@ -1,4 +1,4 @@ -/* +/* Copyright (c) 2007-2010 Michael G Schwern @@ -30,7 +30,7 @@ THE SOFTWARE. Programmers who have available to them 64-bit time values as a 'long long' type can use localtime64_r() and gmtime64_r() which correctly -converts the time even on 32-bit systems. Whether you have 64-bit time +converts the time even on 32-bit systems. Whether you have 64-bit time values will depend on the operating system. localtime64_r() is a 64-bit equivalent of localtime_r(). @@ -59,11 +59,11 @@ static const short julian_days_by_month[2][12] = { {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}, }; -static char wday_name[7][4] = { +static const char wday_name[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -static char mon_name[12][4] = { +static const char mon_name[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; @@ -176,34 +176,28 @@ static int is_exception_century(Year year) static int cmp_date( const struct TM* left, const struct tm* right ) { if( left->tm_year > right->tm_year ) return 1; - else if( left->tm_year < right->tm_year ) + if( left->tm_year < right->tm_year ) return -1; - if( left->tm_mon > right->tm_mon ) return 1; - else if( left->tm_mon < right->tm_mon ) + if( left->tm_mon < right->tm_mon ) return -1; - if( left->tm_mday > right->tm_mday ) return 1; - else if( left->tm_mday < right->tm_mday ) + if( left->tm_mday < right->tm_mday ) return -1; - if( left->tm_hour > right->tm_hour ) return 1; - else if( left->tm_hour < right->tm_hour ) + if( left->tm_hour < right->tm_hour ) return -1; - if( left->tm_min > right->tm_min ) return 1; - else if( left->tm_min < right->tm_min ) + if( left->tm_min < right->tm_min ) return -1; - if( left->tm_sec > right->tm_sec ) return 1; - else if( left->tm_sec < right->tm_sec ) + if( left->tm_sec < right->tm_sec ) return -1; - return 0; } @@ -233,12 +227,7 @@ Time64_T timegm64(const struct TM *date) { Year orig_year = (Year)date->tm_year; int cycles = 0; - if( orig_year > 100 ) { - cycles = (orig_year - 100) / 400; - orig_year -= cycles * 400; - days += (Time64_T)cycles * days_in_gregorian_cycle; - } - else if( orig_year < -300 ) { + if( (orig_year > 100) || (orig_year < -300) ) { cycles = (orig_year - 100) / 400; orig_year -= cycles * 400; days += (Time64_T)cycles * days_in_gregorian_cycle; @@ -293,7 +282,7 @@ static int check_tm(struct TM *tm) assert(tm->tm_wday >= 0); assert(tm->tm_wday <= 6); - + assert(tm->tm_yday >= 0); assert(tm->tm_yday <= length_of_year[IS_LEAP(tm->tm_year)]); @@ -368,7 +357,7 @@ static int safe_year(const Year year) year_cycle += 17; year_cycle %= SOLAR_CYCLE_LENGTH; - if( year_cycle < 0 ) + if( year_cycle < 0 ) year_cycle = SOLAR_CYCLE_LENGTH + year_cycle; assert( year_cycle >= 0 ); @@ -542,7 +531,7 @@ Time64_T mktime64(struct TM *input_date) { /* Correct the user's possibly out of bound input date */ copy_tm_to_TM64(&safe_date, input_date); - timev += seconds_between_years(year, (Year)(safe_date.tm_year + 1900)); + timev += seconds_between_years(year, (Year)(safe_date.tm_year) + 1900); return timev; } @@ -674,7 +663,7 @@ struct TM *gmtime64_r (const Time64_T *in_time, struct TM *p) p->tm_hour = v_tm_hour; p->tm_mon = v_tm_mon; p->tm_wday = v_tm_wday; - + assert(check_tm(p)); return p; @@ -717,7 +706,7 @@ struct TM *localtime64_r (const Time64_T *timev, struct TM *local_tm) ) { TIME64_TRACE1("Mapping tm_year %lld to safe_year\n", (Year)gm_tm.tm_year); - gm_tm.tm_year = safe_year((Year)(gm_tm.tm_year + 1900)) - 1900; + gm_tm.tm_year = safe_year((Year)(gm_tm.tm_year) + 1900) - 1900; } safe_time = (time_t)timegm64(&gm_tm); @@ -756,7 +745,7 @@ struct TM *localtime64_r (const Time64_T *timev, struct TM *local_tm) local_tm->tm_year++; } - /* GMT is Jan 1st, xx01 year, but localtime is still Dec 31st + /* GMT is Jan 1st, xx01 year, but localtime is still Dec 31st in a non-leap xx00. There is one point in the cycle we can't account for which the safe xx00 year is a leap year. So we need to correct for Dec 31st comming out as @@ -766,7 +755,7 @@ struct TM *localtime64_r (const Time64_T *timev, struct TM *local_tm) local_tm->tm_yday--; assert(check_tm(local_tm)); - + return local_tm; } @@ -774,15 +763,15 @@ struct TM *localtime64_r (const Time64_T *timev, struct TM *local_tm) static int valid_tm_wday( const struct TM* date ) { if( 0 <= date->tm_wday && date->tm_wday <= 6 ) return 1; - else - return 0; + + return 0; } static int valid_tm_mon( const struct TM* date ) { if( 0 <= date->tm_mon && date->tm_mon <= 11 ) return 1; - else - return 0; + + return 0; } |