NodaTime 将夏令时量添加到 ZonedDateTime

NodaTime Adding the Daylight Savings Amount to a ZonedDateTime

我正在尝试确定将当前夏令时量添加到 ZonedDateTime 的正确方法。下面的方法好像是可以的,只要金额是正数就行,但是不知有没有更好的方法?

' Get the current moment in time
Dim now As Instant = SystemClock.Instance.GetCurrentInstant()                                        

' Convert to UTC time
Dim UtcDateTime As ZonedDateTime = now.InUtc                                       

' Get this computer's Local TimeZone
Dim LocalTimeZone As DateTimeZone = DateTimeZoneProviders.Tzdb.GetSystemDefault()

' Convert the UTC DateTime to the time in the Local TimeZone
Dim LocalDateTime As ZonedDateTime = UtcDateTime.WithZone(LocalTimeZone)

' The above code is just to set things up. The following code is the question:

' Check if Daylight Savings Time is in force
Dim DstInForce As Boolean = LocalDateTime.IsDaylightSavingTime

' Get the Interval for the Local TimeZone
Dim LocalInterval As ZoneInterval = LocalTimeZone.GetZoneInterval(LocalDateTime.ToInstant)

' If Daylight Savings Time is in force, add the Savings Amount
If DstInForce = True Then
    LocalDateTime.PlusSeconds(CLng(LocalInterval.Savings.ToTimeSpan.TotalSeconds))    
End If

编辑添加..

这是另一个工作示例:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim NowDateTime As LocalDateTime = New LocalDateTime(2020, 8, 5, 9, 15)
    Dim UtcZone As DateTimeZone = DateTimeZoneProviders.Tzdb("UTC")
    Dim UtcDateTime As ZonedDateTime = NowDateTime.InZoneLeniently(UtcZone)

    TextBox1.AppendText("It is currently " & UtcDateTime.ToString & " in zone " & UtcZone.ToString & vbCrLf)

    Dim ThisComputerTimeZone As DateTimeZone = DateTimeZoneProviders.Tzdb("Europe/London")
    Dim ThisComputerDateTime As ZonedDateTime = UtcDateTime.WithZone(ThisComputerTimeZone)

    ' Check if Daylight Savings Time is in force
    Dim DstInForce As Boolean = ThisComputerDateTime.IsDaylightSavingTime
    Dim ThisComputerInterval As ZoneInterval = ThisComputerTimeZone.GetZoneInterval(ThisComputerDateTime.ToInstant)
    If DstInForce = True Then
        ' If Daylight Savings Time is in force, add the Savings Amount
        ThisComputerDateTime.PlusSeconds(CLng(ThisComputerInterval.Savings.ToTimeSpan.TotalSeconds))
    End If
    TextBox1.AppendText("It is currently " & ThisComputerDateTime.ToString & " in local zone " & ThisComputerTimeZone.ToString & vbCrLf)
    TextBox1.AppendText("Daylight Savings Time is '" & DstInForce & "' and has been applied." & vbCrLf)
End Sub

这是它的输出:

It is currently 2020-08-05T09:15:00 UTC (+00) in zone UTC
It is currently 2020-08-05T10:15:00 Europe/London (+01) in local zone Europe/London
Daylight Savings Time is 'True' and has been applied.

如您所见,我需要添加额外的储蓄金额以将 UTC 时间“09:15”更改为夏令时“10:15”。我想做什么?我正在尝试创建一个 UTC 时间,然后将其更改为我计算机的本地时间,并包含正确的 DST 时间。这些步骤是更大过程的一部分,为清楚起见进行了缩写。

您不需要“检查夏令时是否生效”之后的任何代码 - WithZone 方法已将其考虑在内。您的代码实际上并没有做任何有用的事情,因为您忽略了 PlusSeconds 的结果 - 它不会修改现有的 ZonedDateTime,而是 returns 一个新的。

这一行:

Dim ThisComputerDateTime As ZonedDateTime = UtcDateTime.WithZone(ThisComputerTimeZone)

...做你需要的一切。 WithZone 已经考虑了夏令时。无需尝试手动调整它。