diff options
Diffstat (limited to 'src/time64.c')
| -rw-r--r-- | src/time64.c | 114 | 
1 files changed, 53 insertions, 61 deletions
diff --git a/src/time64.c b/src/time64.c index 8c08caf..cfbe7ac 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;  } @@ -230,15 +224,11 @@ Time64_T timegm64(const struct TM *date) {      Time64_T days    = 0;      Time64_T seconds = 0;      Year     year; +    assert(date != NULL);      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 +283,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)]); @@ -346,11 +336,11 @@ static Year cycle_offset(Year year)  */  static int safe_year(const Year year)  { -    int safe_year= (int)year; +    int _safe_year = (int)year;      Year year_cycle;      if( year >= MIN_SAFE_YEAR && year <= MAX_SAFE_YEAR ) { -        return safe_year; +        return _safe_year;      }      year_cycle = year + cycle_offset(year); @@ -368,24 +358,24 @@ 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 );      assert( year_cycle < SOLAR_CYCLE_LENGTH );      if( year < MIN_SAFE_YEAR ) -        safe_year = safe_years_low[year_cycle]; +        _safe_year = safe_years_low[year_cycle];      else if( year > MAX_SAFE_YEAR ) -        safe_year = safe_years_high[year_cycle]; +        _safe_year = safe_years_high[year_cycle];      else          assert(0);      TIME64_TRACE3("# year: %lld, year_cycle: %lld, safe_year: %d\n", -          year, year_cycle, safe_year); +          year, year_cycle, _safe_year); -    assert(safe_year <= MAX_SAFE_YEAR && safe_year >= MIN_SAFE_YEAR); +    assert(_safe_year <= MAX_SAFE_YEAR && _safe_year >= MIN_SAFE_YEAR); -    return safe_year; +    return _safe_year;  } @@ -519,17 +509,17 @@ static Time64_T seconds_between_years(Year left_year, Year right_year) {  Time64_T mktime64(struct TM *input_date) {      struct tm safe_date;      struct TM date; -    Time64_T  time; +    Time64_T  timev;      Year      year = input_date->tm_year + 1900;      if( date_in_safe_range(input_date, &SYSTEM_MKTIME_MIN, &SYSTEM_MKTIME_MAX) )      {          copy_TM64_to_tm(input_date, &safe_date); -        time = (Time64_T)mktime(&safe_date); +        timev = (Time64_T)mktime(&safe_date);          /* Correct the possibly out of bound input date */          copy_tm_to_TM64(&safe_date, input_date); -        return time; +        return timev;      }      /* Have to make the year safe in date else it won't fit in safe_date */ @@ -537,14 +527,14 @@ Time64_T mktime64(struct TM *input_date) {      date.tm_year = safe_year(year) - 1900;      copy_TM64_to_tm(&date, &safe_date); -    time = (Time64_T)mktime(&safe_date); +    timev = (Time64_T)mktime(&safe_date);      /* Correct the user's possibly out of bound input date */      copy_tm_to_TM64(&safe_date, input_date); -    time += seconds_between_years(year, (Year)(safe_date.tm_year + 1900)); +    timev += seconds_between_years(year, (Year)(safe_date.tm_year) + 1900); -    return time; +    return timev;  } @@ -560,7 +550,7 @@ struct TM *gmtime64_r (const Time64_T *in_time, struct TM *p)      Time64_T v_tm_tday;      int leap;      Time64_T m; -    Time64_T time = *in_time; +    Time64_T timev = *in_time;      Year year = 70;      int cycles = 0; @@ -587,13 +577,13 @@ struct TM *gmtime64_r (const Time64_T *in_time, struct TM *p)      p->tm_zone   = (char*)"UTC";  #endif -    v_tm_sec =  (int)(time % 60); -    time /= 60; -    v_tm_min =  (int)(time % 60); -    time /= 60; -    v_tm_hour = (int)(time % 24); -    time /= 24; -    v_tm_tday = time; +    v_tm_sec =  (int)(timev % 60); +    timev /= 60; +    v_tm_min =  (int)(timev % 60); +    timev /= 60; +    v_tm_hour = (int)(timev % 24); +    timev /= 24; +    v_tm_tday = timev;      WRAP (v_tm_sec, v_tm_min, 60);      WRAP (v_tm_min, v_tm_hour, 60); @@ -674,14 +664,14 @@ 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;  } -struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm) +struct TM *localtime64_r (const Time64_T *timev, struct TM *local_tm)  {      time_t safe_time;      struct tm safe_date; @@ -692,10 +682,10 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)      assert(local_tm != NULL);      /* Use the system localtime() if time_t is small enough */ -    if( SHOULD_USE_SYSTEM_LOCALTIME(*time) ) { -        safe_time = (time_t)*time; +    if( SHOULD_USE_SYSTEM_LOCALTIME(*timev) ) { +        safe_time = (time_t)*timev; -        TIME64_TRACE1("Using system localtime for %lld\n", *time); +        TIME64_TRACE1("Using system localtime for %lld\n", *timev);          LOCALTIME_R(&safe_time, &safe_date); @@ -705,8 +695,8 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)          return local_tm;      } -    if( gmtime64_r(time, &gm_tm) == NULL ) { -        TIME64_TRACE1("gmtime64_r returned null for %lld\n", *time); +    if( gmtime64_r(timev, &gm_tm) == NULL ) { +        TIME64_TRACE1("gmtime64_r returned null for %lld\n", *timev);          return NULL;      } @@ -717,7 +707,7 @@ struct TM *localtime64_r (const Time64_T *time, 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 +746,7 @@ struct TM *localtime64_r (const Time64_T *time, 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 +756,7 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)          local_tm->tm_yday--;      assert(check_tm(local_tm)); -     +      return local_tm;  } @@ -774,15 +764,15 @@ struct TM *localtime64_r (const Time64_T *time, 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;  } @@ -803,10 +793,12 @@ char *asctime64_r( const struct TM* date, char *result ) {  } -char *ctime64_r( const Time64_T* time, char* result ) { +char *ctime64_r( const Time64_T* timev, char* result ) {      struct TM date; -    localtime64_r( time, &date ); +    if (!localtime64_r( timev, &date )) +        return NULL; +      return asctime64_r( &date, result );  }  | 
