SetSystemTime - UTC or local time?

I have a call to SetSystemTime from my C # application. However, if I have Windows timezone set to a non-zero offset from UTC, it seems sometimes I have to adjust the system clock as if the time I was providing was UTC (i.e. converted to local time), while in others this is not the case, it just sets the time directly to the parameter date

.

    [StructLayout(LayoutKind.Sequential)]
    internal struct SystemTime
    {
        public short Year;
        public short Month;
        public short DayOfWeek;
        public short Day;
        public short Hour;
        public short Minute;
        public short Second;
        public short Milliseconds;
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern bool SetSystemTime(ref SystemTime st);

    public static bool AdjustSystemClock(DateTime date)
    {
        SystemTime systemTime = new SystemTime();
        systemTime.Year = (short)date.Year;
        systemTime.Month = (short)date.Month;
        systemTime.Day = (short)date.Day;
        systemTime.Hour = (short)date.Hour;
        systemTime.Minute = (short)date.Minute;
        systemTime.Second = (short)date.Second;
        return SetSystemTime(ref systemTime);
    }

      

The difference seems to be: When I set the timezone using Windows then run the application, when I call SetSystemTime()

it adjusts the time specified as if it were UTC.

But when I set the timezone using a function SetDynamicTimeZoneInformation()

, restart the application and then call SetSystemTime()

, then it sets the time directly to the time I provide, regardless of the timezone.

Is this the expected behavior? How can I get consistency between the two methods for setting the timezone?

+3


source to share


1 answer


I believe I have found the problem.

It turns out that the person who wrote the bit of code SetDynamicTimeZoneInformation()

forgot to set the property Bias

.



So the time zone information is UTC-zero, so no adjustment will take place.

+1


source







All Articles