-2

I am implementing DST checkbox, when checkbox is on clock is adjusted if required and if checkbox is off then DST is not considered,

Suppose When DST begins, clocks are advanced by one hour
so here we have two times - original time and adjusted time
how do I retrieve both these times?

at New York (-04) On Sun, Mar 8 at 2:00 am DST starts so clocks are adjusted +1 hour.
so there is original time and adjusted time

at original time Sun, Mar 9 at 8:00 am the clock displays adjusted time Sun, Mar 9 at 9:00 am

being at a place out of USA, given input as current universal time I want to retrieve original time and DST adjusted time at new york.

Input/Output - Update 03/05
Is this correct way of achieving above

        string fromZoneId = "Asia/Kolkata";
        string toZoneId = "America/New_York";
        var fromDateTime = DateTime.Parse("March 9, 2020");//Input kolkata time

        LocalDateTime fromLocal = LocalDateTime.FromDateTime(fromDateTime);
        DateTimeZone fromZone = DateTimeZoneProviders.Tzdb[fromZoneId];
        ZonedDateTime fromZoned = fromLocal.InZoneLeniently(fromZone);

        DateTimeZone toZone = DateTimeZoneProviders.Tzdb[toZoneId];
        ZonedDateTime toZoned = fromZoned.WithZone(toZone);
        LocalDateTime toLocal = toZoned.LocalDateTime;

        var interval = toZoned.GetZoneInterval();
        var savings = interval.Savings;

        var originalTime = toLocal.ToDateTimeUnspecified().AddSeconds(-savings.Seconds);
        var dstAdjustedTime = toLocal.ToDateTimeUnspecified();

        Console.WriteLine("Actual:"+ originalTime);//output-dst off
        Console.WriteLine("Adjusted:"+ dstAdjustedTime);//output-dst on
Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98
  • 1
    I don't really understand the question. What do you mean "when DST is toggled"? It would be really helpful if you could give a concrete example of input and expected output. While Windows allows you to set the system to not observe DST, I'd strongly recommend against propagating that option - it's a very strange one, and I suspect in some places (e.g. Ireland, with negative DST) it would give unexpected outcomes. – Jon Skeet Mar 03 '20 at 07:16
  • basically I want to retrieve time when dst is off and dst is on – Nitin Sawant Mar 03 '20 at 08:14
  • That's really not a clear explanation I'm afraid - at least I certainly don't find it clear. You can detect from a given `ZonedDateTime` whether DST is currently being applied or not - is that useful? As I said before, it would be helpful if you'd give a concrete example (or ideally several concrete examples) of input and expected output. – Jon Skeet Mar 03 '20 at 08:24
  • re-phrased the question – Nitin Sawant Mar 03 '20 at 11:40
  • There *still* isn't a clear example of input and desired output. (It would also help if you could give some clarity about the use case here. If users just want a fixed offset of UTC-5 for example, I think it would be clearer to offer exactly that.) – Jon Skeet Mar 03 '20 at 11:53
  • 2
    So what would you want to happen for a time zone where standard time is UTC+1 (for the summer), and DST of -1 hour is applied, so in the winter it's UTC+0? Again, what's the actual *use case* here. Where would this "original time" come from, or who wants it as a result? You can use `ZonedDateTime.GetZoneInterval()` and then use `Savings` to see how much of the overall offset is due to DST, but I strongly suspect that any use case involving this should be re-evaluated to look at the bigger picture. – Jon Skeet Mar 03 '20 at 15:45
  • For the record, I agree with Jon about Windows having that option is strange and I work for Microsoft and regularly interact with the team that maintains Windows time zone features. It's there for legacy reasons and generally should not be disabled except under extreme edge cases and even then only temporarily. Don't try to copy that as a feature into your own application. – Matt Johnson-Pint Mar 25 '20 at 20:40

2 Answers2

1

What I would change in your example is staying in Noda to do all the transformations before moving to BLC. That way you can take advantage of the WithOffset() method and use the StandardOffset instead of doing the arithmetics yourself within BLC

So, instead of doing

    DateTimeZone toZone = DateTimeZoneProviders.Tzdb[toZoneId];
    ZonedDateTime toZoned = fromZoned.WithZone(toZone);
    LocalDateTime toLocal = toZoned.LocalDateTime;

    var interval = toZoned.GetZoneInterval();
    var savings = interval.Savings;

    var originalTime = toLocal.ToDateTimeUnspecified().AddSeconds(-savings.Seconds);
    var dstAdjustedTime = toLocal.ToDateTimeUnspecified();

I would do

    DateTimeZone toZone = DateTimeZoneProviders.Tzdb[toZoneId];
    ZonedDateTime toZoned = fromZoned.WithZone(toZone);
    LocalDateTime toLocal = toZoned.LocalDateTime;

    var interval = toZoned.GetZoneInterval();
    var standardOffset = interval.StandardOffset;

    var originalTime = toZoned.ToOffsetDateTime().WithOffset(standardOffset).ToDateTimeOffset();
    var dstAdjustedTime = toLocal.ToDateTimeUnspecified();

Then you can check how to better display the original time because now it is a DateTimeOffset and not a DateTime. But I would even try to use the NodaTime serialization methods instead of going to DateTime or DateTimeOffset

Martín La Rosa
  • 790
  • 4
  • 17
0

I am implementing DST checkbox, when checkbox is on clock is adjusted if required and if checkbox is off then DST is not considered ...

You immediately have a problem. You should not implement such a checkbox. The requirement is flawed. Every implementation that does that is broken. It's simply not how time zones work.

For example, say I pick "America/New_York" then I disable DST. What does that even mean? All of "America/New_York" by definition uses DST presently. You could apply it to an earlier date that did not (say, 1944) - or you could pick a different time zone that is currently at UTC-5 without DST, such as "America/Jamaica". But you can't arbitrarily lie about whether DST is in effect or not.

Put another way - a person living in a particular area does not have a choice on whether DST applies or not for themselves personally. It is up to the government, and those established rules are reflected in the time zone data for the given identifier.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575