输出一个 ISO 8601 字符串

Output an ISO 8601 string

我们如何在特定时区创建date/time并输出a short ISO 8601 date/time with offset from UTC?例如,2015 年 9 月 8 日太平洋标准时间下午 5 点必须如下所示:

2015-09-08T17:00:00-07:00

这是我目前的尝试。

using System;
using NodaTime;
using NodaTime.Text;

namespace ConsoleApplication1_Nodatime
{
    class Program
    {
        public static void Log(string x) => Console.WriteLine(x);
        public static void Read() => Console.ReadLine();

        static void Main(string[] args)
        {
            var localDateTime = new LocalDateTime(2015, 09, 08, 17, 0);
            var zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/Vancouver");
            var zonedDateTime = localDateTime.InZoneStrictly(zone);
            Log(zonedDateTime.ToOffsetDateTime().ToString());
            Read();
        }
    }
}

这可行,但似乎步骤太多。

  1. 创建一个LocalDateTime
  2. 创建一个DateTimeZone
  3. LocalDateTime 转换为 ZonedDateTime
  4. ZonedDateTime 转换为 OffsetDateTime

我们如何以更少的步骤做到这一点?

如问题评论中所述,您只需要 ZonedDateTime 即可实现所需的格式(不需要 OffsetDateTime)。格式字符串传递 "general" 偏移量模式,以便仅当偏移量包含分钟数时才应包含分钟数 ("medium format")。

zonedDateTime.ToString(
    "yyyy-MM-ddTHH:mm:sso<g>", 
    System.Globalization.CultureInfo.InvariantCulture)

为简洁起见,这里记录了可用的偏移模式:

  • f: Full format, displaying all information including fractional seconds. Typical pattern text: +HH:mm:ss.fff
  • l: Long format, displaying information down to the second. Typical pattern text: +HH:mm:ss
  • m: Medium format, displaying information down to the minute. Typical pattern text: +HH:mm
  • s: Short format, displaying information down to the hour. Typical pattern text: +HH
  • g: General pattern. Formatting depends on the value passed in:
    • If the offset has fractional seconds, the full format is used; otherwise
    • If the offset has seconds, the long format is used; otherwise
    • If the offset has minutes, the medium format is used; otherwise
    • The short format is used When parsing, the other standard format patterns are tried one at a time. This is the default format pattern.
  • G: As g, but using Z for an offset of 0, as if it were Z-prefixed.

来源:http://nodatime.org/1.3.x/userguide/offset-patterns.html

原始问题

对于格式为 ISO-8601 的原始请求始终显示尾随分钟数,您可以使用下面的自定义格式字符串。默认情况下,它符合不需要尾随“:00”的 ISO-8601 标准。但是,您可以传递一个偏移模式来强制使用您想要的格式:

zonedDateTime.ToString(
    "yyyy-MM-ddTHH:mm:sso<m>", 
    System.Globalization.CultureInfo.InvariantCulture)

来源:http://nodatime.org/1.3.x/userguide/offset-patterns.html

更新以缩短代码

如果您只是想缩短代码,您始终可以将代码包装在辅助方法中 - 甚至可能作为静态扩展方法。

public static class NodaTimeHelpers
{
    public static Lazy<DateTimeZone> Zone = new Lazy<DateTimeZone>(
        () => DateTimeZoneProviders.Tzdb.GetZoneOrNull("America/Vancouver"));
    public static string ToStringWithOffset(this LocalDateTime localDateTime)
    {
        if (localDateTime == null)
            return "";
        var zonedDateTime = localDateTime.InZoneStrictly(Zone.Value);
        return zonedDateTime.ToString(
            "yyyy-MM-ddTHH:mm:sso<g>",
            System.Globalization.CultureInfo.InvariantCulture);
    }
}

这使您的本地日期时间对象可以非常轻松地转换为字符串:

localDateTime.ToStringWithOffset();