在格式化 NodaTime 即时时包括尾随零?

Including trailing zeroes when formatting NodaTime instant?

使用 NodaTime 2.4.x

如果我创建一个 Instant,它在一秒钟内开始爆炸,然后尝试使用包含毫秒/纳秒的 Pattern 对其进行格式化,则尾随零将被截断。

例如

using System;
using NodaTime;
using NodaTime.Text;

public class Program
{
    private static readonly InstantPattern _instantPattern 
            = InstantPattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFF'Z'");

    public static void Main()
    {
        var instantRound = Instant.FromUnixTimeMilliseconds(158507100000);
        var instantNotRound = Instant.FromUnixTimeMilliseconds(158507100001);
        Console.WriteLine(_instantPattern.Format(instantRound));
        Console.WriteLine(_instantPattern.Format(instantNotRound));
    }
}

产出

1975-01-09T13:45:00Z
1975-01-09T13:45:00.001Z

You can see this here

无论如何我可以得到如下输出,最好使用内置方法:

1975-01-09T13:45:00.000000Z
1975-01-09T13:45:00.001000Z

基本上,保留尾随零。

模式说明符可以参考this pageF 将导致无关紧要的尾随数字被删除。 f不会。

您还使用了错误的方法名称。应该是FromMillisecondsSinceUnixEpoch,不是FromUnixTimeMilliseconds.

private static readonly InstantPattern _instantPattern 
        = InstantPattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;ffff'Z'");

public static void Main()
{
    var instantRound = Instant.FromMillisecondsSinceUnixEpoch(158507100000);
    var instantNotRound = Instant. FromMillisecondsSinceUnixEpoch(158507100001);
    Console.WriteLine(_instantPattern.Format(instantRound));
    Console.WriteLine(_instantPattern.Format(instantNotRound));
}