问题
I want to change the system time zone programmatically in my windows CE Mobile ?? How do I do it ??
I read through some posts but i am not able to do it ??
Can someone give an sample code to acheive it ?
回答1:
That is easy but tricky if you cross 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;
}
Does this help?
EDIT: Added TZ-Info db info: See https://github.com/hjgode/win-mobile-code/tree/master/TimeZoneSet
To get the TZ-Info I used the following C/C++ code:
...
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;
};
...
and then this to get the info
...
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();
...
Now you can iterate thru all data, for details see the github code:
...
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();
...
here is a sample output:
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
See also my post at http://community.intermec.com/t5/Device-Management/change-Time-Zone-with-xml/td-p/17007
来源:https://stackoverflow.com/questions/34016733/c-sharp-windows-ce-compact-framework-2-0-set-system-timezone