"Inverted" time_t value handling.
So far time_t values were interpreted as local time values. However, usually time_t values are to be interpreted as "seconds since 1 Jan 1970 in UTC". Therefore all logic handling time_t values has to be changed. - So far gmtime() called localtime() with an adjusted time_t, now localtime() calls gmtime() with an adjusted time_t. - mktime() has to do "the opposite" of localtime(), to keep it that way mktime() does now the inverse adjustment made by localtime(). - All currently present time() implementations internally call mktime() so they don't require individual adjustments.
This commit is contained in:
parent
753b48647f
commit
a9ce4dc76c
3 changed files with 27 additions and 31 deletions
|
@ -45,19 +45,29 @@
|
||||||
|
|
||||||
struct tm* __fastcall__ gmtime (const time_t* timep)
|
struct tm* __fastcall__ gmtime (const time_t* timep)
|
||||||
{
|
{
|
||||||
|
static struct tm timebuf;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
/* Check for a valid time spec */
|
/* Check the argument */
|
||||||
if (timep == 0) {
|
if (timep == 0 || (long) (t = *timep) < 0) {
|
||||||
|
/* Invalid arg */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the time and correct for the time zone offset */
|
/* Since our ints are just 16 bits, split the given time into seconds,
|
||||||
t = *timep + _tz.timezone;
|
** hours and days. Each of the values will fit in a 16 bit variable.
|
||||||
|
** The mktime routine will then do the rest.
|
||||||
|
*/
|
||||||
|
timebuf.tm_sec = t % 3600;
|
||||||
|
timebuf.tm_min = 0;
|
||||||
|
timebuf.tm_hour = (t / 3600) % 24;
|
||||||
|
timebuf.tm_mday = (t / (3600UL * 24UL)) + 1;
|
||||||
|
timebuf.tm_mon = 0;
|
||||||
|
timebuf.tm_year = 70; /* Base value is 1/1/1970 */
|
||||||
|
|
||||||
/* Use localtime for conversion */
|
/* Call mktime to do the final conversion */
|
||||||
return localtime (&t);
|
mktime (&timebuf);
|
||||||
|
|
||||||
|
/* Return the result */
|
||||||
|
return &timebuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -45,31 +45,16 @@
|
||||||
|
|
||||||
struct tm* __fastcall__ localtime (const time_t* timep)
|
struct tm* __fastcall__ localtime (const time_t* timep)
|
||||||
{
|
{
|
||||||
static struct tm timebuf;
|
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
/* Check the argument */
|
/* Check for a valid time spec */
|
||||||
if (timep == 0 || (long) (t = *timep) < 0) {
|
if (timep == 0) {
|
||||||
/* Invalid arg */
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Since our ints are just 16 bits, split the given time into seconds,
|
/* Get the time and correct for the time zone offset */
|
||||||
** hours and days. Each of the values will fit in a 16 bit variable.
|
t = *timep + _tz.timezone;
|
||||||
** The mktime routine will then do the rest.
|
|
||||||
*/
|
|
||||||
timebuf.tm_sec = t % 3600;
|
|
||||||
timebuf.tm_min = 0;
|
|
||||||
timebuf.tm_hour = (t / 3600) % 24;
|
|
||||||
timebuf.tm_mday = (t / (3600UL * 24UL)) + 1;
|
|
||||||
timebuf.tm_mon = 0;
|
|
||||||
timebuf.tm_year = 70; /* Base value is 1/1/1970 */
|
|
||||||
|
|
||||||
/* Call mktime to do the final conversion */
|
/* Use gmtime for conversion */
|
||||||
mktime (&timebuf);
|
return gmtime (&t);
|
||||||
|
|
||||||
/* Return the result */
|
|
||||||
return &timebuf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -180,7 +180,8 @@ time_t __fastcall__ mktime (register struct tm* TM)
|
||||||
return DayCount * 86400UL +
|
return DayCount * 86400UL +
|
||||||
((unsigned) TM->tm_hour) * 3600UL +
|
((unsigned) TM->tm_hour) * 3600UL +
|
||||||
((unsigned) TM->tm_min) * 60U +
|
((unsigned) TM->tm_min) * 60U +
|
||||||
((unsigned) TM->tm_sec);
|
((unsigned) TM->tm_sec) -
|
||||||
|
_tz.timezone;
|
||||||
|
|
||||||
Error:
|
Error:
|
||||||
/* Error exit */
|
/* Error exit */
|
||||||
|
|
Loading…
Add table
Reference in a new issue