零个月编号 [英] Zero-based month numbering

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

问题描述

一些流行的编程语言使用月份编号,这是一个关闭的1 - JavaScript的想法,和Java一样,如果内存服务,C是另一个。我有一些问题:




  • 如果你将忽略外行人使用的月份编号,那为什么不是为了一致性

  • 为什么这么常见?

  • 这是谁的想法是首先?


解决方案

使用零开始计数实际上是汇编程序员的优化技巧。而不是将1分配给计数寄存器,它们将自己的寄存器异或,这在CPU周期中稍快。这意味着计数将从0开始,并且始终取决于元素的长度,不包括最后一个元素。



此外,使用零也受到指针的欢迎算术运算符,您可以使用指向某个分配的内存的一个基本指针,再加上与该基准指针偏移的第二个指针。这里,使用值零可以将偏移指向内存块的基础。 (通用阵列逻辑往往是基地址加偏移x记录大小。)



和基于零的月份号码?通常情况下,许多编程环境会将数据计算为一些默认数据的天数。 1899年12月31日是一个受欢迎的日期,虽然有很多其他日期用作基准日期。所有其他日期偏离此基数,并将仅存储为一个单个数字。分数将用于指示小时,分钟和秒,其中0.25将为24/4 = 6小时。因此,要将日期转换为实际日期,所有环境都要将该数字转换为实际日期。



然而,基于零的数组的组合和1个月的价值确实带来了问题。要获取月份9的月份名称,您必须从月数组中获取项目8。有些开发者会很高兴在取得名称之前减少月份号码。其他人喜欢将这个月改为零,因为人们只想知道名字,而不是数字。这是个人看法。


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:

  • 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?

解决方案

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.

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

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.

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天全站免登陆