Comparing LocalDateTime for Different Time Zones in Nodatime

放肆的年华 提交于 2019-11-28 14:01:44

It sounds like you're on the right track.

Regarding LenientResolver, make sure you are aware of its behavior. It uses ReturnStartOfIntervalAfter for the spring-forward gap, and ReturnLater for the fall-back overlap.

IMHO, that isn't the best configuration for scheduling of future events. (See Issue #295), and try this instead:

VB.NET

Public Shared ReadOnly SchedulingResolver As ZoneLocalMappingResolver = _
  Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, _
  AddressOf ReturnForwardShifted)

Public Shared Function ReturnForwardShifted(local As LocalDateTime, _
  zone As DateTimeZone, before As ZoneInterval, after As ZoneInterval) _
  As ZonedDateTime
    Dim newLocal As LocalDateTime = local.PlusTicks(after.Savings.Ticks)
    Return New ZonedDateTime(newLocal, zone, after.WallOffset)
End Function

C#

public static readonly ZoneLocalMappingResolver SchedulingResolver =
  Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier, ReturnForwardShifted);

public static ZonedDateTime ReturnForwardShifted(LocalDateTime local,
  DateTimeZone zone, ZoneInterval before, ZoneInterval after)
{
    LocalDateTime newLocal = local.PlusTicks(after.Savings.Ticks);
    return new ZonedDateTime(newLocal, zone, after.WallOffset);
}

Regarding the server's time zone - you should leave that out of your code. Your code should not care what the time zone of the server is. Instead, call ToInstant() on the ZonedDateTime (your eventZonedDateTime variable), then compare that with the Instant returned from clock.Now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!