fix tm_gmtoff

tm_gmtoff is opposite of timezone despite daylight saving.
timezone(global variable): seconds west of UTC, not adjusted for daylight saving.
tm_gmtoff(member of struct tm): seconds east of UTC, including adjustment for daylight saving.

Change-Id: If2e76b7da64989fb2df063ce4101d317474a7ab7
This commit is contained in:
Caoruihong 2021-05-08 10:43:07 +08:00
parent 167b4d0c37
commit 9b498e651e
1 changed files with 5 additions and 5 deletions

View File

@ -485,7 +485,7 @@ static INT32 ConvertSecs2Utc(time_t t, INT32 offset, struct tm *tp)
days -= daysInMonth[month]; days -= daysInMonth[month];
tp->tm_mon = month; tp->tm_mon = month;
tp->tm_mday = days + 1; tp->tm_mday = days + 1;
tp->__tm_gmtoff = -offset; tp->__tm_gmtoff = offset;
tp->__tm_zone = NULL; tp->__tm_zone = NULL;
tp->tm_isdst = 0; tp->tm_isdst = 0;
return 1; return 1;
@ -557,7 +557,7 @@ static time_t ConvertUtc2Secs(struct tm *tm)
seconds += (tm->tm_mday - 1) * SECS_PER_DAY; seconds += (tm->tm_mday - 1) * SECS_PER_DAY;
seconds += tm->tm_hour * SECS_PER_HOUR + tm->tm_min * SECS_PER_MIN + tm->tm_sec; seconds += tm->tm_hour * SECS_PER_HOUR + tm->tm_min * SECS_PER_MIN + tm->tm_sec;
seconds += tm->__tm_gmtoff; // add time zone to get UTC time seconds -= tm->__tm_gmtoff; // sub time zone to get UTC time
return seconds; return seconds;
} }
@ -571,8 +571,8 @@ time_t mktime(struct tm *tmptr)
/* tm_isdst is not supported and is ignored */ /* tm_isdst is not supported and is ignored */
if (tmptr->tm_year < (EPOCH_YEAR - TM_YEAR_BASE) || if (tmptr->tm_year < (EPOCH_YEAR - TM_YEAR_BASE) ||
tmptr->__tm_gmtoff > (TIME_ZONE_MAX * SECS_PER_MIN) || tmptr->__tm_gmtoff > (-TIME_ZONE_MIN * SECS_PER_MIN) ||
tmptr->__tm_gmtoff < (TIME_ZONE_MIN * SECS_PER_MIN) || tmptr->__tm_gmtoff < (-TIME_ZONE_MAX * SECS_PER_MIN) ||
tmptr->tm_sec > 60 || tmptr->tm_sec < 0 || /* Seconds [0-60] */ tmptr->tm_sec > 60 || tmptr->tm_sec < 0 || /* Seconds [0-60] */
tmptr->tm_min > 59 || tmptr->tm_min < 0 || /* Minutes [0-59] */ tmptr->tm_min > 59 || tmptr->tm_min < 0 || /* Minutes [0-59] */
tmptr->tm_hour > 23 || tmptr->tm_hour < 0 || /* Hours [0-23] */ tmptr->tm_hour > 23 || tmptr->tm_hour < 0 || /* Hours [0-23] */
@ -583,7 +583,7 @@ time_t mktime(struct tm *tmptr)
} }
timeInSeconds = ConvertUtc2Secs(tmptr); timeInSeconds = ConvertUtc2Secs(tmptr);
/* normalize tm_wday and tm_yday */ /* normalize tm_wday and tm_yday */
ConvertSecs2Utc(timeInSeconds, -tmptr->__tm_gmtoff, tmptr); ConvertSecs2Utc(timeInSeconds, tmptr->__tm_gmtoff, tmptr);
return timeInSeconds; return timeInSeconds;
} }