简体   繁体   English

为什么Perl的localtime函数(和C的tm结构)中的年份值相对于1900?

[英]Why is the year value in Perl's localtime function (and C's tm struct) relative to 1900?

As all Perl programmers (hopefully) know, the year value from a call to Perl's localtime function is relative to 1900. Wondering why this was, I took a look at the perldoc for localtime , and found this interesting nugget: 正如所有Perl程序员(希望)都知道的那样,调用Perl的localtime函数的年份值相对于1900年。想知道为什么会这样,我看了一下perldoc for localtime ,并找到了这个有趣的块:

All list elements are numeric and come straight out of the C `struct tm'. 所有列表元素都是数字的,直接来自C`struct tm'。

Looking then at the C++ reference for the tm struct , I found that the tm_year member variable is declared as an int . 然后看一下tm结构C ++引用 ,我发现tm_year成员变量被声明为int

Question: Why, then, is the year value relative to 1900 and not simply the full, four-digit year? 问题:那么,为什么年份值相对于1900而不仅仅是完整的四位数年份呢? Is there some historical reason? 有一些历史原因吗? It seems to me that, even with early memory limitations in computing, an integer is (obviously) more than sufficient for storing the full year. 在我看来,即使计算中的早期内存限制,整数(显然)足以存储全年。 There must have been a good reason; 一定有充分的理由; I'm curious as to what that might be. 我很好奇这可能是什么。

I was programming in the early 1970's, before C and Unix were invented. 在C和Unix发明之前,我在1970年代早期编程。 Two digit years were used to save disk space which was very very tight, we were always trying to figure tricks like that to save it. 两位数的年份用于节省非常紧张的磁盘空间,我们总是试图用这样的技巧来保存它。 The first machine I worked on had two 20 megabyte drives, each the size of a washing machine. 我工作的第一台机器有两个20兆字节的驱动器,每个驱动器都是一台洗衣机。

I worked at a hospital that had a Y2K problem in 1975. The age of a patient is an important thing to know, and the date of birth only had a two digit year. 我在一家1975年患有Y2K问题的医院工作。病人的年龄是一件很重要的事情,出生日期只有两位数。 Being a hospital, we obviously had some very old patients, born in the 1800's. 作为一家医院,我们显然有一些非常老的患者,出生于1800年代。 The system assumed that anyone whose year of birth was 75 or over was born in the 1800's. 该系统假设任何出生年份为75岁或以上的人出生于1800年代。 This worked well for people born in 1890, but once Jan 1, 1975 hit, all heck broke loose as the newborns were deemed to be 100 years old. 这对于1890年出生的人来说效果很好,但是一旦1975年1月1日到来,当新生儿被认为是100岁时,所有人都会失败。 (It was also a major maternity hospital.) We ran around fixing that problem by moving the threshold from 75 to 80. It was also my first understanding of what a problem Y2K was going to be, and I realized I would be better off doing something else by the year 2000. I failed. (它也是一家主要的妇产医院。)我们通过将阈值从75移到80来解决这个问题。这也是我第一次理解Y2K会出现什么问题,我意识到我会做得更好到了2000年的其他事情。我失败了。

Those who think Y2K was not a real problem because nothing happened do not understand the amount of work that went into fixing stuff for the few years beforehand. 那些认为Y2K不是真正问题的人,因为什么都没发生,所以不了解事先几年修理东西的工作量。

When computers started to keep track of time, the year was referenced only with 2 digits. 当计算机开始跟踪时间时,年份仅以2位数字引用。 As for the "why" part, i haven't heard any definite reason, other than "They wouldn't have to worry about that for another 30 years". 至于“为什么”部分,我没有听到任何明确的理由,除了“他们不必再担心这30年”。 When clocks got dangerously close to year 2000, and people realized that their code needed to keep track of time after 1999 as well, most software was still using 2 digit years (afterall, having everyone swap to 4 digits would break a lot of code unless 100% swapped). 当时钟危险地接近2000年时,人们意识到他们的代码需要在1999年之后跟踪时间,大多数软件仍然使用2位数年份(毕竟,每个人都换成4位数会打破很多代码,除非100%互换)。 The workaround for dodging the Y2K bug was to make the year a reference to 1900. 躲避Y2K错误的解决方法是将年份作为1900年的参考。

It's one of these cases where it coulda shoulda woulda been done right in the first place. 这是其中一个案例,它本来应该是正确的。 (QWERTY keyboard layout was originally designed to be inefficient. Also, the current in an electronic circuit is dictated as going from + to -, while in reality, electrons actually move the opposite way). (QWERTY键盘布局最初设计效率低。而且,电子电路中的电流被指定为从+到 - ,而实际上,电子实际上是以相反的方式移动)。

This is speculation. 这是猜测。

Back when Unix was invented there were machines in use that had 36-bit words, and one popular unpacking of those words was 4 9-bit characters. 当Unix被发明时,使用的机器有36位字,而这些字的一个流行的解包是4个9位字符。 If tm_year is set to year - 1900, then all the individual struct tm fields would fit into 9-bit unsigned chars. 如果将tm_year设置为year - 1900,那么所有单独的struct tm字段将适合9位无符号字符。 Memory was very tight on those old machines, so packing data tightly into structs was a bigger concern then than it is today. 这些旧机器的内存非常紧张,因此将数据紧密地打包到结构中是一个比现在更大的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM