Wow, as I first saw this issue, I thought I was facing Einstein’s Relativity Theory.
The issue is simple to explain:
- The WinMo device has a local time and date within DST.
- You use
SetSystemTime
to set a new system time and date outside the DST frame.
- The local time changes, but DST is still applied!
In concrete example:
GetTimeZoneInformation:
DaylightSaving-Time:
Bias -60 Daylight-Name: W Daylight-Bias: -60 Standard-Name: W Standard-Bias: 0
Standard-Date: 00/10/05 03:00:00, Daylight-Date: 00/03/05 02:00:00
1. Set time inside DST +++++++++++
+++++++++++ TstSetTime ++++++++++++
SetSystemTime: 2010/09/21 10:32:00
GetLocalTime: 2010/09/21 12:32:00
GetSystemTime: 2010/09/21 10:32:00
------------ TstSetTime ------------
2. Set time outside DST ----------
+++++++++++ TstSetTime ++++++++++++
SetSystemTime: 2010/10/31 01:50:00
GetLocalTime: 2010/10/31 03:50:00
GetSystemTime: 2010/10/31 01:50:00
As you can see, local time is 2 hours of, although the data/time is outside DST. You can also see that the device is in GMT+1 time zone.
Ah, you say this is a known issue. OK, let's do it the Microsoft way and Sleep()
and set system time again:
3. SLEEP...
4. Set time outside DST 2nd CALL ----------
+++++++++++ TstSetTime ++++++++++++
SetSystemTime: 2010/10/31 01:50:00
GetLocalTime: 2010/10/31 02:50:00
GetSystemTime: 2010/10/31 01:50:00
You are right, now the local time is correct.
Ok, not clear but a workaround. Now go on and set time back into DST:
------------ TstSetTime ------------
5. Set time back inside DST +++++++++++
+++++++++++ TstSetTime ++++++++++++
SetSystemTime: 2010/09/21 10:32:00
GetLocalTime: 2010/09/21 11:32:00
GetSystemTime: 2010/09/21 10:32:00
Again a fault. This time DST is not applied although the date is within DST frame.
Doing the same in Compact Framework is worse and you will only get valid results with disabled DST.
The best workaround is to disable DST BEFORE you change the system time and then restore DST after changing the system time.
.NET Code Snippets
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){
SYSTEMTIME stStd;
stStd=tzi.standardDate;
stStd.wMonth=0;
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;
}
Possibly good information for all those who do TimeSync
with a server.