S
Sonnich Jensen
Hi
We do this twice a year, but in the autum, we turn our clocks back,
and usually the hour 2 or 3 repeats itself.
This is a problem for a data handler of my, which should not do this
when the hour happens the first time.
I an older Delphi version, I used GetTimeZoneInformation to get to
know whether we have daylight savings time or standard (winter) time.
This works also in the code below, which btw currently is for testing
only.
When I know that I have daylight savings time (if is missing below),
I'd like to know when the change happens.
In Delphi I just set the years, and they would be there.
I digged into Delphi how that happens, but did not find it.
Now, how do I know when the change over happens?
WBR
Sonnich
My code as of now:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(ref
TimeZoneInformation lpTimeZoneInformation);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref
TimeZoneInformation lpTimeZoneInformation);
[StructLayout(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 TimeZoneInformation
{
public int bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
public SYSTEMTIME standardDate;
public SYSTEMTIME daylightDate;
public int standardBias;
public int daylightBias;
}
const uint TIME_ZONE_ID_INVALID = 0xFFFFFFFF;
const uint TIME_ZONE_ID_UNKNOWN = 0;
const uint TIME_ZONE_ID_STANDARD = 1;
const uint TIME_ZONE_ID_DAYLIGHT = 2;
// ********** check for daylight savings time *************
bool Daylight()
{
TimeZoneInformation TZI = new TimeZoneInformation();
short myyear = (short)DateTime.Now.Year;
TZI.standardDate.wYear = myyear;
TZI.daylightDate.wYear = myyear;
// true = we have daylight savings time
bool bDaylight = (GetTimeZoneInformation(ref TZI) ==
TIME_ZONE_ID_DAYLIGHT);
// *** this should set the time so I know the time when it actuallty
happend, but that does not work here...
TZI.standardDate.wYear = myyear;
TZI.daylightDate.wYear = myyear;
// check for repeated hour
// **** and I cannot remember why I checkc Month==0 ... I should
comment my software a bit more...
if ((TZI.standardDate.wMonth != 0) && // 0= no time
change
((DateTime.Now.Month == TZI.standardDate.wMonth) &&
(DateTime.Now.Day == TZI.standardDate.wDay)))
{
// if it is that very hour (the repeated hour)
return (DateTime.Now.Hour != (TZI.standardDate.wHour -
1));
}
return true;
}
We do this twice a year, but in the autum, we turn our clocks back,
and usually the hour 2 or 3 repeats itself.
This is a problem for a data handler of my, which should not do this
when the hour happens the first time.
I an older Delphi version, I used GetTimeZoneInformation to get to
know whether we have daylight savings time or standard (winter) time.
This works also in the code below, which btw currently is for testing
only.
When I know that I have daylight savings time (if is missing below),
I'd like to know when the change happens.
In Delphi I just set the years, and they would be there.
I digged into Delphi how that happens, but did not find it.
Now, how do I know when the change over happens?
WBR
Sonnich
My code as of now:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(ref
TimeZoneInformation lpTimeZoneInformation);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref
TimeZoneInformation lpTimeZoneInformation);
[StructLayout(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 TimeZoneInformation
{
public int bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
public SYSTEMTIME standardDate;
public SYSTEMTIME daylightDate;
public int standardBias;
public int daylightBias;
}
const uint TIME_ZONE_ID_INVALID = 0xFFFFFFFF;
const uint TIME_ZONE_ID_UNKNOWN = 0;
const uint TIME_ZONE_ID_STANDARD = 1;
const uint TIME_ZONE_ID_DAYLIGHT = 2;
// ********** check for daylight savings time *************
bool Daylight()
{
TimeZoneInformation TZI = new TimeZoneInformation();
short myyear = (short)DateTime.Now.Year;
TZI.standardDate.wYear = myyear;
TZI.daylightDate.wYear = myyear;
// true = we have daylight savings time
bool bDaylight = (GetTimeZoneInformation(ref TZI) ==
TIME_ZONE_ID_DAYLIGHT);
// *** this should set the time so I know the time when it actuallty
happend, but that does not work here...
TZI.standardDate.wYear = myyear;
TZI.daylightDate.wYear = myyear;
// check for repeated hour
// **** and I cannot remember why I checkc Month==0 ... I should
comment my software a bit more...
if ((TZI.standardDate.wMonth != 0) && // 0= no time
change
((DateTime.Now.Month == TZI.standardDate.wMonth) &&
(DateTime.Now.Day == TZI.standardDate.wDay)))
{
// if it is that very hour (the repeated hour)
return (DateTime.Now.Hour != (TZI.standardDate.wHour -
1));
}
return true;
}