2009-01-19 22:05:27 +00:00
|
|
|
/*
|
2009-08-06 11:01:44 +00:00
|
|
|
* COPYRIGHT: LGPL, See LGPL.txt in the top level directory
|
2009-01-19 22:05:27 +00:00
|
|
|
* PROJECT: ReactOS CRT library
|
|
|
|
* FILE: lib/sdk/crt/time/mktime.c
|
|
|
|
* PURPOSE: Implementation of mktime, _mkgmtime
|
|
|
|
* PROGRAMERS: Timo Kreuzer
|
|
|
|
*/
|
|
|
|
#include <precomp.h>
|
2009-02-03 12:27:01 +00:00
|
|
|
#include "bitsfixup.h"
|
2009-01-19 22:05:27 +00:00
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
#define MAX_32BIT_TIME 0xFFFFFFFFULL
|
2009-01-19 22:05:27 +00:00
|
|
|
|
|
|
|
static int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
|
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
__time64_t
|
|
|
|
mktime_worker(struct tm * ptm, int utc)
|
2009-01-19 22:05:27 +00:00
|
|
|
{
|
|
|
|
struct tm *ptm2;
|
2009-02-03 12:27:01 +00:00
|
|
|
__time64_t time;
|
2009-01-19 22:05:27 +00:00
|
|
|
int mons, years, leapyears;
|
2009-08-06 15:18:24 +00:00
|
|
|
TIME_ZONE_INFORMATION tzi;
|
|
|
|
DWORD ret;
|
2009-01-19 22:05:27 +00:00
|
|
|
|
|
|
|
/* Normalize year and month */
|
|
|
|
if (ptm->tm_mon < 0)
|
|
|
|
{
|
|
|
|
mons = -ptm->tm_mon - 1;
|
|
|
|
ptm->tm_year -= 1 + mons / 12;
|
|
|
|
ptm->tm_mon = 11 - (mons % 12);
|
|
|
|
}
|
|
|
|
else if (ptm->tm_mon > 11)
|
|
|
|
{
|
|
|
|
mons = ptm->tm_mon;
|
|
|
|
ptm->tm_year += (mons / 12);
|
|
|
|
ptm->tm_mon = mons % 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is it inside margins */
|
|
|
|
if (ptm->tm_year < 70 || ptm->tm_year > 139) // FIXME: max year for 64 bits
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
years = ptm->tm_year - 70;
|
|
|
|
|
|
|
|
/* Number of leapyears passed since 1970 */
|
|
|
|
leapyears = (years + 1) / 4;
|
|
|
|
|
|
|
|
/* Calculate days up to 1st of Jan */
|
|
|
|
time = years * 365 + leapyears;
|
|
|
|
|
|
|
|
/* Calculate days up to 1st of month */
|
|
|
|
time += g_monthdays[ptm->tm_mon];
|
|
|
|
|
|
|
|
/* Check if we need to add a leap day */
|
|
|
|
if (((years + 2) % 4) == 0)
|
|
|
|
{
|
|
|
|
if (ptm->tm_mon > 2)
|
|
|
|
{
|
|
|
|
time++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time += ptm->tm_mday - 1;
|
|
|
|
|
|
|
|
time *= 24;
|
|
|
|
time += ptm->tm_hour;
|
|
|
|
|
|
|
|
time *= 60;
|
|
|
|
time += ptm->tm_min;
|
|
|
|
|
|
|
|
time *= 60;
|
|
|
|
time += ptm->tm_sec;
|
|
|
|
|
|
|
|
if (time < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally get normalized tm struct */
|
2009-06-20 11:21:33 +00:00
|
|
|
ptm2 = _gmtime64(&time);
|
2009-01-19 22:05:27 +00:00
|
|
|
if (!ptm2)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*ptm = *ptm2;
|
|
|
|
|
2009-08-06 11:01:44 +00:00
|
|
|
/* Finally adjust by the difference to GMT in seconds */
|
2009-08-06 15:18:24 +00:00
|
|
|
ret = GetTimeZoneInformation(&tzi);
|
|
|
|
if (ret != TIME_ZONE_ID_INVALID)
|
|
|
|
{
|
|
|
|
time += tzi.Bias * 60;
|
|
|
|
}
|
2009-08-06 11:01:44 +00:00
|
|
|
|
2009-01-19 22:05:27 +00:00
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
/* int tm_sec;
|
|
|
|
int tm_min;
|
|
|
|
int tm_hour;
|
|
|
|
int tm_mday;
|
|
|
|
int tm_mon;
|
|
|
|
int tm_year;
|
|
|
|
int tm_wday;
|
|
|
|
int tm_yday;
|
|
|
|
int tm_isdst;
|
|
|
|
*/
|
|
|
|
|
2011-09-15 17:11:53 +00:00
|
|
|
/**
|
2009-02-03 12:27:01 +00:00
|
|
|
* \name _mkgmtime
|
2011-09-15 17:11:53 +00:00
|
|
|
*
|
2009-02-03 12:27:01 +00:00
|
|
|
*/
|
|
|
|
time_t
|
|
|
|
_mkgmtime(struct tm *ptm)
|
|
|
|
{
|
2011-09-15 17:11:53 +00:00
|
|
|
__time64_t time = mktime_worker(ptm, 1);
|
|
|
|
return (time_t)((time > MAX_32BIT_TIME) ? -1 : time);
|
2009-02-03 12:27:01 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 22:05:27 +00:00
|
|
|
time_t
|
|
|
|
mktime(struct tm *ptm)
|
|
|
|
{
|
2011-09-15 17:11:53 +00:00
|
|
|
__time64_t time = mktime_worker(ptm, 0);
|
|
|
|
return (time_t)((time > MAX_32BIT_TIME) ? -1 : time);
|
2009-02-03 12:27:01 +00:00
|
|
|
}
|
2009-01-19 22:05:27 +00:00
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
__time32_t
|
|
|
|
_mkgmtime32(struct tm *ptm)
|
|
|
|
{
|
2011-09-15 17:11:53 +00:00
|
|
|
__time64_t time = mktime_worker(ptm, 1);
|
|
|
|
return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time);
|
2009-02-03 12:27:01 +00:00
|
|
|
}
|
2009-01-19 22:05:27 +00:00
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
__time32_t
|
|
|
|
_mktime32(struct tm *ptm)
|
|
|
|
{
|
2011-09-15 17:11:53 +00:00
|
|
|
__time64_t time = mktime_worker(ptm, 0);
|
|
|
|
return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time);
|
2009-02-03 12:27:01 +00:00
|
|
|
}
|
2009-01-19 22:05:27 +00:00
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
__time64_t
|
|
|
|
_mkgmtime64(struct tm *ptm)
|
|
|
|
{
|
|
|
|
return mktime_worker(ptm, 1);
|
2009-01-19 22:05:27 +00:00
|
|
|
}
|
|
|
|
|
2009-02-03 12:27:01 +00:00
|
|
|
__time64_t
|
|
|
|
_mktime64(struct tm *ptm)
|
|
|
|
{
|
|
|
|
return mktime_worker(ptm, 0);
|
|
|
|
}
|