0

I'm working on a ASP.net web application and it hosted in windows server.I want to test few functionalities with DST fallback ambiguous hour (2020-11-1 01.15 am) with different user time zones .Server time zone is Eastern (EST/EDT) i can change that. But users can select their timezone . If i change Eastern time zone to 2020-11-1 01.15 am other time zones not adjusting according to that users are seeing the current time not the time. is there a way i can set other time zones time manually .

  • 1
    The server version or even the OS doesn't matter. For starters, *don't* use `DateTime`, use DateTimeOffset at least. The `DateTime` type supports only 2.5 kinds of values - UTC, Local and Unspecified. `Local` means whatever timezone the executing account is using, Unspecified means you have no idea what the offset is. – Panagiotis Kanavos Jun 19 '20 at 07:10
  • 1
    Date localization is a complex topic and there is no perfect solution. In my case I prefer to make my systems to run in UTC date and then when it interacts with the user I convert to/from the user's time zone. – Gusman Jun 19 '20 at 07:10
  • 3
    Don't just store UTC either, unless you also store the actual timezone separately. As Jon Skeet wrote [UTC is not a silver bullet](https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/). UTC says nothing about DST for example, which means you *can't* really use UTC times unless you know what offset *and* DST rules were used – Panagiotis Kanavos Jun 19 '20 at 07:12
  • 1
    The best solution is to store the IANA timezone name (eg `Europe\Berlin`). The IANA timezone database (tzdb) is the defacto standard for timezones and contains offset and DST rules for all timezones going back to the 1900s. That's why so many forums ask users for their timezone name, not their offset. You should probably use NodaTime too to handle timezones and conversions. – Panagiotis Kanavos Jun 19 '20 at 07:16
  • "If i change Eastern time zone to 2020-11-1 01.15" -> I was lost at this sentence. It doesn't make sense to me to change "a timezone" to a "datetime". The time is the same, the way time is represented is changing. – Pac0 Jun 19 '20 at 07:19
  • Could you provide some precise code, a [mcve] for instance, so we can understand better? – Pac0 Jun 19 '20 at 07:19
  • @Pac0 In Eastern time zone 2020-11-1 01.15 is the ambiguous time , ambiguous time depends on the timezone – charitht99 perera Jun 19 '20 at 07:28
  • This part I understand, and I agree. What I don't understand is what is supposed to be changing, and in which conditions it could happen in your software. – Pac0 Jun 19 '20 at 07:31

1 Answers1

0

The below example can help you.

DateTime dt = DateTime.Now.ToUniversalTime();
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime date = TimeZoneInfo.ConvertTimeFromUtc(dt, timeZone);

You can find a list of time zones here

https://stackoverflow.com/a/7908482/6527049

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 2
    Not really. Where would that timezone name come from? It has to be stored somewhere. Windows timezones are problematic too, with several gaps especially for older dates. They depend on OS updates too. Everyone who really cares about timeones uses IANA tzdb directly or through a library, eg NodaTime – Panagiotis Kanavos Jun 19 '20 at 07:17
  • 2
    I'd also strongly recommend avoiding `DateTime.Now`. The expression `DateTime.Now.ToUniversalTime()` effectively performs "Take the system time (UTC), convert it to local time, then convert it back to UTC." It's *much* less efficient (as well as harder to read) than `DateTime.UtcNow`. – Jon Skeet Jun 19 '20 at 07:26
  • As for non-standard, take IST - what does it mean? It's definitely *not* what you just thought - there are actually 3 different timezones with those initials. Irish Standard Time, Israel Standard Time, India Standard Time – Panagiotis Kanavos Jun 19 '20 at 07:37