0

Ok I have an issue where I want a user interface to enter date/time for an arbitrary location on Earth. Basically I can put in my system some sort of location information to allow me to calculate the timezone for that "place". I want my UI to display the datetime entered in the location's relevant timezone setting for that specified date.

Is there a .NET API for calculating what the timezone is for a location at a specified datetime? I don't want the current timezone. I want the timezone for a specified DateTime.

E.g. in Australia AEDST, in Sydney, TZ is +11GMT today. 3 months ago it was AEST ie +10GMT. If someone enters a DateTime in of say 15/07/2020 09:00:00, I want the control to display that exact DateTime but I will store 15/07/2020 09:00:00+10:00. How do I determine what timezone Sydney had at that date?

EDIT: solution needs to be offline

Shiv
  • 1,274
  • 1
  • 19
  • 23
  • Are you looking for [`GetUtcOffset`](https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.getutcoffset?view=netcore-3.1#System_TimeZoneInfo_GetUtcOffset_System_DateTime_)? – Sweeper Oct 15 '20 at 02:18
  • 2
    https://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates Get time zone from location. – MikeJ Oct 15 '20 at 02:22
  • @MikeJ a marvellous link! –  Oct 15 '20 at 02:31
  • 1
    Does this answer your question? [How to get a time zone from a location using latitude and longitude coordinates?](https://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates) –  Oct 15 '20 at 02:33
  • @MikeJ Thanks for the link! I am parsing through it for the strategy. I'm thinking I can store IANA tz name against my resources as a first step. Then when a datetime is entered, I will use the tz name and the date info to calculate what the offset is which I'm trying to piece together now. – Shiv Oct 15 '20 at 03:46
  • 1
    @Shiv great! If you feel your question would benefit from an explicit answer not fully covered in the link, feel free to post it as an answer below –  Oct 15 '20 at 03:55

1 Answers1

0

Solution workflow:

  1. Store IANA tz name against a resource e.g. "Australia/Sydney"

  2. Have a date control specify a transaction record against that resource (e.g. using a standard DateTime control). For argument sake let's say 13/12/2020 (dd/MM/yyyy) specified. This would be +11GMT in AEDST. Need to offline calculate how to get from "13/12/2020" + "Australia/Sydney" => storing "2020-12-13T00:00:00+11:00" in DateTimeOffset field.

Use

TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Australia/Sydney");

to get timezone info.

Then for an arbitrary date, calculate offset

TimeSpan offsetTest = tzi.GetUtcOffset(new DateTime(2020, 7, 1));

The final component of the design will be the ambiguous time period where DST changes over. I would probably choose and arbitrary valid result for GetUtcOffset and then for edits, retain the previous offset if still ambiguous.

Shiv
  • 1,274
  • 1
  • 19
  • 23
  • You may find the `ToDateTimeOffset` extensions method [I gave in this other answer](https://stackoverflow.com/a/59866718/634824) useful. – Matt Johnson-Pint Nov 02 '20 at 00:59