从零开始的月份编号 [英] Zero-based month numbering

查看:47
本文介绍了从零开始的月份编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些流行的编程语言使用以 1 为单位的月份编号——我会想到 JavaScript,Java 也是如此,如果没记错的话,C 是另一种.我有几个问题:

Some popular programming languages use month numbering which is off by 1 -- JavaScript comes to mind, as does Java, and if memory serves, C is another. I have some questions:

  • 如果您要忽略非专业人士使用的月份编号,那么为什么不为了一致性也忽略非专业人士使用的编号,并从以下日期开始编号每个月的天数?0 ?
  • 为什么这种情况如此普遍?
  • 这最初是谁的主意?
  • If you are going to be ignoring the month numbering used by laypeople, then why not for the sake of consistency also ignore the day numbering used by laypeople, and number the days in each month starting from 0 ?
  • Why is this so common?
  • Whose idea was this in the first place?

推荐答案

使用零开始计数实际上是汇编程序员的一个优化技巧.他们没有将 1 分配给计数寄存器,而是将寄存器与自身进行异或运算,这在 CPU 周期中稍微快一些.这意味着计数将从 0 开始,并且始终达到元素的长度,不包括最后一个.

The use of zero to start counting is actually an optimization trick from Assembly programmers. Instead of assigning 1 to the count register, they XOR'ed the register with itself, which was slightly faster in CPU cycles. This meant that counting would start with 0 and would always be up to the length of elements, excluding the last one.

此外,零的使用在指针算术中也很流行,在这种情况下,您将使用一个指向某个已分配内存的基指针,再加上一个位于该基指针偏移处的第二个指针.在这里,使用零值将偏移量指向内存块的基址很有意义.(一般的数组逻辑往往是基地址加上偏移量 x 记录大小.)

Also, the use of zero is also popular with pointer arithmetics where you would use one base pointer pointing at some allocated memory, plus a second pointer which would be at an offset from this base pointer. Here, using the value zero makes a lot of sense to point the offset to the base of the memory block. (General array logic tends to be the base address plus the offset x record size.)

从零开始的月份数字?通常,许多编程环境将数据计算为自某些默认数据以来的天数.1899 年 12 月 31 日是一个受欢迎的日期,尽管有很多其他日期用作基准日期.所有其他日期都从这个基数偏移,并且将仅存储为一个数字.分数将用于表示小时、分钟和秒,其中 0.25 表示 24/4 = 6 小时.因此,要将日期转换为真实日期,环境所需要做的就是将这个数字转换为真实日期.

And zero-based month numbers? Often, many programming environments calculate the data as a number of days since some default data. December 31, 1899 is a popular date, although there have been plenty of other dates used as base date. All other dates are offset from this base, and will just be stored as one single number. Fractions would be used to indicate hours, minutes and seconds, where 0.25 would be 24/4 = 6 hours. Thus, to transform a date into a real date, all the environment has to do is transform this number into a real date.

但是,从零开始的数组和从 1 开始的月份值的组合确实带来了问题.要获得第 9 个月的月份名称,您必须从月份数组中获取第 8 项.一些开发人员会很高兴在获得它的名字之前减少月份数.其他人更喜欢将月份更改为从零开始的月份,因为人们只想知道名称,而不是数字.个人观点.

However, the combination of zero-based arrays and 1-based month values does bring a problem. To get the month name of month 9, you would have to get item 8 from the month array. Some developers would be happy with decreasing the month number before getting it's name. Others preferred to change the month into something zero-based since people just want to know the name, not the number. It's a personal view.

这篇关于从零开始的月份编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆