使用 NodaTime 获取当地时间

Getting local hour with NodaTime

NodaTime 的新手,我选择使用它而不是 BCL 库,因为它在处理日期时消除了很多歧义。但是,我似乎无法让它做我想做的事。我有一个按年、月、日、小时和分钟指定的日期和时间。我还有两个时区需要显示 "clock time"。例如,如果我的输入是 2015 年 12 月 15 日 3:30 下午并且我的两个时区是中部标准和东部标准(相隔一小时),我希望我的输出是

12/15/2015 3:30 PM Central
12/15/2015 4:30 PM Eastern

但我似乎只能获得中央时区(如果重要的话,对我来说是本地时区)。这是我的代码:

var localDateTime = new LocalDateTime(
            year: 2015,
            month: 12,
            day: 15,
            hour: 15,
            minute: 30,
            second: 0
            );

var centralTimeZone = DateTimeZoneProviders.Bcl.GetZoneOrNull("Central Standard Time");
var easternTimeZone = DateTimeZoneProviders.Bcl.GetZoneOrNull("Eastern Standard Time");

var centralTime = centralTimeZone.AtLeniently(localDateTime);
var easternTime = easternTimeZone.AtLeniently(localDateTime);

似乎 centralTime 和 easternTime 都是 ZonedDateTime 对象,其时间为 2015-12-10T15:30,具有正确的偏移量,即 centralTime 为 -6,easternTime 为 -5。)

我只是不知道如何获得我想要的输出。

听起来你的初始 date/time 实际上是中部时间 - 所以一旦你执行了初始转换,你就可以说 "Hey, I want the same instant in time, but in a different time zone" - 这与 "I want a different instant in time for the same local time"。你想要:

var centralTime = centralTimeZone.AtLeniently(localDateTime);
var easternTime = centralTime.InZone(easternTimeZone);