crt update:

- initial implementation of secure crt functions for vista compatibility (implement _get_invalid_parameter_handler, _set_invalid_parameter_handler, _invalid_parameter)
- rewrite time apis, add TLS support, add 64bit time functions and secure crt function support

svn path=/branches/ros-amd64-bringup/; revision=38956
This commit is contained in:
Timo Kreuzer 2009-01-19 22:05:27 +00:00
parent a6ccd8981a
commit eb53e42811
51 changed files with 1575 additions and 4 deletions

View file

@ -291,6 +291,11 @@
<file>lfind.c</file>
<file>lsearch.c</file>
</directory>
<directory name="securecrt">
<file>getinvp.c</file>
<file>invalidp.c</file>
<file>setinvp.c</file>
</directory>
<directory name="setjmp">
<if property="ARCH" value="i386">
<directory name="i386">
@ -437,7 +442,7 @@
<directory name="sys_stat">
<file>systime.c</file>
</directory>
<directory name="time">
<!-- directory name="time">
<file>clock.c</file>
<file>ctime.c</file>
<file>difftime.c</file>
@ -450,7 +455,48 @@
<file>wctime.c</file>
<file>wstrdate.c</file>
<file>wstrtime.c</file>
</directory>
</directory -->
<directory name="time_new">
<file>asctime.c</file>
<file>clock.c</file>
<file>ctime32.c</file>
<file>ctime64.c</file>
<file>ctime.c</file>
<file>difftime32.c</file>
<file>difftime64.c</file>
<file>difftime.c</file>
<file>ftime32.c</file>
<file>ftime64.c</file>
<file>ftime.c</file>
<file>futime32.c</file>
<file>futime64.c</file>
<file>futime.c</file>
<file>gmtime.c</file>
<file>localtime32.c</file>
<file>localtime64.c</file>
<file>localtime.c</file>
<file>mktime.c</file>
<file>strdate.c</file>
<file>strftime.c</file>
<file>strtime.c</file>
<file>time32.c</file>
<file>time64.c</file>
<file>time.c</file>
<file>timezone.c</file>
<file>utime32.c</file>
<file>utime64.c</file>
<file>utime.c</file>
<file>wasctime.c</file>
<file>wctime32.c</file>
<file>wctime64.c</file>
<file>wctime.c</file>
<file>wutime32.c</file>
<file>wutime64.c</file>
<file>wutime.c</file>
<file>wcsftime.c</file>
<file>wstrdate.c</file>
<file>wstrtime.c</file>
</directory>
<directory name="wstring">
<file>wcscoll.c</file>
<file>wcscspn.c</file>

View file

@ -48,8 +48,8 @@ char __fileno_getmode(int _fd);
int __fileno_setmode(int _fd, int _newmode);
void sigabort_handler(int sig);
void UnixTimeToFileTime(time_t unix_time, FILETIME* filetime, DWORD remainder);
time_t FileTimeToUnixTime(const FILETIME* filetime, DWORD *remainder);
//void UnixTimeToFileTime(time_t unix_time, FILETIME* filetime, DWORD remainder);
//time_t FileTimeToUnixTime(const FILETIME* filetime, DWORD *remainder);
#define __FILE_REC_MAX 20

View file

@ -0,0 +1,10 @@
extern void * __pInvalidArgHandler;
void _invalid_parameter(
const wchar_t * expression,
const wchar_t * function,
const wchar_t * file,
unsigned int line,
uintptr_t pReserved);

View file

@ -0,0 +1,56 @@
#define DIFFTIME 0x19db1ded53e8000ULL
#define DIFFDAYS (3 * DAYSPER100YEARS + 17 * DAYSPER4YEARS + 1 * DAYSPERYEAR)
#define DAYSPERYEAR 365
#define DAYSPER4YEARS (4*DAYSPERYEAR+1)
#define DAYSPER100YEARS (25*DAYSPER4YEARS-1)
#define DAYSPER400YEARS (4*DAYSPER100YEARS+1)
#define SECONDSPERDAY (24*60*60)
#define SECONDSPERHOUR (60*60)
#define LEAPDAY 59
extern inline
__time64_t
FileTimeToUnixTime(const FILETIME *FileTime, USHORT *millitm)
{
ULARGE_INTEGER ULargeInt;
__time64_t time;
ULargeInt.LowPart = FileTime->dwLowDateTime;
ULargeInt.HighPart = FileTime->dwHighDateTime;
ULargeInt.QuadPart -= DIFFTIME;
time = ULargeInt.QuadPart / 10000;
if (millitm)
*millitm = (ULargeInt.QuadPart % 10000) / 10;
return time;
}
static __inline
long leapyears_passed(long days)
{
long quadcenturies, centuries, quadyears;
quadcenturies = days / DAYSPER400YEARS;
days -= quadcenturies;
centuries = days / DAYSPER100YEARS;
days += centuries;
quadyears = days / DAYSPER4YEARS;
return quadyears - centuries + quadcenturies;
}
static __inline
long leapdays_passed(long days)
{
return leapyears_passed(days + DAYSPERYEAR - LEAPDAY + 1);
}
static __inline
long years_passed(long days)
{
return (days - leapdays_passed(days)) / 365;
}
extern long dst_begin;
extern long dst_end;

View file

@ -28,6 +28,10 @@ typedef struct _ThreadData
int fpecode; /* fp exception code */
EXCEPTION_RECORD *exc_record; /* Head of exception record list */
struct tm tmbuf; /* Used by gmtime, mktime, mkgmtime, localtime */
char asctimebuf[26]; /* Buffer for asctime and ctime */
wchar_t wasctimebuf[26]; /* Buffer for wasctime and wctime */
} THREADDATA, *PTHREADDATA;

View file

@ -16,6 +16,10 @@ static void InitThreadData(PTHREADDATA ThreadData)
ThreadData->tnext = 1;
memset(&ThreadData->tmbuf, sizeof(struct tm), 0);
memset(&ThreadData->asctimebuf, 26 * sizeof(char), 0);
memset(&ThreadData->wasctimebuf, 26 * sizeof(wchar_t), 0);
/* FIXME: init more thread local data */
}

View file

@ -59,5 +59,7 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
#include <internal/rterror.h>
#include <internal/tls.h>
#include <internal/printf.h>
#include <internal/time.h>
#include <internal/safecrt.h>
#endif /* _CRT_PRECOMP_H */

View file

@ -0,0 +1,17 @@
#include <precomp.h>
_invalid_parameter_handler
_get_invalid_parameter_handler(void)
{
_invalid_parameter_handler oldhandler;
if (__pInvalidArgHandler)
{
oldhandler = DecodePointer(__pInvalidArgHandler);
}
else
{
oldhandler = NULL;
}
return oldhandler;
}

View file

@ -0,0 +1,47 @@
#include <precomp.h>
void * __pInvalidArgHandler = NULL;
void
_invalid_parameter_default(
const wchar_t * expression,
const wchar_t * function,
const wchar_t * file,
unsigned int line,
uintptr_t pReserved)
{
// TODO: launch Doc Watson or something like that
UNIMPLEMENTED;
ExitProcess(-1);
}
// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98835
void
_invalid_parameter(
const wchar_t * expression,
const wchar_t * function,
const wchar_t * file,
unsigned int line,
uintptr_t pReserved)
{
_invalid_parameter_handler handler;
if (__pInvalidArgHandler)
{
handler = DecodePointer(__pInvalidArgHandler);
}
else
{
handler = _invalid_parameter_default;
}
handler(expression, function, file, line, pReserved);
}
// http://cowboyprogramming.com/2007/02/22/what-is-_invalid_parameter_noinfo-and-how-do-i-get-rid-of-it/
void
invalid_parameter_noinfo(void)
{
_invalid_parameter(0, 0, 0, 0, 0);
}

View file

@ -0,0 +1,27 @@
#include <precomp.h>
_invalid_parameter_handler
_set_invalid_parameter_handler(_invalid_parameter_handler newhandler)
{
_invalid_parameter_handler oldhandler;
if (__pInvalidArgHandler)
{
oldhandler = DecodePointer(__pInvalidArgHandler);
}
else
{
oldhandler = NULL;
}
if (newhandler)
{
__pInvalidArgHandler = EncodePointer(newhandler);
}
else
{
__pInvalidArgHandler = 0;
}
return oldhandler;
}

View file

@ -0,0 +1,162 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ftime.c
* PURPOSE: Implementation of asctime(), _asctime_s()
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
#include <tchar.h>
#include <time.h>
#include "bitsfixup.h"
#define DAYSPERWEEK 7
#define MONSPERYEAR 12
static const _TCHAR wday_name[DAYSPERWEEK][4] =
{
_T("Sun "), _T("Mon "), _T("Tue "), _T("Wed "),
_T("Thu "), _T("Fri "), _T("Sat ")
};
static const _TCHAR mon_name[MONSPERYEAR][4] =
{
_T("Jan "), _T("Feb "), _T("Mar "), _T("Apr "), _T("May "), _T("Jun "),
_T("Jul "), _T("Aug "), _T("Sep "), _T("Oct "), _T("Nov "), _T("Dec ")
};
#ifdef _UNICODE
typedef unsigned long long _TCHAR4;
typedef unsigned long _TCHAR2;
#else
typedef unsigned long _TCHAR4;
typedef unsigned short _TCHAR2;
#endif
typedef union
{
_TCHAR text[26];
struct
{
_TCHAR4 WeekDay;
_TCHAR4 Month;
_TCHAR2 Day;
_TCHAR Space1;
_TCHAR2 Hour;
_TCHAR Sep1;
_TCHAR2 Minute;
_TCHAR Sep2;
_TCHAR2 Second;
_TCHAR Space2;
_TCHAR2 Year[2];
_TCHAR lb;
_TCHAR zt;
};
} timebuf_t;
_TCHAR2
static __inline__
IntToChar2(int x)
{
union
{
_TCHAR2 char2;
_TCHAR array[2];
} u;
u.array[0] = '0' + (x / 10);
u.array[1] = '0' + (x % 10);
return u.char2;
}
void
static __inline__
FillBuf(timebuf_t *buf, const struct tm *ptm)
{
/* Format looks like this:
* "Sun Mar 01 12:34:56 1902\n\0" */
buf->WeekDay = *(_TCHAR4*)wday_name[ptm->tm_wday];
buf->Month = *(_TCHAR4*)mon_name[ptm->tm_mon];
buf->Day = IntToChar2(ptm->tm_mday);
buf->Space1 = ' ';
buf->Hour = IntToChar2(ptm->tm_mday);
buf->Sep1 = ':';
buf->Minute = IntToChar2(ptm->tm_mday);
buf->Sep2 = ':';
buf->Second = IntToChar2(ptm->tm_mday);
buf->Space2 = ' ';
buf->Year[0] = IntToChar2(ptm->tm_year / 100);
buf->Year[1] = IntToChar2(ptm->tm_year % 100);
buf->lb = '\n';
buf->zt = '\0';
}
/******************************************************************************
* \name _tasctime_s
* \brief Converts a local time into a string and returns a pointer to it.
* \param buffer Buffer that receives the string (26 characters).
* \param numberOfElements Size of the buffer in characters.
* \param time Pointer to the UTC time.
*/
errno_t
_tasctime_s(
_TCHAR* buffer,
size_t numberOfElements,
const struct tm *ptm)
{
/* Validate parameters */
if (!buffer || numberOfElements < 26 || !ptm ||
(unsigned int)ptm->tm_sec > 59 ||
(unsigned int)ptm->tm_min > 59 ||
(unsigned int)ptm->tm_hour > 23 ||
(unsigned int)ptm->tm_mday > 31 ||
(unsigned int)ptm->tm_mon > 11 ||
(unsigned int)ptm->tm_year > 2038 ||
(unsigned int)ptm->tm_wday > 6 ||
(unsigned int)ptm->tm_yday > 365)
{
_invalid_parameter(NULL,
#ifdef UNICODE
L"_wasctime",
#else
L"asctime",
#endif
_CRT_WIDE(__FILE__),
__LINE__,
0);
return EINVAL;
}
/* Fill the buffer */
FillBuf((timebuf_t*)buffer, ptm);
return 0;
}
/******************************************************************************
* \name _tasctime
* \brief Converts a UTC time into a string and returns a pointer to it.
* \param ptm Pointer to the UTC time.
* \remarks The string is stored in thread local buffer, shared between
* ctime, gmtime and localtime (32 and 64 bit versions).
*/
_TCHAR *
_tasctime(const struct tm *ptm)
{
PTHREADDATA pThreadData;
_TCHAR *pstr;
/* Get pointer to TLS buffer */
pThreadData = GetThreadData();
#ifndef _UNICODE
pstr = pThreadData->asctimebuf;
#else
pstr = pThreadData->wasctimebuf;
#endif
/* Fill the buffer */
FillBuf((timebuf_t*)pstr, ptm);
return pstr;
}

View file

@ -0,0 +1,51 @@
#if defined(_USE_EXPLITIT_32BIT_TIME) || defined(_USE_EXPLITIT_64BIT_TIME)
#undef _timeb
#undef _ftime
#undef _tctime
#undef _ftime_s
#undef _tctime_s
#undef _tutime
#else
#define _time time
#endif
#ifdef _USE_EXPLITIT_32BIT_TIME
#define time_t __time32_t
#define _timeb __timeb32
#define _utimbuf __utimbuf32
#define difftime _difftime32
#define localtime _localtime32
#define localtime_s _localtime32_s
#define _time _time32
#define _ftime _ftime32
#define _ftime_s _ftime32_s
#define _futime _futime32
#define _tctime _tctime32
#define _tctime_s _tctime32_s
#define _tutime _tutime32
#define gmtime _gmtime32
#endif
#ifdef _USE_EXPLITIT_64BIT_TIME
#define time_t __time64_t
#define _timeb __timeb64
#define _utimbuf __utimbuf64
#define difftime _difftime64
#define localtime _localtime64
#define localtime_s _localtime64_s
#define _time _time64
#define _ftime _ftime64
#define _ftime_s _ftime64_s
#define _futime _futime64
#define _tctime _tctime64
#define _tctime_s _tctime64_s
#define _tutime _tutime64
#define gmtime _gmtime64
#endif

View file

@ -0,0 +1,30 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/clock.c
* PURPOSE: Implementation of clock()
* PROGRAMER: Timo Kreuzer
*/
#include <precomp.h>
ULARGE_INTEGER g_StartupTime;
void
initclock(void)
{
GetSystemTimeAsFileTime((FILETIME*)&g_StartupTime);
}
/******************************************************************************
* \name clock
* \brief Returns the current process's elapsed time.
*/
clock_t
clock(void)
{
ULARGE_INTEGER Time;
GetSystemTimeAsFileTime((FILETIME*)&Time);
Time.QuadPart -= g_StartupTime.QuadPart;
return FileTimeToUnixTime((FILETIME*)&Time, NULL);
};

View file

@ -0,0 +1,52 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ctime.c
* PURPOSE: Implementation of ctime, _ctime_s
* PROGRAMERS: Timo Kreuzer
*/
#define MINGW_HAS_SECURE_API 1
#include <tchar.h>
#include <time.h>
#include "bitsfixup.h"
#define EINVAL -1
/******************************************************************************
* \name _tctime_s
* \brief Converts a time_t value into a string.
* \param buffer Buffer that receives the string (26 characters).
* \param numberOfElements Size of the buffer in characters.
* \param time Pointer to the UTC time.
*/
errno_t
_tctime_s(_TCHAR *buffer, size_t numberOfElements, const time_t *time)
{
struct tm _tm;
if (localtime_s(&_tm, time) == EINVAL)
{
return EINVAL;
}
return _tasctime_s(buffer, numberOfElements, &_tm);
}
/******************************************************************************
* \name _tctime
* \brief Converts a time_t value into a string and returns a pointer to it.
* \param time Pointer to the UTC time.
* \remarks The string is stored in thread local buffer, shared between
* ctime, gmtime and localtime (both 32 and 64 bit versions).
*/
_TCHAR *
_tctime(const time_t *ptime)
{
struct tm *ptm = localtime(ptime);
if (!ptm)
{
return 0;
}
return _tasctime(ptm);
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ctime32.c
* PURPOSE: Implementation of _ctime32()
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "ctime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ctime64.c
* PURPOSE: Implementation of _ctime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "ctime.c"

View file

@ -0,0 +1,23 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/difftime.c
* PURPOSE: Implementation of difftime
* PROGRAMERS: Timo Kreuzer
*/
#include <time.h>
#include "bitsfixup.h"
/**
* \name difftime
* \brief Retrurns the difference between two time_t values in seconds.
* \param time1 Beginning time.
* \param time2 Ending time.
*/
double
difftime(
time_t time1, /**< Beginning time */
time_t time2) /**< Ending time */
{
return (double)(time1 - time2);
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/difftime32.c
* PURPOSE: Implementation of _difftime32
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "difftime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/difftime64.c
* PURPOSE: Implementation of _difftime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "difftime.c"

View file

@ -0,0 +1,61 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ftime.c
* PURPOSE: Deprecated BSD library call
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
#include <sec_api/time_s.h>
#include <sys/timeb.h>
#include "bitsfixup.h"
/******************************************************************************
* \name _ftime_s
* \brief Get the current time.
* \param [out] ptimeb Pointer to a structure of type struct _timeb that
* recieves the current time.
* \sa http://msdn.microsoft.com/en-us/library/95e68951.aspx
*/
errno_t
_ftime_s(struct _timeb *ptimeb)
{
DWORD ret;
TIME_ZONE_INFORMATION TimeZoneInformation;
FILETIME SystemTime;
/* Validate parameters */
if (!ptimeb)
{
_invalid_parameter(0,
0,//__FUNCTION__,
_CRT_WIDE(__FILE__),
__LINE__,
0);
return EINVAL;
}
ret = GetTimeZoneInformation(&TimeZoneInformation);
ptimeb->dstflag = (ret == TIME_ZONE_ID_DAYLIGHT) ? 1 : 0;
ptimeb->timezone = TimeZoneInformation.Bias;
GetSystemTimeAsFileTime(&SystemTime);
ptimeb->time = FileTimeToUnixTime(&SystemTime, &ptimeb->millitm);
return 0;
}
/******************************************************************************
* \name _ftime
* \brief Get the current time.
* \param [out] ptimeb Pointer to a structure of type struct _timeb that
* recieves the current time.
* \note This function is for compatability and simply calls the secure
* version _ftime_s().
* \sa http://msdn.microsoft.com/en-us/library/z54t9z5f.aspx
*/
void
_ftime(struct _timeb *ptimeb)
{
_ftime_s(ptimeb);
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ftime32.c
* PURPOSE: Implementation of _ftime32
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "ftime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ftime64.c
* PURPOSE: Implementation of _ftime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "ftime.c"

View file

@ -0,0 +1,61 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/futime.c
* PURPOSE: Implementation of _futime
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
#include <time.h>
#include <sys/utime.h>
#include "bitsfixup.h"
HANDLE fdtoh(int fd);
/******************************************************************************
* \name _futime
* \brief Set a files modification time.
* \param [out] ptimeb Pointer to a structure of type struct _timeb that
* recieves the current time.
* \sa http://msdn.microsoft.com/en-us/library/95e68951.aspx
*/
int
_futime(int fd, struct _utimbuf *filetime)
{
HANDLE handle;
FILETIME at, wt;
handle = fdtoh(fd);
if (handle == INVALID_HANDLE_VALUE)
{
return -1;
}
if (!filetime)
{
time_t currTime;
_time(&currTime);
RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at);
wt = at;
}
else
{
RtlSecondsSince1970ToTime(filetime->actime, (LARGE_INTEGER *)&at);
if (filetime->actime == filetime->modtime)
{
wt = at;
}
else
{
RtlSecondsSince1970ToTime(filetime->modtime, (LARGE_INTEGER *)&wt);
}
}
if (!SetFileTime(handle, NULL, &at, &wt))
{
__set_errno(GetLastError());
return -1 ;
}
return 0;
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/futime32.c
* PURPOSE: Implementation of _futime32
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "futime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/futime64.c
* PURPOSE: Implementation of _futime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "futime.c"

View file

@ -0,0 +1,146 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/gmtime.c
* PURPOSE: Implementation of gmtime, _gmtime32, _gmtime64
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
unsigned int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
unsigned int g_lpmonthdays[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
struct tm *
_gmtime_worker(struct tm *ptm, __time64_t time, int do_dst)
{
unsigned int days, daystoyear, dayinyear, leapdays, leapyears, years, month;
unsigned int secondinday, secondinhour;
unsigned int *padays;
if (time < 0)
{
return 0;
}
/* Divide into date and time */
days = time / SECONDSPERDAY;
secondinday = time % SECONDSPERDAY;
/* Shift to days from 1.1.1601 */
days += DIFFDAYS;
/* Calculate leap days passed till today */
leapdays = leapdays_passed(days);
/* Calculate number of full leap years passed */
leapyears = leapyears_passed(days);
/* Are more leap days passed than leap years? */
if (leapdays > leapyears)
{
/* Yes, we're in a leap year */
padays = g_lpmonthdays;
}
else
{
/* No, normal year */
padays = g_monthdays;
}
/* Calculate year */
years = (days - leapdays) / 365;
ptm->tm_year = years - 299;
/* Calculate number of days till 1.1. of this year */
daystoyear = years * 365 + leapyears;
/* Calculate the day in this year */
dayinyear = days - daystoyear;
/* Shall we do DST corrections? */
ptm->tm_isdst = 0;
if (do_dst)
{
unsigned int yeartime = dayinyear * SECONDSPERDAY + secondinday ;
if (yeartime >= dst_begin && yeartime <= dst_end) // FIXME! DST in winter
{
time -= _dstbias;
days = time / SECONDSPERDAY + DIFFDAYS;
dayinyear = days - daystoyear;
ptm->tm_isdst = 1;
}
}
ptm->tm_yday = dayinyear;
/* dayinyear < 366 => terminates with i <= 11 */
for (month = 0; dayinyear >= padays[month+1]; month++)
;
/* Set month and day in month */
ptm->tm_mon = month;
ptm->tm_mday = 1 + dayinyear - padays[month];
/* Get weekday */
ptm->tm_wday = (days + 4) % 7;
/* Calculate hour and second in hour */
ptm->tm_hour = secondinday / SECONDSPERHOUR;
secondinhour = secondinday % SECONDSPERHOUR;
/* Calculate minute and second */
ptm->tm_min = secondinhour / 60;
ptm->tm_sec = secondinhour % 60;
return ptm;
}
/******************************************************************************
* \name _gmtime64
* \brief
* \param ptime Pointer to a variable of type __time64_t containing the time.
*/
struct tm *
_gmtime64(const __time64_t * ptime)
{
PTHREADDATA pThreadData;
struct tm *ptm;
__time64_t time = *ptime;
/* Validate parameters */
if (time < 0)
{
return 0;
}
/* Get pointer to TLS tm buffer */
pThreadData = GetThreadData();
ptm = &pThreadData->tmbuf;
/* Use _gmtime_worker to do the ral work */
return _gmtime_worker(ptm, time, 0);
}
/******************************************************************************
* \name _gmtime32
* \brief
* \param ptime Pointer to a variable of type __time32_t containing the time.
*/
struct tm *
_gmtime32(const __time32_t * ptime)
{
__time64_t time64 = (__time64_t)*ptime;
return _gmtime64(&time64);
}
/******************************************************************************
* \name gmtime
* \brief
* \param ptime Pointer to a variable of type time_t containing the time.
*/
struct tm *
gmtime(const time_t * ptime)
{
__time64_t time64 = (__time64_t)*ptime;
return _gmtime64(&time64);
}

View file

@ -0,0 +1,65 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/localtime.c
* PURPOSE: Implementation of localtime, localtime_s
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
#include <time.h>
#include "bitsfixup.h"
errno_t
localtime_s(struct tm* _tm, const time_t *ptime)
{
/* Validate parameters */
if (!_tm || !ptime)
{
_invalid_parameter(NULL,
0,//__FUNCTION__,
_CRT_WIDE(__FILE__),
__LINE__,
0);
return EINVAL;
}
return 0;
}
extern char _tz_is_set;
struct tm *
localtime(const time_t *ptime)
{
time_t time = *ptime;
struct tm * ptm;
/* Check for invalid time value */
if (time < 0)
{
return 0;
}
/* Never without */
if (!_tz_is_set)
_tzset();
/* Check for overflow */
/* Correct for timezone */
time -= _timezone;
#if 0
/* Correct for daylight saving */
if (_isdstime(time))
{
ptm->tm_isdst = 1;
time -= _dstbias;
}
#endif
ptm = gmtime(&time);
return ptm;
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/localtime32.c
* PURPOSE: Implementation of _localtime32
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "localtime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/localtime64.c
* PURPOSE: Implementation of _localtime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "localtime.c"

View file

@ -0,0 +1,124 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/mktime.c
* PURPOSE: Implementation of mktime, _mkgmtime
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
time_t
mktime_worker(struct tm * ptm, int utc)
{
return 0;
}
/* 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;
*/
static int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
/**
* \name _mkgmtime
*
*/
time_t
_mkgmtime(struct tm *ptm)
{
struct tm *ptm2;
time_t time;
int mons, years, leapyears;
/* 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 */
ptm2 = gmtime(&time);
if (!ptm2)
{
return -1;
}
*ptm = *ptm2;
return time;
}
time_t
mktime(struct tm *ptm)
{
time_t time;
/* Convert the time as if it was UTC */
time = _mkgmtime(ptm);
/* Apply offset */
if (time != -1)
{
time += _timezone;
}
return time;
}

View file

@ -0,0 +1,23 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/msvcrt/time/strtime.c
* PURPOSE: Fills a buffer with a formatted date representation
* PROGRAMER: Ariadne
* UPDATE HISTORY:
* 28/12/98: Created
*/
#include <precomp.h>
/*
* @implemented
*/
char* _strdate(char* date)
{
static const char format[] = "MM'/'dd'/'yy";
GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
return date;
}

View file

@ -0,0 +1,12 @@
#include <precomp.h>
#include <tchar.h>
size_t
_tcsftime(_TCHAR *strDest,
size_t maxsize,
const _TCHAR *format,
const struct tm *timeptr)
{
return 0;
}

View file

@ -0,0 +1,22 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/msvcrt/time/strtime.c
* PURPOSE: Fills a buffer with a formatted time representation
* PROGRAMER: Ariadne
* UPDATE HISTORY:
* 28/12/98: Created
*/
#include <precomp.h>
/*
* @implemented
*/
char* _strtime(char* time)
{
static const char format[] = "HH':'mm':'ss";
GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
return time;
}

View file

@ -0,0 +1,24 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/sdk/crt/time/time.c
* PURPOSE: Implementation of _time (_tim32, _tim64)
* PROGRAMER: Timo Kreuzer
*/
#include <precomp.h>
#include <time.h>
#include "bitsfixup.h"
time_t _time(time_t* ptime)
{
FILETIME SystemTime;
time_t time = 0;
if (ptime)
{
GetSystemTimeAsFileTime(&SystemTime);
time = FileTimeToUnixTime(&SystemTime, NULL);
*ptime = time;
}
return time;
}

View file

@ -0,0 +1,3 @@
#define _USE_EXPLITIT_32BIT_TIME
#include "time.c"

View file

@ -0,0 +1,3 @@
#define _USE_EXPLITIT_64BIT_TIME
#include "time.c"

View file

@ -0,0 +1,216 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/timezone.c
* PURPOSE: Implementation of time zone functions
* PROGRAMERS: Timo Kreuzer
*/
#include "precomp.h"
char _tz_is_set = 0;
/* buffers must hold 64 characters! */
static char tz_name[64] = "PST";
static char tz_dst_name[64] = "PDT";
long dst_begin = 0;
long dst_end = 0;
/******************************************************************************
* \var _tzname
*/
char * _tzname[2] = {
tz_name,
tz_dst_name,
};
/******************************************************************************
* \var _daylight
*/
int _daylight = 1;
/******************************************************************************
* \name __p__daylight
* \brief Returns a pointer to the _daylight variable;
*/
void *
__p__daylight(void)
{
return &_daylight;
}
/******************************************************************************
* \var _timezone
* \brief
*/
long _timezone = 28800;
/******************************************************************************
* \name __p__timezone
* \brief Returns a pointer to the _timezone variable;
*/
long *
__p__timezone(void)
{
return &_timezone;
}
/******************************************************************************
* \var _dstbias
* \brief
*/
long _dstbias = 0;
/******************************************************************************
* \name __p__dstbias
* \brief Returns a pointer to the _dstbias variable;
*/
long *
__p__dstbias(void)
{
return &_dstbias;
}
/******************************************************************************
* \name __p__tzname
* \brief Returns a pointer to the _tzname buffer;
*/
char **
__p__tzname(void)
{
return _tzname;
}
/******************************************************************************
* \name _tzset
* \brief Initializes the variables _daylight, _timezone, and _tzname from the
* "TZ" environment variable if available or else by calling
* GetTimeZoneInformation.
* \sa http://msdn.microsoft.com/en-us/library/90s5c885.aspx
*/
void
_tzset(void)
{
const char * str;
if (_tz_is_set)
{
return;
}
/* Try to read the timezone from environment */
str = getenv("TZ");
if (str && str[0] != 0)
{
long hour = 0, min = 0, sec = 0;
int len = strnlen(str, 16);
int sign = 1;
dst_begin = 0;
for (;;)
{
/* Copy timezone name */
strncpy(tz_name, str, 3);
str += 3;
len -= 3;
if (len < 1) break;
if (*str == '+' || *str == '-')
{
sign = *str == '-' ? -1 : 1;
str++;
len--;
}
if (len < 1) break;
hour = atol(str);
while (*str != 0 && *str != ':') str++;
if (*str == 0) break;
min = atol(++str);
while (*str != 0 && *str != ':') str++;
if (*str == 0) break;
sec = atol(++str);
while (*str != 0 && *str <= '9') str++;
if (*str == 0) break;
/* Copy DST name */
strncpy(tz_dst_name, str, 3);
// FIXME: set dst_begin etc
/* We are finished */
break;
}
_timezone = sign * (((hour * 60) + min) * 60 + sec);
}
else
{
TIME_ZONE_INFORMATION tzi;
DWORD ret;
ret = GetTimeZoneInformation(&tzi);
if (ret == TIME_ZONE_ID_INVALID)
{
return;
}
ret = WideCharToMultiByte(CP_ACP,
0,
tzi.StandardName,
-1,
tz_name,
sizeof(tz_name),
NULL,
NULL);
ret = WideCharToMultiByte(CP_ACP,
0,
tzi.DaylightName,
-1,
tz_dst_name,
sizeof(tz_dst_name),
NULL,
NULL);
_timezone = tzi.Bias * 60;
if (tzi.DaylightDate.wMonth)
{
struct tm _tm;
_daylight = 1;
_dstbias = (tzi.DaylightBias - tzi.StandardBias) * 60;
_tm.tm_year = 70;
_tm.tm_mon = tzi.DaylightDate.wMonth - 1;
_tm.tm_mday = tzi.DaylightDate.wDay;
_tm.tm_hour = tzi.DaylightDate.wHour;
_tm.tm_min = tzi.DaylightDate.wMinute;
_tm.tm_sec = tzi.DaylightDate.wSecond;
dst_begin = _mkgmtime(&_tm);
_tm.tm_mon = tzi.StandardDate.wMonth - 1;
_tm.tm_mday = tzi.StandardDate.wDay;
_tm.tm_hour = tzi.StandardDate.wHour;
_tm.tm_min = tzi.StandardDate.wMinute;
_tm.tm_sec = tzi.StandardDate.wSecond;
dst_end = _mkgmtime(&_tm);
}
else
{
_daylight = 0;
_dstbias = 0;
}
}
_tz_is_set = 1;
}

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/ctime.c
* PURPOSE: Implementation of ctime
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>

View file

@ -0,0 +1,25 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/utime.c
* PURPOSE: Implementation of utime, _wutime
* PROGRAMERS: Timo Kreuzer
*/
#include <precomp.h>
#include <tchar.h>
#include <sys/utime.h>
#include "bitsfixup.h"
int
_tutime(const _TCHAR* path, struct _utimbuf *t)
{
int fd = _topen(path, _O_WRONLY | _O_BINARY);
if (fd > 0)
{
int retVal = _futime(fd, t);
_close(fd);
return retVal;
}
return -1;
}

View file

@ -0,0 +1,10 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/utime32.c
* PURPOSE: Implementation of _utime32
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_32BIT_TIME
#include "utime.c"

View file

@ -0,0 +1,9 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/utime64.c
* PURPOSE: Implementation of _utime64
* PROGRAMERS: Timo Kreuzer
*/
#define _USE_EXPLITIT_64BIT_TIME
#include "utime.c"

View file

@ -0,0 +1,11 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wasctime.c
* PURPOSE: Implementation of _wasctime
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#include "asctime.c"

View file

@ -0,0 +1,11 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wcsftime.c
* PURPOSE: Implementation of _wcsftime
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#include "strftime.c"

View file

@ -0,0 +1,11 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wctime.c
* PURPOSE: Implementation of _wctime
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#include "ctime.c"

View file

@ -0,0 +1,12 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wctime32.c
* PURPOSE: Implementation of _wctime32
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#define _USE_EXPLITIT_32BIT_TIME
#include "ctime.c"

View file

@ -0,0 +1,12 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wctime64.c
* PURPOSE: Implementation of _Wctime64
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#define _USE_EXPLITIT_64BIT_TIME
#include "ctime.c"

View file

@ -0,0 +1,23 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/msvcrt/time/strtime.c
* PURPOSE: Fills a buffer with a formatted date representation
* PROGRAMER: Ariadne
* UPDATE HISTORY:
* 28/12/98: Created
*/
#include <precomp.h>
/*
* @implemented
*/
wchar_t* _wstrdate(wchar_t* date)
{
static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
return date;
}

View file

@ -0,0 +1,22 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/msvcrt/time/strtime.c
* PURPOSE: Fills a buffer with a formatted time representation
* PROGRAMER: Ariadne
* UPDATE HISTORY:
* 28/12/98: Created
*/
#include <precomp.h>
/*
* @implemented
*/
wchar_t* _wstrtime(wchar_t* time)
{
static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
return time;
}

View file

@ -0,0 +1,11 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wutime.c
* PURPOSE: Implementation of _wutime
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#include "utime.c"

View file

@ -0,0 +1,12 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wutime32.c
* PURPOSE: Implementation of _wutime32
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#define _USE_EXPLITIT_32BIT_TIME
#include "utime.c"

View file

@ -0,0 +1,12 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CRT library
* FILE: lib/sdk/crt/time/wutime64.c
* PURPOSE: Implementation of _wutime64
* PROGRAMERS: Timo Kreuzer
*/
#define UNICODE
#define _UNICODE
#define _USE_EXPLITIT_64BIT_TIME
#include "utime.c"