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

折月煮酒 提交于 2019-12-02 02:42:42

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 :)

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