Determine how to calculate times across different timezones using nodaTime and withZone on ZoneDateTime

后端 未结 1 527
抹茶落季
抹茶落季 2021-01-22 08:41

When converting times between time zones. I found the follow code works. However I am not sure how programmatically what to put as my offset for the constructor for the

相关标签:
1条回答
  • 2021-01-22 09:21

    If you're interested in the current time, you can make this much simpler - and indeed more testable. Make your code take an IClock as a "service for providing the current instant in time". For the concrete implementation, use SystemClock.Instance in production, but FakeClock for testing.

    IClock has a single member: Now. That returns the current Instant in time, which isn't tied to a time zone or even a calendar system.

    With an Instant and a time zone, you can easily get to a ZonedDateTime:

    Instant now = clock.Now;
    var zone = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
    ZonedDateTime = now.InZone(zone);
    

    It will work out what the local date/time and offset is. You don't need to do anything clever :)

    0 讨论(0)
提交回复
热议问题