-2

DateTime already has a function called IsDaylightSavingTime that returns a boolean if that DateTime falls under Daylight Saving Time or not. However, it uses server (local) time to make that determination.

In my Web services I have a DateTimeOffset for each of my clients throughout the country. How can I determine IsDaylightSavingTime from a DateTimeOffset?

skink
  • 5,133
  • 6
  • 37
  • 58
Jarrette
  • 1,085
  • 2
  • 16
  • 40
  • 3
    From the docs for DateTimeOffset on MSDN: "Although a DateTimeOffset value includes an offset, it is not a fully time zone-aware data structure. While an offset from UTC is one characteristic of a time zone, it does not unambiguously identify a time zone. Not only do multiple time zones share the same offset from UTC, but the offset of a single time zone changes if it observes daylight saving time. This means that, as soon as a DateTimeOffset value is disassociated from its time zone, it can no longer be unambiguously linked back to its original time zone." – Chris Oct 23 '17 at 20:09

1 Answers1

1

Instead of DateTime.IsDaylightSavingTime, use TimeZoneInfo.IsDaylightSavingTime instead.

To use this, you will indeed need to know the time zone in question. The time zone cannot be derived from the offset alone.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • but where do I get TimeZoneInfo? do I derive it from my DateTimeOffset value? – Jarrette Oct 23 '17 at 20:04
  • That is a different matter. Unfortunately, one cannot tell what the time zone is from the offset alone. See "time zone != offset" in the [timezone tag wiki](https://stackoverflow.com/tags/timezone/info). Without knowing the time zone, you cannot tell if DST is in effect or not. – Matt Johnson-Pint Oct 23 '17 at 20:05
  • so for each client, there is a lat/long and an offset in hours in the database for that client. How can I determine if a certain time and date falls under daylight savings for that client? – Jarrette Oct 23 '17 at 20:08
  • Now you're getting somewhere, though you should search first. Most of these questions have been asked already. Start with this: https://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates – Matt Johnson-Pint Oct 23 '17 at 20:09
  • Also, you should probably not trust that offset in your database. It is fixed, so doesn't necessarily reflect reality for any given timestamp. It's not just DST to consider, but also the rich history of changes to standard time. Instead, once you know the time zone, use either `TimeZoneInfo` for Windows time zones, or [Noda Time](http://nodatime.org) for IANA time zones, to convert your timestamp to that time zone. – Matt Johnson-Pint Oct 23 '17 at 20:16
  • So what would you recommend me save in each client record instead of the offset in hours? Some string that names the timezone? – Jarrette Oct 23 '17 at 20:22
  • 1
    Exactly. With Windows time zones, save the value you use with `TimeZoneInfo.Id`. For example `"Eastern Standard Time"`. You can get a list with `TimeZoneInfo.GetSystemTimeZones()`. With IANA time zones, save the value you'd use with Noda Time's TZDB provider, such as `"America/New_York"` (list [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)). – Matt Johnson-Pint Oct 23 '17 at 20:30