野田时代与古代

NodaTime and Ancient Times

我正在使用来自维基百科的一些值,这些值在某些情况下是数百万年前(比如月球的形成,维基数据报告它正在形成:“-4527000000-01-01T00:00:00Z” . 在其他情况下,我只是在看可能是 10000 B.C 的年份,等等,仍然表示为“-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

使用唱这个字符串效果很好顺便说一句:string P = "1000-01-25T20:34:25Z";

我希望我只是忽略了一些明显的东西来处理非常大的旧 dates/years。其他例子包括古老的城市,顺便说一句,它们的确切日期早于 0 年。

非常感谢有关如何处理这些问题的指导。

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

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

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

支持的取值范围是in the user guide

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).

恐怕我认为对于这个用例最好不要使用 Noda Time,而不是尝试解决它 - 除非你可以有效地逐字报告遥远的日期并且不与他们一起做其他工作。

对此感到抱歉 - 这是尝试支持 所有 用例会对更常见的用例造成损害的情况之一:(