野田时代与远古时代

And*_*ndy 2 c# nodatime

我正在使用 WikiPedia 中的一些值,这些值在某些情况下是数百万年前的(例如月球的形成,WikiData 报告其正在形成:“-4527000000-01-01T00:00:00Z”。在其他情况下,我'我只是看看可能是公元前 10000 年等的年份,仍然表示为“-10000-01-01T00:00:00Z”。

据我所知,测试 NodaTime 并阅读文档,不支持这些古老的年份(或者我忽略了一些东西)。例如,这失败了:

OffsetDateTime defaultValue = new OffsetDateTime(new LocalDateTime(77, 1, 1, 0, 0), Offset.Zero);
var pattern = OffsetDateTimePattern.Create("yyyy-MM-ddTHH:mm:ss'Z'", CultureInfo.InvariantCulture, defaultValue);
string P = "-1000-01-25T20:34:25Z";
var result = pattern.Parse(P);

NodaTime.Text.UnparsableValueException: The value string includes a negative value where only a non-negative one is allowed. Value being parsed: '^-1000-01-25T20:34:25Z'. (^ indicates error position.)
   at NodaTime.Text.ParseResult`1.GetValueOrThrow() in C:\Users\jon\Test\Projects\nodatime\build\releasebuild\src\NodaTime\Text\ParseResult.cs:line 81
   at TryNodaTime.Program.Main(String[] args
Run Code Online (Sandbox Code Playgroud)

顺便说一句,使用 sing 这个字符串可以正常工作: string P = "1000-01-25T20:34:25Z";

我希望我只是忽略了一些明显的处理非常大的旧日期/年份的事情。其他例子包括老城市顺便说一句,它们的确切日期早在 0 年之前。

关于如何处理这些问题的指导将不胜感激。

编辑:发现这个例子,它要求我首先手动检测年份,如果它是负数,然后使用 LocalDate,但是,这里没有解决更大的问题,因为它在 -9999 以下失败:

LocalDate BCDATE = new LocalDate(Era.BeforeCommon, -15000, 10, 1);
Console.WriteLine($"BC: {BCDATE}");
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

Noda Time 不支持古老或遥远的未来值。

支持的值范围在用户指南中

Noda Time can only represent values within a limited range. This is for pragmatic reasons: while it would be possible to try to model every instant since some estimate of the big bang until some expected "end of time," this would come at a significant cost to the experience of the 99.99% of programmers who just need to deal with dates and times in a reasonably modern (but not far-flung future) era.

The Noda Time 1.x releases supported years between around -27000 and +32000 (in the Gregorian calendar), but had significant issues around the extremes. Noda Time 2.0 has a smaller range, with more clearly-defined behaviour.

...

Additionally, all calendars are restricted to four digit formats, even in year-of-era representations, which avoids ever having to parse 5-digit years. This leads to a Gregorian calendar from 9999 BCE to 9999 CE inclusive, or -9998 to 9999 in "absolute" years. The range of other calendars is determined from this and from natural restrictions (such as not being proleptic).

I'm afraid I think it may well be better not to use Noda Time at all for this use case, rather than trying to work around it - unless you're okay with effectively reporting the far-distant dates verbatim and doing no other work with them.

Sorry about this - it's one of those situations where trying to support all use cases does harm to the far more common use case :(