2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll date/time functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2004-03-19 02:16:59 +01:00
|
|
|
* Copyright 2004 Hans Leidekker
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2001-04-23 20:22:33 +02:00
|
|
|
|
2002-08-26 23:53:24 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
#include <time.h>
|
2002-08-26 23:53:24 +02:00
|
|
|
#ifdef HAVE_SYS_TIMES_H
|
|
|
|
# include <sys/times.h>
|
|
|
|
#endif
|
2004-11-06 04:53:53 +01:00
|
|
|
#include <limits.h>
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-04-23 20:22:33 +02:00
|
|
|
#include "msvcrt.h"
|
2004-03-11 01:43:47 +01:00
|
|
|
#include "winbase.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-03-19 02:16:59 +01:00
|
|
|
static const int MonthLengths[2][12] =
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-19 02:16:59 +01:00
|
|
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
|
|
|
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int IsLeapYear(int Year)
|
|
|
|
{
|
|
|
|
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2004-03-19 02:16:59 +01:00
|
|
|
#define SECSPERDAY 86400
|
|
|
|
/* 1601 to 1970 is 369 years plus 89 leap days */
|
|
|
|
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
|
|
|
|
#define TICKSPERSEC 10000000
|
|
|
|
#define TICKSPERMSEC 10000
|
|
|
|
#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
|
|
|
|
|
|
|
|
/* native uses a single static buffer for localtime/gmtime/mktime */
|
|
|
|
static struct MSVCRT_tm tm;
|
|
|
|
|
2001-11-12 16:47:26 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* mktime (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
|
|
|
|
{
|
2004-11-06 04:53:53 +01:00
|
|
|
MSVCRT_time_t secs;
|
|
|
|
FILETIME lft, uft;
|
|
|
|
ULONGLONG time;
|
|
|
|
struct MSVCRT_tm ts;
|
|
|
|
int cleaps, day;
|
2004-03-19 02:16:59 +01:00
|
|
|
|
2004-11-06 04:53:53 +01:00
|
|
|
ts=*t;
|
|
|
|
/* to prevent arithmetic overflows put constraints on some fields */
|
|
|
|
/* whether the effective date falls in the 1970-2038 time period */
|
|
|
|
/* will be tested later */
|
|
|
|
/* BTW, I have no idea what limits native msvcrt has. */
|
|
|
|
if ( ts.tm_year < 0 || ts.tm_year > 140 ||
|
|
|
|
ts.tm_mon < -840 || ts.tm_mon > 840 ||
|
|
|
|
ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
|
|
|
|
ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
|
|
|
|
ts.tm_min < -29000000 || ts.tm_min > 29000000 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* normalize the tm month fields */
|
|
|
|
if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
|
|
|
|
if( ts.tm_mon < 0) {
|
|
|
|
int dy = (11 - ts.tm_mon) / 12;
|
|
|
|
ts.tm_year -= dy;
|
|
|
|
ts.tm_mon += dy * 12;
|
|
|
|
}
|
|
|
|
/* now calculate a day count from the date
|
|
|
|
* First start counting years from March. This way the leap days
|
|
|
|
* are added at the end of the year, not somewhere in the middle.
|
|
|
|
* Formula's become so much less complicate that way.
|
|
|
|
* To convert: add 12 to the month numbers of Jan and Feb, and
|
|
|
|
* take 1 from the year */
|
|
|
|
if(ts.tm_mon < 2) {
|
|
|
|
ts.tm_mon += 14;
|
|
|
|
ts.tm_year += 1899;
|
|
|
|
} else {
|
|
|
|
ts.tm_mon += 2;
|
|
|
|
ts.tm_year += 1900;
|
|
|
|
}
|
|
|
|
cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
|
|
|
|
day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
|
|
|
|
(1959 * ts.tm_mon) / 64 + /* months * daypermonth */
|
|
|
|
ts.tm_mday - /* day of the month */
|
|
|
|
584817 ; /* zero that on 1601-01-01 */
|
|
|
|
/* done */
|
2004-03-19 02:16:59 +01:00
|
|
|
|
2004-11-06 04:53:53 +01:00
|
|
|
/* convert to 100 ns ticks */
|
|
|
|
time = ((((ULONGLONG) day * 24 +
|
|
|
|
ts.tm_hour) * 60 +
|
|
|
|
ts.tm_min) * 60 +
|
|
|
|
ts.tm_sec ) * TICKSPERSEC;
|
|
|
|
|
|
|
|
lft.dwHighDateTime = (DWORD) (time >> 32);
|
|
|
|
lft.dwLowDateTime = (DWORD) time;
|
2004-03-19 02:16:59 +01:00
|
|
|
|
2004-11-06 04:53:53 +01:00
|
|
|
LocalFileTimeToFileTime(&lft, &uft);
|
2004-03-19 02:16:59 +01:00
|
|
|
|
2004-11-06 04:53:53 +01:00
|
|
|
time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
|
|
|
|
time /= TICKSPERSEC;
|
|
|
|
if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
|
|
|
|
return -1;
|
|
|
|
secs = time - SECS_1601_TO_1970;
|
|
|
|
/* compute tm_wday, tm_yday and renormalize the other fields of the
|
|
|
|
* tm structure */
|
|
|
|
if( MSVCRT_localtime( &secs)) *t = tm;
|
|
|
|
|
|
|
|
return secs;
|
2004-03-19 02:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* localtime (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
FILETIME ft, lft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
DWORD tzid;
|
|
|
|
TIME_ZONE_INFORMATION tzinfo;
|
|
|
|
|
|
|
|
ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
|
|
|
|
|
|
|
|
ft.dwHighDateTime = (UINT)(time >> 32);
|
|
|
|
ft.dwLowDateTime = (UINT)time;
|
|
|
|
|
|
|
|
FileTimeToLocalFileTime(&ft, &lft);
|
|
|
|
FileTimeToSystemTime(&lft, &st);
|
|
|
|
|
|
|
|
if (st.wYear < 1970) return NULL;
|
|
|
|
|
|
|
|
tm.tm_sec = st.wSecond;
|
|
|
|
tm.tm_min = st.wMinute;
|
|
|
|
tm.tm_hour = st.wHour;
|
|
|
|
tm.tm_mday = st.wDay;
|
|
|
|
tm.tm_year = st.wYear - 1900;
|
2004-03-25 01:12:40 +01:00
|
|
|
tm.tm_mon = st.wMonth - 1;
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_wday = st.wDayOfWeek;
|
|
|
|
|
2004-03-25 01:12:40 +01:00
|
|
|
for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
|
|
|
|
}
|
|
|
|
|
2004-03-25 01:12:40 +01:00
|
|
|
tm.tm_yday += st.wDay - 1;
|
2004-03-19 02:16:59 +01:00
|
|
|
|
|
|
|
tzid = GetTimeZoneInformation(&tzinfo);
|
|
|
|
|
2004-10-27 23:17:44 +02:00
|
|
|
if (tzid == TIME_ZONE_ID_INVALID)
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_isdst = -1;
|
2004-10-27 23:17:44 +02:00
|
|
|
else
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
|
|
|
|
|
|
|
|
return &tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2004-03-25 01:12:40 +01:00
|
|
|
FILETIME ft;
|
2004-03-19 02:16:59 +01:00
|
|
|
SYSTEMTIME st;
|
|
|
|
|
|
|
|
ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
|
|
|
|
|
|
|
|
ft.dwHighDateTime = (UINT)(time >> 32);
|
|
|
|
ft.dwLowDateTime = (UINT)time;
|
|
|
|
|
2004-03-25 01:12:40 +01:00
|
|
|
FileTimeToSystemTime(&ft, &st);
|
2004-03-19 02:16:59 +01:00
|
|
|
|
|
|
|
if (st.wYear < 1970) return NULL;
|
|
|
|
|
|
|
|
tm.tm_sec = st.wSecond;
|
|
|
|
tm.tm_min = st.wMinute;
|
|
|
|
tm.tm_hour = st.wHour;
|
|
|
|
tm.tm_mday = st.wDay;
|
|
|
|
tm.tm_year = st.wYear - 1900;
|
2004-03-25 01:12:40 +01:00
|
|
|
tm.tm_mon = st.wMonth - 1;
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_wday = st.wDayOfWeek;
|
2004-03-25 01:12:40 +01:00
|
|
|
for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
|
|
|
|
}
|
|
|
|
|
2004-03-25 01:12:40 +01:00
|
|
|
tm.tm_yday += st.wDay - 1;
|
2004-03-19 02:16:59 +01:00
|
|
|
tm.tm_isdst = 0;
|
|
|
|
|
|
|
|
return &tm;
|
2001-11-12 16:47:26 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* _strdate (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char* _strdate(char* date)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-19 02:16:59 +01:00
|
|
|
LPCSTR format = "MM'/'dd'/'yy";
|
|
|
|
|
|
|
|
GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
|
|
|
|
|
|
|
|
return date;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strtime (MSVCRT.@)
|
|
|
|
*/
|
2001-04-10 23:16:07 +02:00
|
|
|
char* _strtime(char* date)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-19 02:16:59 +01:00
|
|
|
LPCSTR format = "HH':'mm':'ss";
|
|
|
|
|
|
|
|
GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
|
|
|
|
|
|
|
|
return date;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* clock (MSVCRT.@)
|
|
|
|
*/
|
2001-04-16 20:54:31 +02:00
|
|
|
MSVCRT_clock_t MSVCRT_clock(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-19 02:16:59 +01:00
|
|
|
FILETIME ftc, fte, ftk, ftu;
|
|
|
|
ULONGLONG utime, ktime;
|
|
|
|
|
|
|
|
MSVCRT_clock_t clock;
|
|
|
|
|
|
|
|
GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
|
|
|
|
|
|
|
|
ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
|
|
|
|
utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
|
|
|
|
|
2004-09-08 21:05:28 +02:00
|
|
|
clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
|
2004-03-19 02:16:59 +01:00
|
|
|
|
|
|
|
return clock;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* difftime (MSVCRT.@)
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-19 02:16:59 +01:00
|
|
|
return (double)(time1 - time2);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ftime (MSVCRT.@)
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
void _ftime(struct MSVCRT__timeb *buf)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2004-03-11 01:43:47 +01:00
|
|
|
TIME_ZONE_INFORMATION tzinfo;
|
|
|
|
FILETIME ft;
|
|
|
|
ULONGLONG time;
|
|
|
|
|
|
|
|
DWORD tzid = GetTimeZoneInformation(&tzinfo);
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
|
|
|
|
time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
|
|
|
|
|
|
|
|
buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
|
|
|
|
buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
|
2004-10-25 23:48:57 +02:00
|
|
|
buf->timezone = tzinfo.Bias +
|
|
|
|
( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
|
|
|
|
( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
|
2004-03-11 01:43:47 +01:00
|
|
|
buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2003-03-04 03:19:15 +01:00
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* time (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
|
|
|
|
{
|
|
|
|
MSVCRT_time_t curtime;
|
|
|
|
struct MSVCRT__timeb tb;
|
|
|
|
|
|
|
|
_ftime(&tb);
|
|
|
|
|
|
|
|
curtime = tb.time;
|
|
|
|
return buf ? *buf = curtime : curtime;
|
|
|
|
}
|
|
|
|
|
2003-03-04 03:19:15 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _daylight (MSVCRT.@)
|
|
|
|
*/
|
2005-05-14 13:07:10 +02:00
|
|
|
int MSVCRT___daylight = 0;
|
2003-03-04 03:19:15 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __p_daylight (MSVCRT.@)
|
|
|
|
*/
|
2005-05-14 13:07:10 +02:00
|
|
|
int *MSVCRT___p__daylight(void)
|
2003-03-04 03:19:15 +01:00
|
|
|
{
|
|
|
|
return &MSVCRT___daylight;
|
|
|
|
}
|
2005-04-23 21:06:29 +02:00
|
|
|
|
2005-09-27 12:55:50 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _dstbias (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int MSVCRT__dstbias = 0;
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __p_dstbias (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int *__p__dstbias(void)
|
|
|
|
{
|
|
|
|
return &MSVCRT__dstbias;
|
|
|
|
}
|
|
|
|
|
2005-05-14 13:07:10 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _timezone (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
long MSVCRT___timezone = 0;
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __p_timezone (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
long *MSVCRT___p__timezone(void)
|
|
|
|
{
|
|
|
|
return &MSVCRT___timezone;
|
|
|
|
}
|
|
|
|
|
2005-04-23 21:06:29 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _tzname (MSVCRT.@)
|
|
|
|
* NOTES
|
|
|
|
* Some apps (notably Mozilla) insist on writing to these, so the buffer
|
|
|
|
* must be large enough. The size is picked based on observation of
|
|
|
|
* Windows XP.
|
|
|
|
*/
|
2005-05-14 13:07:10 +02:00
|
|
|
static char tzname_std[64] = "";
|
|
|
|
static char tzname_dst[64] = "";
|
2005-04-23 21:06:29 +02:00
|
|
|
char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __p_tzname (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
char **__p__tzname(void)
|
|
|
|
{
|
|
|
|
return MSVCRT__tzname;
|
|
|
|
}
|
2005-05-14 13:07:10 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _tzset (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void MSVCRT__tzset(void)
|
|
|
|
{
|
|
|
|
tzset();
|
2005-05-24 13:52:46 +02:00
|
|
|
#if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
|
2005-05-14 13:07:10 +02:00
|
|
|
MSVCRT___daylight = daylight;
|
|
|
|
MSVCRT___timezone = timezone;
|
2005-05-24 13:52:46 +02:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
|
|
|
|
time_t t;
|
|
|
|
struct tm *tmp;
|
|
|
|
long zone_january, zone_july;
|
|
|
|
|
|
|
|
t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
|
|
|
|
tmp = localtime(&t);
|
|
|
|
zone_january = -tmp->tm_gmtoff;
|
|
|
|
t += seconds_in_year / 2;
|
|
|
|
tmp = localtime(&t);
|
|
|
|
zone_july = -tmp->tm_gmtoff;
|
|
|
|
MSVCRT___daylight = (zone_january != zone_july);
|
|
|
|
MSVCRT___timezone = max(zone_january, zone_july);
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-14 13:07:10 +02:00
|
|
|
lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
|
|
|
|
tzname_std[sizeof(tzname_std) - 1] = '\0';
|
|
|
|
lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
|
|
|
|
tzname_dst[sizeof(tzname_dst) - 1] = '\0';
|
|
|
|
}
|