5 ms·
Why is day of the month 1-indexed but the month is 0-indexed in C?
- codezero 6y agoIt’s be funny if this went back further to some pre Unix telco system, my hunch was some date conversion optimization.
- TYMorningCoffee 6y agoDoesn't predate Unix according to him > I stopped at Unix 5, from 1974. It didn't even have a <time.h> file! In its <ctime.c> file, it stores the date as an array of integer values. The `tm` struct only starts appearing in Unix 7. https://mobile.twitter.com/hillelogram/status/1329228438679531521 https://mobile.twitter.com/hillelogram/status/13292284386795...
- SloopJon 6y agoAlthough the source for the fourth edition is not available, the man page for the ctime(3) library function is: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4/man/man3/ctime.3 https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4/man/man3/ct... It confirms that the day of the month element was one based. This function is different in earlier editions: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V3/man/man3/ctime.3 https://minnie.tuhs.org/cgi-bin/utree.pl?file=V3/man/man3/ct... https://minnie.tuhs.org/cgi-bin/utree.pl?file=V1/man/man3/ctime.3 https://minnie.tuhs.org/cgi-bin/utree.pl?file=V1/man/man3/ct...
- noncoml 6y agoSummary: For historical reasons. When displaying the day of the month, we wanted to print "1" so we stored that. For months, we wanted to print a 3 letter abbreviation, so we had a string ("JanFebMar...") and we stored an index-0 value to start at the first entry of the months string.
- kbumsik 6y agoSo the day of the month is just a number but the month is index, I guess.
- TYMorningCoffee 6y agoThank you for the summary. These twitter threads are difficult to read, especially on mobile.
- drallison 6y agoEverything is 0-indexed in C. It is convenient for each day in a month to correspond to it's ordinal position.For example, the Ides of March would be entry 15 in the structure containing data for the month of March. There is no 0-th day in a month so there are extra, unused entries but this is better than continually changing day values at each reference. The months are just a list and so are 0-indexed and dense.
- kube-system 6y agoDays are natural numbers. Months are a string array. It seems intuitive to me. If you looked at a calendar and typed the data, that’s what you’d end up with.
- deepsun 6y agoMonth is not always string array, for example in ISO: 2020-11-23. And days can be string as well: "1st November".
- kube-system 6y agoIt was a string array first. ISO came long after the Gregorian calendar. And the months came from the Julian calendar. If we go all the way back, one could say it was a mixed array of string and integer. September (7), October (8), November (9), and December (10) are linguistically ordinal names... but they’re all off by two since the deprecation of the Roman calendar.
- 082349872349872 6y agoSince Julius and Augustus. (written 4 Freezy 229)
- _Microft 6y agoIt was a list of names long before it was considered "a string array".
- jsrjenkins 6y agoActually the Romans counted their months beginning with March: March (1) April (2) May (3) ... July (5) (named after Julius Caesar) August (6) (named after Augustus) September (7) ... etc. The reason being in the ancient world all serious activity begins in the spring, especially war campaigns which were to be under the aegis of the god Mars, from which the name of the month. Note that when we have a leap year we add the extra day to the end of the Roman year, that is at the end of February which is the last month of the year. More precisely, we duplicate the sixth of the calends of March. This is why in French the leap year is called 'bisextile'.
- ternaryoperator 6y agoIt's not just in C, by the way. It's the same in the Java Date class [0] [0] https://docs.oracle.com/javase/7/docs/api/java/util/Date.html https://docs.oracle.com/javase/7/docs/api/java/util/Date.htm...
- CornCobs 6y agoI learnt this the hard way the other day that this is the case with Postgres too. Its very jarring in my opinion that we have to write date(1, 0, 2020) to mean 1st Jan - the date and year are fine but we suddenly need to minus one in the middle
- imglorp 6y agoOne has to wonder how many gigajoules have been saved on Earth because of that one optimization. There are a lot of date columns out there.
- deleted 6y ago[deleted]
- andreareina 6y agoIs make_date not available in your installation? https://www.postgresql.org/docs/9.5/functions-datetime.html#FUNCTIONS-DATETIME-TABLE https://www.postgresql.org/docs/9.5/functions-datetime.html#...
- banana_giraffe 6y agoAn unrolled version of thread, I find it easier to read this way: https://threadreaderapp.com/thread/1329228419628998665.html https://threadreaderapp.com/thread/1329228419628998665.html
- alisonkisk 6y agoQuintessential illustration of "premature optimization is the root of all evil". A tiny shortcut in non-performance critical formatting code causes 50 years of pain and bugs.
- globular-toast 6y agoThe answer is right there in the text from K&R. `tm_mday` is day of the month, `tm_mon` is months since January. There's nothing else to it really. The first month of the year is not "1", it's January. In any case, this is all about how we display dates and it's all rather silly and it's best not to think about it too much.
- hwayne 6y agoI'm planning on writing this up into a full blogpost, so people won't have to deal with the terrible Twitter UI. I just like Twitter because it's a fun writing constraint ¯\_(ツ)_/¯
- zhanghc 6y agoavoid zero-day bugs?