📄 time.c
字号:
TimeToDaysAndFraction(lpft, &Days, &Milliseconds );
lpst->wDayOfWeek = (WORD)((Days + WEEKDAY_OF_1601) % 7);
Years = ElapsedDaysToYears(Days);
Days = Days - ElapsedYearsToDays(Years);
if (IsLeapYear(Years + 1)) {
lpst->wMonth = (WORD)(LeapYearDayToMonth[Days] + 1);
Days = Days - LeapYearDaysBeforeMonth[lpst->wMonth-1];
} else {
lpst->wMonth = (WORD)(NormalYearDayToMonth[Days] + 1);
Days = Days - NormalYearDaysBeforeMonth[lpst->wMonth-1];
}
Seconds = Milliseconds/1000;
lpst->wMilliseconds = (WORD)(Milliseconds % 1000);
Minutes = Seconds / 60;
lpst->wSecond = (WORD)(Seconds % 60);
lpst->wHour = (WORD)(Minutes / 60);
lpst->wMinute = (WORD)(Minutes % 60);
lpst->wYear = (WORD)(Years + 1601);
lpst->wDay = (WORD)(Days + 1);
return TRUE;
}
/*
@doc BOTH EXTERNAL
@func BOOL | FileTimeToLocalFileTime | Converts a file time based on the
Coordinated Universal Time (UTC) to a local file time.
@parm CONST FILETIME * | lpFileTime | address of UTC file time to convert
@parm LPFILETIME | lpLocalFileTime | address of converted file time
@comm Follows the Win32 reference description without restrictions or modifications.
*/
BOOL WINAPI FileTimeToLocalFileTime(const FILETIME * lpft, LPFILETIME lpftLocal) {
FILETIME bias;
bias.dwHighDateTime = 0;
if (UserKInfo[KINX_TIMEZONEBIAS] < 0)
bias.dwLowDateTime = - UserKInfo[KINX_TIMEZONEBIAS];
else
bias.dwLowDateTime = UserKInfo[KINX_TIMEZONEBIAS];
mul64_32_64(&bias,600000000L,&bias);
if (UserKInfo[KINX_TIMEZONEBIAS] < 0)
add64_64_64(lpft,&bias,lpftLocal);
else
sub64_64_64(lpft,&bias,lpftLocal);
return TRUE;
}
/*
@doc BOTH EXTERNAL
@func BOOL | LocalFileTimeToFileTime | Converts a local file time to a file time based
on the Coordinated Universal Time (UTC).
@parm CONST FILETIME * | lpftLocal | address of local file time to convert
@parm LPFILETIME | lpft | address of converted file time
@comm Follows the Win32 reference description without restrictions or modifications.
*/
BOOL WINAPI LocalFileTimeToFileTime(const FILETIME *lpftLocal, LPFILETIME lpft) {
FILETIME bias;
bias.dwHighDateTime = 0;
if (UserKInfo[KINX_TIMEZONEBIAS] < 0)
bias.dwLowDateTime = - UserKInfo[KINX_TIMEZONEBIAS];
else
bias.dwLowDateTime = UserKInfo[KINX_TIMEZONEBIAS];
mul64_32_64(&bias,600000000L,&bias);
if (UserKInfo[KINX_TIMEZONEBIAS] < 0)
sub64_64_64(lpftLocal,&bias,lpft);
else
add64_64_64(lpftLocal,&bias,lpft);
return TRUE;
}
//
// This routine computes the difference between two FILETIMEs. It returns TRUE
// if the new time is greater than the old time, FALSE otherwise.
//
BOOL FileTimeDelta(FILETIME *pftOrig, FILETIME *pftNew, FILETIME *pftDelta)
{
ULARGE_INTEGER uliOrig, uliNew;
BOOL fForward;
// convert to ULARGE_INTEGER
uliOrig.LowPart = pftOrig->dwLowDateTime;
uliOrig.HighPart = pftOrig->dwHighDateTime;
uliNew.LowPart = pftNew->dwLowDateTime;
uliNew.HighPart = pftNew->dwHighDateTime;
// compute the delta
if(uliNew.QuadPart > uliOrig.QuadPart) {
fForward = TRUE;
uliNew.QuadPart -= uliOrig.QuadPart;
} else {
fForward = FALSE;
uliNew.QuadPart = uliOrig.QuadPart - uliNew.QuadPart;
}
// convert to FILETIME
pftDelta->dwLowDateTime = uliNew.LowPart;
pftDelta->dwHighDateTime = uliNew.HighPart;
return fForward;
}
/*
@doc BOTH EXTERNAL
@func VOID | GetSystemTime | Retrieves the current system date and time.
The system time is expressed in Coordinated Universal Time (UTC).
@parm LPSYSTEMTIME | lpst | address of system time structure
@comm Follows the Win32 reference description without restrictions or modifications.
*/
VOID WINAPI GetSystemTime (LPSYSTEMTIME lpSystemTime) {
FILETIME ft;
GetCurrentFT (&ft);
FileTimeToSystemTime(&ft,lpSystemTime);
}
/*
@doc BOTH EXTERNAL
@func BOOL | SetSystemTime | Sets the current system time and date. The system time
is expressed in Coordinated Universal Time (UTC).
@parm CONST SYSTEMTIME * | lpst | address of system time to set
@comm Follows the Win32 reference description without restrictions or modifications.
*/
BOOL WINAPI SetSystemTime (const SYSTEMTIME *lpSystemTime) {
SYSTEMTIME st;
FILETIME ft;
DWORD dwStatus = ERROR_INVALID_PARAMETER;
if (SystemTimeToFileTime(lpSystemTime,&ft) &&
FileTimeToLocalFileTime(&ft,&ft) &&
FileTimeToSystemTime(&ft,&st)) {
SYSTEMTIME stOrig;
FILETIME ftOrig;
BOOL fForward;
// get the time change direction
GetSystemTime(&stOrig);
if(SystemTimeToFileTime(&stOrig, &ftOrig) &&
FileTimeToLocalFileTime(&ftOrig, &ftOrig)) {
fForward = FileTimeDelta(&ftOrig, &ft, &ft);
} else {
fForward = FALSE;
ft.dwLowDateTime = 0;
ft.dwHighDateTime = 0;
}
// update the RTC
if(SetRealTime(&st)) {
dwStatus = ERROR_SUCCESS;
I_BatteryNotifyOfTimeChange(fForward, &ft);
}
}
// return an error code on failure
if(dwStatus != ERROR_SUCCESS) {
SetLastError(dwStatus);
return FALSE;
}
// if successful, broadcast a notification
CeEventHasOccurred(NOTIFICATION_EVENT_TIME_CHANGE, NULL);
RefreshKernelAlarm();
return TRUE;
}
/*
@doc BOTH EXTERNAL
@func VOID | GetLocalTime | Retrieves the current local date and time.
@parm LPSYSTEMTIME |lpst | address of system time structure
@comm Follows the Win32 reference description without restrictions or modifications.
*/
VOID WINAPI GetLocalTime (LPSYSTEMTIME lpSystemTime) {
FILETIME ft;
GetLocalTimeAsFileTime (&ft);
FileTimeToSystemTime(&ft,lpSystemTime);
}
/*
@doc BOTH EXTERNAL
@func BOOL | SetLocalTime | Sets the current local time and date.
@parm CONST SYSTEMTIME * | lpst | address of local time to set
@comm Follows the Win32 reference description without restrictions or modifications.
*/
BOOL WINAPI SetLocalTime (const SYSTEMTIME *lpSystemTime) {
SYSTEMTIME st;
FILETIME ft;
DWORD dwStatus = ERROR_INVALID_PARAMETER;
if (SystemTimeToFileTime(lpSystemTime,&ft) &&
FileTimeToSystemTime(&ft,&st)) {
SYSTEMTIME stOrig;
FILETIME ftOrig;
BOOL fForward;
// get the time change direction
GetSystemTime(&stOrig);
if(SystemTimeToFileTime(&stOrig, &ftOrig) &&
FileTimeToLocalFileTime(&ftOrig, &ftOrig)) {
fForward = FileTimeDelta(&ftOrig, &ft, &ft);
} else {
fForward = FALSE;
ft.dwLowDateTime = 0;
ft.dwHighDateTime = 0;
}
// update the RTC
if(SetRealTime(&st)) {
dwStatus = ERROR_SUCCESS;
I_BatteryNotifyOfTimeChange(fForward, &ft);
}
}
// return an error code on failure
if(dwStatus != ERROR_SUCCESS) {
SetLastError(dwStatus);
return FALSE;
}
// if successful, broadcast a notification
CeEventHasOccurred(NOTIFICATION_EVENT_TIME_CHANGE, NULL);
RefreshKernelAlarm();
return TRUE;
}
/*
@doc BOTH EXTERNAL
@func DWORD | GetTimeZoneInformation | Retrieves the current time-zone parameters.
These parameters control the translations between Coordinated Universal Time (UTC)
and local time.
@parm LPTIME_ZONE_INFORMATION | lptzi | address of time-zone settings
@devnote CHOOSE COMM TAG FOR ONLY ONE OF THE FOLLOWING:
@comm Follows the Win32 reference description with the following restrictions:
Always return TIME_ZONE_ID_UNKNOWN;
*/
#define TZIStrCpyW(D,S) memcpy(D,S,sizeof(S))
typedef struct tagTZREG {
LONG Bias;
LONG StandardBias;
LONG DaylightBias;
SYSTEMTIME StandardDate;
SYSTEMTIME DaylightDate;
} TZREG;
const TCHAR cszTimeZones[] = L"Time Zones";
void GetDefaultTimeZoneInformation (LPTIME_ZONE_INFORMATION lpTzi)
{
DWORD type, size1, size2;
HKEY hk1=0, hk2=0;
TZREG tzr;
WCHAR TimezoneName[32];
memset(lpTzi, 0, sizeof(TIME_ZONE_INFORMATION));
memset(&tzr, 0, sizeof(tzr));
size1 = sizeof(TimezoneName);
size2 = sizeof(tzr);
// see if we have a value "Default" under HKLM\Time Zones
if( (ERROR_SUCCESS==OpenKeyHKLM(L"Time Zones", &hk1)) &&
(ERROR_SUCCESS==GetKey(hk1, L"Default", &type, TimezoneName, &size1)) &&
(ERROR_SUCCESS==OpenKey(hk1, TimezoneName, &hk2)) &&
(ERROR_SUCCESS==GetKey(hk2, L"TZI", &type, &tzr, &size2)) )
{
// Read the value "TZI" and "Dlt" under HKLM\Time Zones\<time zone std name>
size1 = sizeof(lpTzi->StandardName);
GetKey(hk2,L"Std",&type,lpTzi->StandardName,&size1);
size1 = sizeof(lpTzi->DaylightName);
GetKey(hk2,L"Dlt",&type,lpTzi->DaylightName,&size1);
lpTzi->Bias = tzr.Bias;
lpTzi->StandardBias = tzr.StandardBias;
lpTzi->DaylightBias = tzr.DaylightBias;
lpTzi->StandardDate = tzr.StandardDate;
lpTzi->DaylightDate = tzr.DaylightDate;
}
else
{
// Default to Redmond, WA for now
lpTzi->Bias = 480;
TZIStrCpyW(lpTzi->StandardName,L"Pacific Standard Time");
lpTzi->StandardDate.wYear = 0;
lpTzi->StandardDate.wMonth = 10;
lpTzi->StandardDate.wDayOfWeek = 0;
lpTzi->StandardDate.wDay = 5;
lpTzi->StandardDate.wHour = 2;
lpTzi->StandardDate.wMinute = 0;
lpTzi->StandardDate.wSecond = 0;
lpTzi->StandardDate.wMilliseconds = 0;
lpTzi->StandardBias = 0;
TZIStrCpyW(lpTzi->DaylightName,L"Pacific Daylight Time");
lpTzi->DaylightDate.wYear = 0;
lpTzi->DaylightDate.wMonth = 4;
lpTzi->DaylightDate.wDayOfWeek = 0;
lpTzi->DaylightDate.wDay = 1;
lpTzi->DaylightDate.wHour = 2;
lpTzi->DaylightDate.wMinute = 0;
lpTzi->DaylightDate.wSecond = 0;
lpTzi->DaylightDate.wMilliseconds = 0;
lpTzi->DaylightBias = -60;
}
if(hk1) RegCloseKey(hk1);
if(hk2) RegCloseKey(hk2);
ASSERT(lpTzi->StandardName && lpTzi->DaylightName);
}
DWORD WINAPI GetTimeZoneInformation (LPTIME_ZONE_INFORMATION lpTzi)
{
DWORD type;
DWORD size = sizeof(TIME_ZONE_INFORMATION);
// check for existing time-zone struct
if (GetKeyHKLM(L"TimeZoneInformation",L"Time",&type,(LPBYTE)lpTzi,&size))
{
// fallback to default
GetDefaultTimeZoneInformation(lpTzi);
}
if (lpTzi->Bias + lpTzi->StandardBias == UserKInfo[KINX_TIMEZONEBIAS])
return TIME_ZONE_ID_STANDARD;
else if (lpTzi->Bias + lpTzi->DaylightBias == UserKInfo[KINX_TIMEZONEBIAS])
return TIME_ZONE_ID_DAYLIGHT;
return TIME_ZONE_ID_UNKNOWN;
}
BOOL IsValidTZISystemTime(const SYSTEMTIME *lpst) {
if (IsValidSystemTime(lpst))
return TRUE;
if (!lpst->wYear && (lpst->wDayOfWeek <= 6) && (lpst->wDay >= 1) && (lpst->wDay <= 5))
return TRUE;
if (!lpst->wMonth)
return TRUE;
return FALSE;
}
/*
@doc BOTH EXTERNAL
@func BOOL | SetTimeZoneInformation | Sets the current time-zone parameters.
These parameters control translations from Coordinated Universal Time (UTC) to local time.
@parm CONST TIME_ZONE_INFORMATION * | lptzi | address of time-zone settings
@comm Follows the Win32 reference description without restrictions or modifications.
*/
BOOL WINAPI SetTimeZoneInformation (const TIME_ZONE_INFORMATION *lpTzi) {
DWORD dwDisp;
HKEY hKey=NULL;
if (!IsValidTZISystemTime(&lpTzi->StandardDate) ||
!IsValidTZISystemTime(&lpTzi->DaylightDate)) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
SetTimeZoneBias(lpTzi->Bias+lpTzi->StandardBias,lpTzi->Bias+lpTzi->DaylightBias);
if(ERROR_SUCCESS==RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"Time", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp) && hKey) {
if(ERROR_SUCCESS==RegSetValueExW(hKey, L"TimeZoneInformation", 0, REG_BINARY, (LPBYTE)lpTzi,sizeof(TIME_ZONE_INFORMATION))) {
CeEventHasOccurred(NOTIFICATION_EVENT_TZ_CHANGE, NULL);
RegCloseKey(hKey);
return TRUE;
}
RegCloseKey(hKey);
}
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -