简体   繁体   English

为什么.weekday日期部分迅速从1开始?

[英]Why does .weekday date component start at 1 in swift?

The .weekday component starts at 1 (sunday = 1, monday = 2 etc...) and I'm interested if anyone knows why. .weekday组件从1开始(星期日= 1,星期一= 2等等),如果有人知道原因,我很感兴趣。 It seems that usually in programming things start at 0. 似乎在编程中通常从0开始。

The reason for zero based indexing in programming dates back to the time when programs were written in machine language or assembly code. 编程中从零开始的索引的原因可以追溯到用机器语言或汇编代码编写程序的时间。 It is a reflexion of the base+displacement capability of memory access from CPU registers. 这是对来自CPU寄存器的内存访问的基址+移位功能的反映。 It was maintained in low level programming languages (such as C) that were essentially a bridge to assembly code. 它使用低级编程语言(例如C)进行维护,这些语言本质上是汇编代码的桥梁。 Zero based indexing also provides much simpler index manipulation when processing a one dimensional array (or memory block) as a multidimensional matrix. 当将一维数组(或存储块)作为多维矩阵处理时,基于零的索引还提供了更简单的索引操作。 That being said, it is still just a convention. 话虽如此,它仍然只是一个惯例。 Some languages (such as Pascal) use one based indexing and normal human beings don't start numbering things at zero. 某些语言(例如Pascal)使用基于1的索引,而普通人类不会从零开始编号。

I don't know the fundamental reason for the numbering of weekdays being based on 1 but I strongly suspect that it is more consistant (and practical) to use with calendars where day numbers within a month, and months with a year are also 1 based. 我不知道以1为基础的工作日编号的根本原因,但我强烈怀疑与日历配合使用(日历在一个月内以及一年中的月份也以1为基础)更加一致(实用) 。 It would be very confusing to manipulate days and months as zero based indexes. 将天和月作为基于零的索引进行操作将非常令人困惑。 Given this, weekdays should follow the same conventions. 鉴于此,工作日应遵循相同的惯例。

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

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