1

I'm trying to get the difference between an application running on Windows Embedded 7 and UTC time. To do that I have the following piece of code:

TimeZoneInfo utcTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime localTime = DateTime.Now;
DateTime utcTime = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Local, utcTimeZone);
TimeSpan utcOffset = localTime - utcTime;

This runs fine on my own development PC, running Windows 7. However, when I install my application on a device running Windows Embedded 7, no matter what timezone I set it to, when I run my application,

  • The value for TimeZoneInfo.Local.BaseUtcOffset is always 00:00.
  • The BaseUtcOffset value in the object returned by TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time") is also 00:00 (though this is to be expected).
  • The ConvertTime() function above always returns the current time less one hour. (Kind of not surprised since the TimeZoneInfo.Local.SupportsDaylightSavingsTime value is always false.)

Should I be using another way that TimeZoneInfo.Local to get the offset between UTC and the current time zone? I need to include Daylight Savings in this.

komodosp
  • 3,316
  • 2
  • 30
  • 59

1 Answers1

0

A few things:

  • The time zone with the ID "GMT Standard Time" is not UTC - it's UK Time. Its display name matches "Dublin, Edinburgh, Lisbon, London". It uses UTC+0 in the winter, and UTC+1 in the summer for daylight saving time.

  • The UTC time zone ID is simply "UTC" - though you'll rarely need that.

  • If TimeZoneInfo.Local.BaseUtcOffset is zero, then that means the computer's time zone setting is one that has UTC+0 as its standard offset. There are four of those currently defined in Windows. This property does not reflect daylight saving time.

  • Recognize that offsets will change depending on what time of the year that you are running the code. A time zone is not the same as a time zone offset.

  • Since you said you got zero in your above code, I'd guess that your local time zone is either the previously mentioned UK Time, or Casablanca, Morocco. This is because you are subtracting a UTC+1 local time with the time from another time zone that is also UTC+1 presently. 1 - 1 = 0

  • The correct way to do this does not involve subtraction at all. Simply use the GetUtcOffset method:

    TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
    

    Again, note that this returns the current offset. Running it at different times of the year, or by passing a different value instead of DateTime.Now could return a different result.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • On your first point, I actually want to include the Daylight Savings time, so UTC+1 is fine for the summer. However, my problem is that no matter what I set the timezone to, even if it's Moscow (UTC+4 - the WE7 installation is a bit out of date) or Fiji (UTC+12), I still get zero `BaseUtcOffset`. – komodosp Jul 19 '16 at 07:24
  • You still wouldn't want to use UK Time if you meant UTC - That actually counteracts any DST that may be in effect in the local time zone - and the start/end dates may be different also. – Matt Johnson-Pint Jul 19 '16 at 15:03
  • On the second part - if you're getting zeros in the offset from any time zone, then I'd say somehow the time zone registry data is missing or corrupt. You could try installing [the Dec 2015 cumulative time zone update](https://support.microsoft.com/en-us/kb/3112148), which was the last one produced for Windows Embedded 7. Note that since [mainstream support has ended](https://support.microsoft.com/en-us/lifecycle?p1=15421) for this version, the more recent Windows time zone updates are not available for Windows Embedded 7. – Matt Johnson-Pint Jul 19 '16 at 15:03