delphi - 是否有 Delphi EncodeDate/DecodeDate 函数版本可以处理 B.C.日期?

标签 delphi date encoding negative-number

Delphi 函数 EncodeDate/DecodeDate 似乎只能处理 1.1.0001 之后的日期。是否有一些 EncodeDate/DecodeDate 的实现可以处理 B.C. tDateTime 值?

最佳答案

AFAIK TDateTime 是一种 Windows 基本类型,常见于 COM、Variants、DotNet 和 Delphi。负值可用于 1899 年之前的日期。

但这并不是那么简单 - 因为负值会带来一些麻烦,如 stated by this page :

The integral part is the date, the fraction is the time. Date.time. That's easy. Things get odd when the value is negative. This is on or before #12/30/1899#.

With modern dates time always runs forwards, as you would suspect. With negative historical dates time actually runs backwards! Midnight #1/1/1800# equals −36522, but noon #1/1/1800# is −36522.5 (less than midnight!) and one second before midnight is −36522.9999884259 (even less). At midnight the clock jumps forward to -36521, which equals #1/2/1800#. The decimal fraction still shows the time and the integral part is the date, but each second decrements the clock while each new day increments it, not just by 1, but by almost 2. Negative times are really counterintuitive.

To make things worse, time values for #12/30/1899# are ambigous in two ways. First, a time value without a date equals that time on #12/30/1899#. This means that 0.5 is either noon or noon on #12/30/1899#, depending on context. Zero is either midnight, #12/30/1899# or midnight #12/30/1899#. The other ambiguity is that all time values come in double for #12/30/1899#. 0.5 is noon #12/30/1899#, but -0.5 is noon #12/30/1899# as well. The integral part is the date, the fraction is the time. Another surprise is here: #12/30/1899 11:59:59 PM# - #12/29/1899 11:59:59 PM# = 2.99997685185185. Not 1, what you normally would expect for a 24-hour period. Be careful when working with historical dates.

据我所知,EncodeDate/DecodeDate 的当前实现可以工作,但是在使用负值或接近于零的 TDateTime 值时可能会遇到麻烦...

您最好使用自己的时间格式,例如ISO-8601 或这样的简单记录:

TMyDateTime = packed record
  Year: SmallInt;
  Month: Byte;
  Day: Byte;
end;

当计算有关持续时间或显示日期/时间的内容时,您必须意识到“我们的时间”不是连续的。因此,使用 TDateTime=double 技巧进行的计算并不总是按预期工作。例如,我记得Teresa of Avila died in 1582, on October 4th ,正如天主教国家从儒略历转换为公历一样,这需要从日历中删除 10 月 5 日至 14 日。 :)

关于delphi - 是否有 Delphi EncodeDate/DecodeDate 函数版本可以处理 B.C.日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856757/

相关文章:

delphi - 自动调整列标题

javascript - 通过 JavaScript 对表中的日期进行排序

delphi - 为什么 Delphi FMX 的字体大小相差 1.35 倍(太小)

delphi - 如何将http下载图像从扩展转换为另一个?

delphi - 禁用/停用具有已发布属性的自定义组件

PHP:如何获得星期几?

date - application.yaml 和属性中的 Spring 启动日期处理

java - 当我们在 url 中提供编码值和英文字符时,Endeca 查询中的 Nrs 不会获取结果

objective-c - 将字符集名称转换为 NSStringEncoding

c# - 如何检查文件编码是否正确(对应于国家/代码页)?