我想在我的Windows CE Mobile中以編程方式更改系統時區?我該怎麼做 ??C#WIndows CE Compact Framework 2.0設置系統時區
我閱讀了一些帖子,但我無法做到這一點?
有人可以給一個示例代碼來實現它嗎?
我想在我的Windows CE Mobile中以編程方式更改系統時區?我該怎麼做 ??C#WIndows CE Compact Framework 2.0設置系統時區
我閱讀了一些帖子,但我無法做到這一點?
有人可以給一個示例代碼來實現它嗎?
,如果你越過DST這是容易的,但棘手:http://www.hjgode.de/wp/2010/10/08/windows-mobile-setsystemtime-and-dst-einsteins-relativity-theory/
private DateTime startDateTime = DateTime.Parse("2010/9/24 11:42:00");
[DllImport("coredll.dll", SetLastError = true)]
static extern Int32 GetLastError();
[DllImport("coredll.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);
[DllImport("coredll.dll", SetLastError = true)]
static extern void GetSystemTime(out SYSTEMTIME lpSystemTime);
[DllImport("coredll.dll")]
static extern bool SetTimeZoneInformation([In] ref TIME_ZONE_INFORMATION lpTimeZoneInformation);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(out TIME_ZONE_INFORMATION lpTimeZoneInformation);
private const int TIME_ZONE_ID_UNKNOWN = 0;
private const int TIME_ZONE_ID_STANDARD = 1;
private const int TIME_ZONE_ID_DAYLIGHT = 2;
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TIME_ZONE_INFORMATION
{
public int bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
public SYSTEMTIME standardDate;
public int standardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
public SYSTEMTIME daylightDate;
public int daylightBias;
}
...
private bool disableDST(TIME_ZONE_INFORMATION tzi){
//set wMonth in standardDate to zero
SYSTEMTIME stStd;
stStd=tzi.standardDate;
stStd.wMonth=0;
//set wMonth in daylightDate to zero
SYSTEMTIME stDST;
stDST=tzi.daylightDate;
stDST.wMonth=0;
tzi.daylightDate=stDST;
tzi.standardDate=stStd;
bool bRes = SetTimeZoneInformation(ref tzi);
if (bRes)
addText("*** Disabling DST OK***");
else
addText("*** Disabling DST failed***");
return bRes;
}
這是否幫助?
編輯:添加TZ-信息數據庫信息: 見https://github.com/hjgode/win-mobile-code/tree/master/TimeZoneSet
要獲得TZ-信息我用下面的C/C++代碼:
...
typedef void (*INITCITYDB)(void);
typedef void (*UNINITCITYDB)(void);
typedef void (*LOADTZDATA)(void);
typedef void (*FREETZDATA)(void);
typedef int (*GETNUMZONES)(void);
typedef void * (*GETTZDATABYOFFSET)(int, int*);
typedef void * (*GETTZDATA)(int);
struct TZData
{
TCHAR *Name;
TCHAR *ShortName;
TCHAR *DSTName;
int GMTOffset;
int DSTOffset;
};
...
,然後該工具來獲取信息
...
int getCityDB()
{
TZData *pTZ = NULL;
int index;
// load the library
HINSTANCE hLib = LoadLibrary(_T("CityDB.dll"));
if (hLib==NULL)
return -1;
// load the CityDB functions
INITCITYDB InitCityDB = (INITCITYDB)GetProcAddress(
hLib, _T("InitCityDb"));
UNINITCITYDB UninitCityDB = (UNINITCITYDB)GetProcAddress(
hLib, _T("UninitCityDb"));
LOADTZDATA ClockLoadAllTimeZoneData = (LOADTZDATA)GetProcAddress(
hLib, _T("ClockLoadAllTimeZoneData"));
FREETZDATA ClockFreeAllTimeZoneData = (FREETZDATA)GetProcAddress(
hLib, _T("ClockFreeAllTimeZoneData"));
GETNUMZONES ClockGetNumTimezones = (GETNUMZONES)GetProcAddress(
hLib, _T("ClockGetNumTimezones"));
GETTZDATABYOFFSET ClockGetTimeZoneDataByOffset =
(GETTZDATABYOFFSET)GetProcAddress(hLib, _T("ClockGetTimeZoneDataByOffset"));
GETTZDATA ClockGetTimeZoneData = (GETTZDATA)GetProcAddress(
hLib, _T("ClockGetTimeZoneData"));
// Init the library
InitCityDB();
// load the TZ data
ClockLoadAllTimeZoneData();
// find out how many zones are defined
int zoneCount = ClockGetNumTimezones();
...
現在你可以遍歷直通的所有數據,詳見GitHub的代碼:
...
wsprintf(wBuff, L"=================CityDB====================\n");
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
// interate through them all
for(int zone = 0 ; zone < zoneCount ; zone++)
{
// these are pointers to a timezone data struct
pTZ = (TZData*)ClockGetTimeZoneDataByOffset(zone, &index);
wsprintf(wBuff, L"index: %i\n", index);
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
wsprintf(wBuff, L"\tshort name: %s\n", pTZ->ShortName );
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
wsprintf(wBuff, L"\tname: %s\n", pTZ->Name);
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
wsprintf(wBuff, L"\tGMT offset: %i\n", pTZ->GMTOffset );
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
wsprintf(wBuff, L"\tdst name: %s\n", pTZ->DSTName );
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
wsprintf(wBuff, L"\tDST offset: %i\n", pTZ->DSTOffset);
wcstombs(buff, wBuff, sizeof(wBuff)*sizeof(TCHAR));
WriteFile(hFile, buff, strlen(buff), &numWritten, NULL);
}
CloseHandle(hFile);
// unload the TZ data
ClockFreeAllTimeZoneData();
// uninit the library
UninitCityDB();
...
這裏是一個示例輸出:
index: 95
short name: GMT+1 Prague,Budapest
name: Central Europe Standard Time
GMT offset: -60
dst name: Central Europe Daylight Time
DST offset: 0
...
index: 110
short name: GMT+1 Berlin,Rome
name: W. Europe Standard Time
GMT offset: -60
dst name: W. Europe Daylight Time
DST offset: 0
參見我的文章在http://community.intermec.com/t5/Device-Management/change-Time-Zone-with-xml/td-p/17007
其工作。我仍然有疑問。我在哪裏可以獲得所有時區的所有偏差值和名稱清單? – user3383301
添加的C/C++代碼有幫助嗎? – josef