TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

History of Zero-Based Months?

52 点作者 expjpi超过 2 年前

7 条评论

schot超过 2 年前
&quot;Why is day of the month 1-indexed but the month is 0-indexed in C?&quot;<p>This Twitter thread from November 2020[1] and its HackerNews discussion[2] seem relevant.<p>1: <a href="https:&#x2F;&#x2F;twitter.com&#x2F;hillelogram&#x2F;status&#x2F;1329228419628998665" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;hillelogram&#x2F;status&#x2F;1329228419628998665</a><p>2: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=25195287" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=25195287</a>
评论 #32617529 未加载
gerikson超过 2 年前
At least in Perl, the rationale for this (apart from copying C) is that it&#x27;s very easy to reference a list (array) of month names if the month indices are zero-based.
评论 #32617355 未加载
nayuki超过 2 年前
Zero-based months is awkward when printing as a number, but convenient when indexing an array:<p><pre><code> const MONTHS = [&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, ..., &quot;Dec&quot;]; console.log(MONTHS[d.getMonth()]);</code></pre>
评论 #32620906 未加载
评论 #32621853 未加载
评论 #32622082 未加载
lofatdairy超过 2 年前
The same justification exists in JS (where it&#x27;s copied from Java which copies it from C). However, interestingly checking the FORTRAN IV specification documents for the PDP-10, it&#x27;s not implemented as a 3 letter ASCII abbreviation. That document dates to 1975, I don&#x27;t know if I can find if the date exists in the first version of the document, which should date to 1967. I was unable to find a reference to a builtin in the base FORTRAN II, which only provides 20 builtin functions, date not making the list. I think newer versions of FORTRAN77 has idate which is 1-indexed but I couldn&#x27;t find it in the older standard listed on wg5&#x27;s specification documents.<p>[^1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;PDP-10&#x2F;f40&#x2F;tree&#x2F;master&#x2F;doc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;PDP-10&#x2F;f40&#x2F;tree&#x2F;master&#x2F;doc</a>
photochemsyn超过 2 年前
I think it all comes down to whether you&#x27;re counting elements as the primary goal. However, months are poorly defined relative to something like an astronomical year or a standard day, so they&#x27;re more like objects. That is to say, adding different months doesn&#x27;t give you the same number of days as a result.<p>Hence, dealing with months is perhaps a bit more like indexing variable-sized objects, and for that purpose traditionally, the array-of-pointers approach uses zero-based pointer arithmetic, which is what the designers might have been thinking.<p>Really however, the months should probably be treated more like structs, with the number of days in that month being a data member. This would allow sanity checks, i.e. entering Feb 30 should raise an error, but not May 30.
_OOps_超过 2 年前
I think it&#x27;s important to be able to work with mod(%) 12 for some operations on year &lt;--&gt; month relationship. All years have 12 months. For day of the month you need additional logic to handle diferent lengths... and other issues.
评论 #32624100 未加载
评论 #32618023 未加载
11235813213455超过 2 年前
&gt; One-based indexing for the year and day,<p>Year is 0 based too
评论 #32623070 未加载