这是在Nodatime之间转换时区的正确方法吗?

CIN*_*PPS 6 c# nodatime

目标是创建一个函数,使用Nodatime将时间从一个时区转换为另一个时区.我不只是在寻找关于结果是否正确的反馈,而是针对"我如何"这样做的反馈(这是真的正确,还是有微妙的漏洞).

这是功能:

static ZonedDateTime ConvertDateTimeToDifferentTimeZone(DateTime p_from_datetime, string p_from_zone, string p_to_zone)
{
    DateTimeZone from_zone = DateTimeZoneProviders.Tzdb[p_from_zone];
    LocalDateTime from_local = new LocalDateTime(p_from_datetime.Year, p_from_datetime.Month, p_from_datetime.Day, p_from_datetime.Hour, p_from_datetime.Minute);
    ZonedDateTime from_datetime = from_zone.AtStrictly(from_local);

    var from_instant = from_datetime.ToInstant();

    DateTimeZone to_zone = DateTimeZoneProviders.Tzdb[p_to_zone];
    ZonedDateTime to_datetime = to_zone.AtStrictly(from_local);

    var to_offset_datetime = from_instant.WithOffset(to_datetime.Offset);

    return to_zone.AtStrictly(to_offset_datetime.LocalDateTime);
}
Run Code Online (Sandbox Code Playgroud)

我称之为的方式如下:

    DateTime from_datetime = new DateTime(2016, 10, 15, 16, 30, 0);
    string from_zone = "US/Central";
    string to_zone = "US/Eastern";
    var x = ConvertDateTimeToDifferentTimeZone(from_datetime, from_zone, to_zone);

    Console.WriteLine(from_datetime.ToString() + " (" + from_zone + ") = " + " (" + x.ToString() + " (" + to_zone + ")");
Run Code Online (Sandbox Code Playgroud)

我错过了什么或做错了吗?

Jon*_*eet 6

我会尽可能坚持Noda Time类型(和.NET命名约定).区域之间的转换应该完成ZonedDateTime.WithZone- 这意味着你真的要求转换和转换ZonedDateTime.如果你真的,真的必须使用DateTime而不是Noda Time类型,你可能需要这样的东西:

public static DateTime ConvertDateTimeToDifferentTimeZone(
    DateTime fromDateTime,
    string fromZoneId,
    string tozoneId)
{
    LocalDateTime fromLocal = LocalDateTime.FromDateTime(fromDateTime);
    DateTimeZone fromZone = DateTimeZoneProviders.Tzdb[fromZoneId];
    ZonedDateTime fromZoned = fromLocal.InZoneLeniently(fromZone);

    DateTimeZone toZone = DateTimeZoneProviders.Tzdb[toZoneId];
    ZonedDateTime toZoned = fromZoned.WithZone(toZone);
    LocalDateTime toLocal = toZoned.LocalDateTime;
    return toLocal.ToDateTimeUnspecified();
}
Run Code Online (Sandbox Code Playgroud)

注意InZoneLeniently这里的使用- 这意味着如果你给出的本地时间由于UTC偏移的跳跃(通常由于夏令时)而无效,它仍将返回一个值而不是抛出异常 - 请参阅文档更多细节.

如果你在整个过程中使用Noda Time,很难知道这个方法会是什么样子,因为我们不知道你是从一个LocalDateTime还是一个开始ZonedDateTime.例如,您可以:

public static LocalDateTime ConvertDateTimeToDifferentTimeZone(
    LocalDateTime fromLocal,
    string fromZoneId,
    string tozoneId)
{
    DateTimeZone fromZone = DateTimeZoneProviders.Tzdb[fromZoneId];
    ZonedDateTime fromZoned = fromLocal.InZoneLeniently(fromZone);

    DateTimeZone toZone = DateTimeZoneProviders.Tzdb[toZoneId];
    ZonedDateTime toZoned = fromZoned.WithZone(fromZone);
    return toZoned.LocalDateTime;
}
Run Code Online (Sandbox Code Playgroud)