Sweden-Number/dlls/kernel/time.c

601 lines
20 KiB
C
Raw Normal View History

/*
* Win32 kernel functions
*
* Copyright 1995 Martin von Loewis and Cameron Heide
*/
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/times.h>
#include "file.h"
#include "ntddk.h"
#include "winerror.h"
#include "winnls.h"
#include "wine/unicode.h"
#include "debugtools.h"
DEFAULT_DEBUG_CHANNEL(win32);
/* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
#define SETTIME_MAX_ADJUST 120
/* This structure is used to store strings that represent all of the time zones
in the world. (This is used to help GetTimeZoneInformation)
*/
struct tagTZ_INFO
{
const char *psTZFromUnix;
WCHAR psTZWindows[32];
int bias;
int dst;
};
static const struct tagTZ_INFO TZ_INFO[] =
{
{"MHT",
{'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-720, 0},
{"SST",
{'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
660, 0},
{"HST",
{'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
600, 0},
{"AKDT",
{'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
480, 1},
{"PDT",
{'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
420, 1},
{"MST",
{'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
420, 0},
{"MDT",
{'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
360, 1},
{"CST",
{'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
360, 0},
{"CDT",
{'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
300, 1},
{"COT",
{'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
300, 0},
{"EDT",
{'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
240, 1},
{"EST",
{'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
300, 0},
{"ADT",
{'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
180, 1},
{"VET",
{'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
240, 0},
{"CLT",
{'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
240, 0},
{"NDT",
{'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
150, 1},
{"BRT",
{'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
180, 0},
{"ART",
{'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
180, 0},
{"WGST",
{'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
120, 1},
{"GST",
{'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
120, 0},
{"AZOST",
{'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
0, 1},
{"CVT",
{'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
60, 0},
{"WET",
{'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
0, 0},
{"BST",
{'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-60, 1},
{"GMT",
{'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
0, 0},
{"CEST",
{'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-120, 1},
{"WAT",
{'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-60, 0},
{"EEST",
{'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-180, 1},
{"EET",
{'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-120, 0},
{"CAT",
{'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-120, 0},
{"IST",
{'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-120, 0},
{"ADT",
{'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-240, 1},
{"AST",
{'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-180, 0},
{"MSD",
{'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-240, 1},
{"EAT",
{'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-180, 0},
{"IRST",
{'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-270, 1},
{"GST",
{'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-240, 0},
{"AZST",
{'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-300, 1},
{"AFT",
{'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-270, 0},
{"YEKST",
{'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-360, 1},
{"PKT",
{'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-300, 0},
{"IST",
{'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-330, 0},
{"NPT",
{'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-345, 0},
{"ALMST",
{'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-420, 1},
{"BDT",
{'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-360, 0},
{"LKT",
{'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-360, 0},
{"MMT",
{'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-390, 0},
{"ICT",
{'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-420, 0},
{"KRAST",
{'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-480, 1},
{"CST",
{'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-480, 0},
{"IRKST",
{'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-540, 1},
{"SGT",
{'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-480, 0},
{"WST",
{'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-480, 0},
{"JST",
{'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-540, 0},
{"KST",
{'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-540, 0},
{"YAKST",
{'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-600, 1},
{"CST",
{'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-570, 0},
{"EST",
{'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-600, 0},
{"GST",
{'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-600, 0},
{"VLAST",
{'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-660, 1},
{"MAGST",
{'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-720, 1},
{"NZST",
{'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-720, 0},
{"FJT",
{'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-720, 0},
{"TOT",
{'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
-780, 0}
};
/* TIME_GetTZAsStr: helper function that returns the given timezone as a string.
This could be done with a hash table instead of merely iterating through
a table, however with the small amount of entries (60 or so) I didn't think
it was worth it. */
static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
{
char psTZName[7];
struct tm *ptm = localtime(&utc);
int i;
if (!strftime (psTZName, 7, "%Z", ptm))
return (NULL);
for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
{
if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
TZ_INFO[i].bias == bias &&
TZ_INFO[i].dst == dst
)
return TZ_INFO[i].psTZWindows;
}
return (NULL);
}
/* TIME_GetBias: helper function calculates delta local time from UTC */
static int TIME_GetBias( time_t utc, int *pdaylight)
{
struct tm *ptm = localtime(&utc);
*pdaylight = ptm->tm_isdst; /* daylight for local timezone */
ptm = gmtime(&utc);
ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
return (int)(utc-mktime(ptm));
}
/***********************************************************************
* SetLocalTime (KERNEL32.@)
*
2000-12-20 00:33:03 +01:00
* Sets the local time using current time zone and daylight
* savings settings.
*
* RETURNS
*
* True if the time was set, false if the time was invalid or the
* necessary permissions were not held.
*/
2001-02-12 04:49:57 +01:00
BOOL WINAPI SetLocalTime(
const SYSTEMTIME *systime) /* [in] The desired local time. */
{
struct timeval tv;
struct tm t;
time_t sec;
time_t oldsec=time(NULL);
int err;
/* get the number of seconds */
t.tm_sec = systime->wSecond;
t.tm_min = systime->wMinute;
t.tm_hour = systime->wHour;
t.tm_mday = systime->wDay;
t.tm_mon = systime->wMonth - 1;
t.tm_year = systime->wYear - 1900;
t.tm_isdst = -1;
sec = mktime (&t);
/* set the new time */
tv.tv_sec = sec;
tv.tv_usec = systime->wMilliseconds * 1000;
/* error and sanity check*/
if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
err = 1;
SetLastError(ERROR_INVALID_PARAMETER);
} else {
err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
if(err == 0)
return TRUE;
SetLastError(ERROR_PRIVILEGE_NOT_HELD);
}
ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
systime->wMinute, systime->wSecond,
sec-oldsec, err == -1 ? "No Permission" :
sec==(time_t)-1 ? "" : "is too large." );
return FALSE;
}
/***********************************************************************
* GetSystemTimeAdjustment (KERNEL32.@)
*
2000-12-20 00:33:03 +01:00
* Indicates the period between clock interrupt and the amount the clock
* is adjusted each interrupt so as to keep it insync with an external source.
*
* RETURNS
*
* Always returns true.
*
* BUGS
*
* Only the special case of disabled time adjustments is supported.
* (also the signature is wrong it should have a return type of BOOL)
*/
2000-12-20 00:33:03 +01:00
DWORD WINAPI GetSystemTimeAdjustment(
2001-02-12 04:49:57 +01:00
LPDWORD lpTimeAdjustment, /* [out] The clock adjustment per interupt in 100's of nanoseconds. */
LPDWORD lpTimeIncrement, /* [out] The time between clock interupts in 100's of nanoseconds. */
LPBOOL lpTimeAdjustmentDisabled) /* [out] The clock synchonisation has been disabled. */
{
*lpTimeAdjustment = 0;
*lpTimeIncrement = 0;
*lpTimeAdjustmentDisabled = TRUE;
return TRUE;
}
/***********************************************************************
* SetSystemTime (KERNEL32.@)
2000-12-20 00:33:03 +01:00
*
* Sets the system time (utc).
*
* RETURNS
*
* True if the time was set, false if the time was invalid or the
* necessary permissions were not held.
*/
2000-12-20 00:33:03 +01:00
BOOL WINAPI SetSystemTime(
2001-02-12 04:49:57 +01:00
const SYSTEMTIME *systime) /* [in] The desired system time. */
{
struct timeval tv;
struct timezone tz;
struct tm t;
time_t sec, oldsec;
int dst, bias;
int err;
/* call gettimeofday to get the current timezone */
gettimeofday(&tv, &tz);
oldsec=tv.tv_sec;
/* get delta local time from utc */
bias=TIME_GetBias(oldsec,&dst);
/* get the number of seconds */
t.tm_sec = systime->wSecond;
t.tm_min = systime->wMinute;
t.tm_hour = systime->wHour;
t.tm_mday = systime->wDay;
t.tm_mon = systime->wMonth - 1;
t.tm_year = systime->wYear - 1900;
t.tm_isdst = dst;
sec = mktime (&t);
/* correct for timezone and daylight */
sec += bias;
/* set the new time */
tv.tv_sec = sec;
tv.tv_usec = systime->wMilliseconds * 1000;
/* error and sanity check*/
if( sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST ){
err = 1;
SetLastError(ERROR_INVALID_PARAMETER);
} else {
err=settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
if(err == 0)
return TRUE;
SetLastError(ERROR_PRIVILEGE_NOT_HELD);
}
ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
systime->wYear, systime->wMonth, systime->wDay, systime->wHour,
systime->wMinute, systime->wSecond,
sec-oldsec, err == -1 ? "No Permission" :
sec==(time_t)-1 ? "" : "is too large." );
return FALSE;
}
/***********************************************************************
* GetTimeZoneInformation (KERNEL32.@)
2000-12-20 00:33:03 +01:00
*
* Fills in the a time zone information structure with values based on
* the current local time.
*
* RETURNS
*
* The daylight savings time standard or TIME_ZONE_ID_INVALID if the call failed.
*/
2000-12-20 00:33:03 +01:00
DWORD WINAPI GetTimeZoneInformation(
2001-02-12 04:49:57 +01:00
LPTIME_ZONE_INFORMATION tzinfo) /* [out] The time zone structure to be filled in. */
{
time_t gmt;
int bias, daylight;
const WCHAR *psTZ;
memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
gmt = time(NULL);
bias=TIME_GetBias(gmt,&daylight);
tzinfo->Bias = -bias / 60;
tzinfo->StandardBias = 0;
tzinfo->DaylightBias = -60;
psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
return TIME_ZONE_ID_STANDARD;
}
/***********************************************************************
* SetTimeZoneInformation (KERNEL32.@)
2000-12-20 00:33:03 +01:00
*
* Set the local time zone with values based on the time zone structure.
*
* RETURNS
*
* True on successful setting of the time zone.
*
* BUGS
*
* Use the obsolete unix timezone structure and tz_dsttime member.
*/
2000-12-20 00:33:03 +01:00
BOOL WINAPI SetTimeZoneInformation(
2001-02-12 04:49:57 +01:00
const LPTIME_ZONE_INFORMATION tzinfo) /* [in] The new time zone. */
{
struct timezone tz;
tz.tz_minuteswest = tzinfo->Bias;
#ifdef DST_NONE
tz.tz_dsttime = DST_NONE;
#else
tz.tz_dsttime = 0;
#endif
return !settimeofday(NULL, &tz);
}
/***********************************************************************
* SystemTimeToTzSpecificLocalTime (KERNEL32.@)
2000-12-20 00:33:03 +01:00
*
* Converts the system time (utc) to the local time in the specified time zone.
*
* RETURNS
*
* Returns true when the local time was calculated.
*
* BUGS
*
* Does not handle daylight savings time adjustments correctly.
*/
BOOL WINAPI SystemTimeToTzSpecificLocalTime(
2001-02-12 04:49:57 +01:00
LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
LPSYSTEMTIME lpLocalTime) /* [out] The local time in the time zone. */
{
FIXME(":stub\n");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
/***********************************************************************
* GetSystemTimeAsFileTime (KERNEL32.@)
2000-12-20 00:33:03 +01:00
*
* Fills in a file time structure with the current time in UTC format.
*/
2000-12-20 00:33:03 +01:00
VOID WINAPI GetSystemTimeAsFileTime(
2001-02-12 04:49:57 +01:00
LPFILETIME time) /* [out] The file time struct to be filled with the system time. */
{
NtQuerySystemTime( (LARGE_INTEGER *)time );
}
/*********************************************************************
2000-12-20 00:33:03 +01:00
* TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
*
* Used by GetProcessTimes to convert clock_t into FILETIME.
*
* Differences to UnixTimeToFileTime:
* 1) Divided by CLK_TCK
* 2) Time is relative. There is no 'starting date', so there is
* no need in offset correction, like in UnixTimeToFileTime
*/
static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
{
LONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
((LARGE_INTEGER *)filetime)->QuadPart = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
}
/*********************************************************************
* GetProcessTimes (KERNEL32.@)
*
2000-12-20 00:33:03 +01:00
* Returns the user and kernel execution times of a process,
* along with the creation and exit times if known.
*
* olorin@fandra.org:
* Would be nice to subtract the cpu time, used by Wine at startup.
* Also, there is a need to separate times used by different applications.
*
* RETURNS
*
* Always returns true.
*
* BUGS
*
* lpCreationTime, lpExitTime are NOT INITIALIZED.
*/
2000-12-20 00:33:03 +01:00
BOOL WINAPI GetProcessTimes(
2001-02-12 04:49:57 +01:00
HANDLE hprocess, /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
LPFILETIME lpExitTime, /* [out] The exit time of the process if exited. */
LPFILETIME lpKernelTime, /* [out] The time spent in kernal routines in 100's of nanoseconds. */
LPFILETIME lpUserTime) /* [out] The time spent in user routines in 100's of nanoseconds. */
{
struct tms tms;
times(&tms);
TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
return TRUE;
}
/*********************************************************************
* GetCalendarInfoA (KERNEL32.@)
*
*/
int WINAPI GetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType,
LPSTR lpCalData, int cchData, LPDWORD lpValue)
{
FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): stub\n",
Locale, Calendar, CalType, lpCalData, cchData, lpValue);
return 0;
}
/*********************************************************************
* GetCalendarInfoW (KERNEL32.@)
*
*/
int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
LPWSTR lpCalData, int cchData, LPDWORD lpValue)
{
FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): stub\n",
Locale, Calendar, CalType, lpCalData, cchData, lpValue);
return 0;
}
/*********************************************************************
* SetCalendarInfoA (KERNEL32.@)
*
*/
int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
{
FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
Locale, Calendar, CalType, debugstr_a(lpCalData));
return 0;
}
/*********************************************************************
* SetCalendarInfoW (KERNEL32.@)
*
*/
int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
{
FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
Locale, Calendar, CalType, debugstr_w(lpCalData));
return 0;
}