📄 time_fun.cpp
字号:
#include "stdafx.h"
#include "time_fun.h"
#include <winbase.h>
#define ZoneHasDaylightTime(tzi) ((tzi).DaylightDate.wMonth && (tzi).DaylightBias)
VOID GetTimeStr(SYSTEMTIME CurTime,SYSTEMTIME CurDate,PTIME_AREA_STR pTimeStr,BOOL b12hour)
{
_stprintf(pTimeStr->year,_T("%d"),CurDate.wYear);
_stprintf(pTimeStr->mon,_T("%d"),CurDate.wMonth);
_stprintf(pTimeStr->date,_T("%d"),CurDate.wDay);
GetTimeFormat(NULL,0,&CurTime,b12hour?L"tt hh":L"HH",pTimeStr->hour,TIME_LEN);
GetTimeFormat(NULL,0,&CurTime,L"mm",pTimeStr->min,TIME_LEN);
GetTimeFormat(NULL,0,&CurTime,L"ss",pTimeStr->sec,TIME_LEN);
}
VOID SetDateTime(SYSTEMTIME CurTime,SYSTEMTIME CurDate)
{
CurTime.wYear = CurDate.wYear;
CurTime.wMonth = CurDate.wMonth;
CurTime.wDay = CurDate.wDay;
SetLocalTime(&CurTime);
}
void CalcRightTime(SYSTEMTIME &CurDate,BOOL bUp,UINT& Mask)
{
if(CurDate.wYear>2050)
CurDate.wYear = 2050;
else if(CurDate.wYear<1950)
CurDate.wYear = 1950;
switch(CurDate.wMonth) {
case 4:
case 6:
case 9:
case 11:
if(CurDate.wDay==31)
{
if(bUp)
CurDate.wDay=1;
else
CurDate.wDay=30;
Mask|=BIT_DATE;
}
break;
case 2:
if(CurDate.wDay==31)
{
if(bUp)
{
CurDate.wDay=1;
}
else if(CurDate.wYear%4==0)
{
CurDate.wDay =29;
}
else
{
CurDate.wDay =30;
}
Mask|=BIT_DATE;
}
else if(CurDate.wDay==30)
{
if(CurDate.wYear%4==0)
{
if(bUp)
CurDate.wDay=1;
else
CurDate.wDay=29;
}
Mask|=BIT_DATE;
}
break;
default:
break;
}
}
VOID DeInitZones(PTIMEZONE_ITEM pZoneInf)
{
if(pZoneInf)
delete[] pZoneInf;
pZoneInf = NULL;
}
BOOL InitZones(PTIMEZONE_ITEM* pZoneItem,UINT& num)
{
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 255
HKEY hKey,hSubkey;
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
CString SubKeyName;
DWORD cchValue = MAX_VALUE_NAME;
PTIMEZONE_ITEM pZoneInf;
SubKeyName = L"Time Zones";
pZoneInf = NULL;
num = 0;
if(ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, SubKeyName,0, KEY_QUERY_VALUE, &hKey ))
return FALSE;
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// Enumerate the subkeys, until RegEnumKeyEx fails.
num = cSubKeys;
if(cSubKeys)
{
pZoneInf = new TIMEZONE_ITEM[cSubKeys];
for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
_tcscpy(pZoneInf[i].std,achKey);
}
SubKeyName.Format(L"Time Zones\\%s",achKey);
if(ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, SubKeyName,0, KEY_QUERY_VALUE, &hSubkey ))
{
cbName = MAX_KEY_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx( hSubkey, L"Display", NULL, NULL,
(LPBYTE)achKey, &cbName))
{
_tcscpy(pZoneInf[i].display,achKey);
}
cbName = MAX_KEY_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx( hSubkey, L"Dlt", NULL, NULL,
(LPBYTE)achKey, &cbName))
{
_tcscpy(pZoneInf[i].dlt,achKey);
}
cbName = MAX_KEY_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx( hSubkey, L"TZI", NULL, NULL,
(LPBYTE)achKey, &cbName))
{
memcpy(pZoneInf[i].tzi,achKey,cbName);
}
RegCloseKey( hSubkey );
}
}
}
RegCloseKey( hKey );
*pZoneItem = pZoneInf;
return TRUE;
}
UINT GetTagetZone(PTIMEZONES_INF pZoneInf,PTCHAR pDisplayStr,SYSTEMTIME* pSystime)
{
BOOL res=0;
TIME_ZONE_INFORMATION Where;
PTIMEZONE_ITEM pZoneItem;
pZoneItem=pZoneInf->pZoneItem;
DWORD dwVal = GetTimeZoneInformation(&Where);
wholeSetZone(&Where,pZoneItem+pZoneInf->dwCurZon);
if(ZoneHasDaylightTime(Where))
pZoneInf->bDl_Mask|=BIT_DL_EN;
else
pZoneInf->bDl_Mask&=(~BIT_DL_EN);
pZoneInf->bDl_Mask |= ZoneHasDaylightTime(Where)?BIT_DL_EN:0x0;
_tcscpy(pDisplayStr,(pZoneItem+pZoneInf->dwCurZon)->display);
#if KEEP_UTC_TIME
res = TimeAutoCal(Where,pSystime);
#endif
return res;
}
UINT TimeAutoCal(TIME_ZONE_INFORMATION& timezone,SYSTEMTIME* pSystime)
{
UINT res=0;
BOOL bInDST;
TIME_ZONE_INFORMATION oldtimezone;
DWORD dwRet = GetTimeZoneInformation(&oldtimezone);
bInDST = (TIME_ZONE_ID_DAYLIGHT == dwRet);
LONG OldBias = oldtimezone.Bias + (bInDST?oldtimezone.DaylightBias:oldtimezone.StandardBias);
LONG NewBias = timezone.Bias + (bInDST?timezone.DaylightBias:timezone.StandardBias);
SYSTEMTIME stOld;
GetLocalTime(&stOld);
__int64 ftOld = 0;
SystemTimeToFileTime(&stOld, (FILETIME*)&ftOld);
__int64 ftNew = ftOld + (((__int64)(OldBias-NewBias)) * FILETIME_TO_MINUTES);
SYSTEMTIME stNew;
FileTimeToSystemTime((FILETIME*)&ftNew, &stNew);
if(pSystime->wMinute!=stNew.wMinute)
{
pSystime->wMinute = stNew.wMinute;
res|=BIT_MIN;
}
if(pSystime->wHour!=stNew.wHour)
{
pSystime->wHour = stNew.wHour;
res|=BIT_HOUR;
}
if(pSystime->wDay!=stNew.wDay)
{
pSystime->wDay = stNew.wDay;
res|=BIT_DATE;
}
return res;
}
int GetCurZone(PTIMEZONES_INF pZoneInf,PTCHAR pDisplayStr)
{
TIME_ZONE_INFORMATION Where;
int index,num;
PTIMEZONE_ITEM pZoneItem;
num = pZoneInf->dwZoneNum;
pZoneItem=pZoneInf->pZoneItem;
DWORD dwVal = GetTimeZoneInformation(&Where);
pZoneInf->bDl_Mask = 0;
// bDaylightOn = (dwVal==TIME_ZONE_ID_DAYLIGHT?TRUE:FALSE);
pZoneInf->bDl_Mask |= GetDaylightRegOn()?BIT_DL_ON:0x0;
pZoneInf->bDl_Mask |= ZoneHasDaylightTime(Where)?BIT_DL_EN:0x0;
for(index=0;index<num;index++)
{
if(wcsicmp(Where.StandardName, pZoneItem[index].std) == 0)
{
_tcscpy(pDisplayStr,pZoneItem[index].display);
break;
}
}
if(index>=num)
index = 0;
pZoneInf->dwCurZon = index;
return index;
}
BOOL Get12HourMode()
{
HKEY hKey;
CString KeyName;
DWORD cbName;
DWORD achKey;
DWORD regType;
BOOL bEnble=FALSE;
KeyName = L"SOFTWARE\\Microsoft\\Clock";
if(ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, KeyName,0, KEY_QUERY_VALUE, &hKey ))
{
cbName = MAX_KEY_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"12HourMode", NULL, ®Type,(LPBYTE)(&achKey), &cbName))
{
bEnble=(achKey==1?TRUE:FALSE);
}
RegCloseKey( hKey );
}
return bEnble;
}
void Set12HourMode(BOOL bMode)
{
HKEY hKey;
CString KeyName;
DWORD cbName;
DWORD achKey;
achKey = bMode;
KeyName = L"SOFTWARE\\Microsoft\\Clock";
if(ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, KeyName,0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hKey ))
{
cbName = MAX_KEY_LENGTH;
RegSetValueEx(hKey, L"12HourMode", NULL, REG_DWORD,(LPBYTE)(&achKey), sizeof(DWORD));
RegCloseKey( hKey );
}
}
BOOL GetDaylightRegOn()
{
HKEY hKey;
CString KeyName;
DWORD cbName;
DWORD achKey;
DWORD regType;
BOOL bEnble=FALSE;
KeyName = L"SOFTWARE\\Microsoft\\Clock";
if(ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, KeyName,0, KEY_QUERY_VALUE, &hKey ))
{
cbName = MAX_KEY_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx(hKey, L"AutoDST", NULL, ®Type,(LPBYTE)(&achKey), &cbName))
{
bEnble=(achKey==1?TRUE:FALSE);
}
RegCloseKey( hKey );
}
return bEnble;
}
void SetDaylightRegOn(BOOL bDaylight)
{
HKEY hKey;
CString KeyName;
DWORD cbName;
DWORD achKey;
achKey = bDaylight;
KeyName = L"SOFTWARE\\Microsoft\\Clock";
if(ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, KeyName,0, KEY_QUERY_VALUE|KEY_SET_VALUE, &hKey ))
{
cbName = MAX_KEY_LENGTH;
RegSetValueEx(hKey, L"AutoDST", NULL, REG_DWORD,(LPBYTE)(&achKey), sizeof(DWORD));
RegCloseKey( hKey );
}
}
/*
LONG GetBias(int iIndex,BOOL bInDST,BOOL &bDstEn)
{
LONG lBias;
TIME_ZONE_INFORMATION timezone;
PTIMEZONE_ITEM pZones;
ZeroMemory(&timezone, sizeof(timezone));
pZones = m_pTZ_CON+m_CurZone;
wholeSetZone(&timezone,pZones);
bDstEn = ZoneHasDaylightTime(timezone);
if(!bDstEn)
bInDST = FALSE;
lBias = timezone.Bias+(bInDST?timezone.DaylightBias:timezone.StandardBias);
return lBias;
}
*/
void wholeSetZone(TIME_ZONE_INFORMATION* tm,PTIMEZONE_ITEM pZones)
{
byte* stream;
stream = pZones->tzi;
wcscpy(tm->StandardName,pZones->std);
wcscpy(tm->DaylightName,pZones->dlt);
tm->Bias=stream[0]+(stream[1]<<8)+(stream[2]<<16)+(stream[3]<<24);
tm->StandardBias=stream[4]+(stream[5]<<8)+(stream[6]<<16)+(stream[7]<<24);
tm->DaylightBias=stream[8]+(stream[9]<<8)+(stream[10]<<16)+(stream[11]<<24);
tm->StandardDate.wYear=(unsigned short)(stream[12]+(stream[13]<<8));
tm->StandardDate.wMonth=(unsigned short)(stream[14]+(stream[15]<<8));
tm->StandardDate.wDayOfWeek=(unsigned short)(stream[16]+(stream[17]<<8));
tm->StandardDate.wDay=(unsigned short)(stream[18]+(stream[19]<<8));
tm->StandardDate.wHour=(unsigned short)(stream[20]+(stream[21]<<8));
tm->StandardDate.wMinute=(unsigned short)(stream[22]+(stream[23]<<8));
tm->StandardDate.wSecond=(unsigned short)(stream[24]+(stream[25]<<8));
tm->StandardDate.wMilliseconds=(unsigned short)(stream[26]+(stream[27]<<8));
tm->DaylightDate.wYear=(unsigned short)(stream[28]+(stream[29]<<8));
tm->DaylightDate.wMonth=(unsigned short)(stream[30]+(stream[31]<<8));
tm->DaylightDate.wDayOfWeek=(unsigned short)(stream[32]+(stream[33]<<8));
tm->DaylightDate.wDay=(unsigned short)(stream[34]+(stream[35]<<8));
tm->DaylightDate.wHour=(unsigned short)(stream[36]+(stream[37]<<8));
tm->DaylightDate.wMinute=(unsigned short)(stream[38]+(stream[39]<<8));
tm->DaylightDate.wSecond=(unsigned short)(stream[40]+(stream[41]<<8));
tm->DaylightDate.wMilliseconds=(unsigned short)(stream[42]+(stream[43]<<8));
}
void SetTimeZone(PTIMEZONES_INF pZoneInf)
{
// TODO: Add your control notification handler code here
int iIndex;
BOOL fUseDST;
TIME_ZONE_INFORMATION timezone;
PTIMEZONE_ITEM pZones;
ZeroMemory(&timezone, sizeof(timezone));
iIndex = pZoneInf->dwCurZon;
pZones = pZoneInf->pZoneItem+iIndex;
wholeSetZone(&timezone,pZones);
fUseDST=((pZoneInf->bDl_Mask&BIT_DL_EN)&&(pZoneInf->bDl_Mask&BIT_DL_ON));
SetTimeZoneInformation(&timezone);
SetDaylightRegOn(fUseDST);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -