实施DST复选框

问题描述 投票:-2回答:1

我正在实现DST复选框,如果需要,则在复选框启用时调整时钟,如果复选框处于关闭状态,则不考虑DST,

假设开始夏令时,时钟提前一小时所以这里有两次-原始时间和调整时间我如何检索这两个时间?

New York (-04)Sun, Mar 8 at 2:00 am处,DST开始,因此时钟调整为+1小时。所以有原始时间和调整时间在原始时间Sun, Mar 9 at 8:00 am,时钟显示调整后的时间Sun, Mar 9 at 9:00 am在美国以外的地方,给定输入current universal time,我想在纽约检索original time和DST adjusted time

输入/输出-更新03/05这是实现上述目标的正确方法吗?

        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

我正在实现DST复选框,如果需要,可以调整复选框的启用时间,如果复选框处于关闭状态,那么就不考虑DST,假设DST开始时,时钟提前了一个小时,所以这里...

c# nodatime
1个回答
0
投票

在您的示例中,我将要更改的是留在Noda进行BLC之前的所有转换。这样,您可以利用WithOffset()方法并使用StandardOffset而不是自己在BLC中进行算术]

© www.soinside.com 2019 - 2024. All rights reserved.