繁体   English   中英

C#Windows CE Compact Framework 2.0设置系统时区

[英]C# WIndows CE Compact Framework 2.0 Set System Timezone

我想在Windows CE Mobile中以编程方式更改系统时区? 我该怎么做 ??

我已阅读了一些帖子,但我无法做到?

有人可以提供示例代码来实现它吗?

如果您越过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-Info数据库信息:请参见https://github.com/hjgode/win-mobile-code/tree/master/TimeZoneSet

为了获得TZ-Info,我使用了以下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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM