Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why a signed int?


Using a signed int allows you to represent times before 1970. This is not just theoretical; it’s done in the wild. Materialize, the data warehouse software I work on, used to have a bug causing it to fail on negative Unix times and we found out when somebody tried to load in a database with human dates of birth.


2018-July-22 was the last day "that wouldn't work for all people" excuse was true.

Chiyo Miyako was born on 1901-May-02


Yeah, but now when you try to load in a database of documents (e.g. legal, blueprints, etc...), the date the document was created crashes everything instead.


time_t has historically been a typedef to a 32-bit signed integer, so the signedness is baked into a lot of legacy software still running today.


Afaik this is so that you can go back in time before epoch (less than 0)


Then why not start the epoch however far back they thought they need to go back? Maybe a naive question.


Whether you make 0 be in 1902 and use an unsigned type, or make 0 be in 1970 and use a signed type, the range of representable dates is the same.


Sure. But (and what I should have added) I don't think they would need to go back that far.

But you're right, you can get the same results with both.


As others have pointed out in the thread, it was already not enough to represent the dates of birth of plenty of people who were still alive in 1970.


Good point. :)


You may need an arbitrary distance back in time, so really any epoch is as good as any other. So if you're going to pick arbitrarily, zero around where most numbers are going to be when you set the standard seems to make sense.


time_t is now 64 bits.[1] The change probably happened around 2014.[2]

[1] https://gcc.godbolt.org/z/Wv99v4W6E

[2] https://lwn.net/Articles/598408/


Converting to unsigned int only changes the first digit (6 becomes e) but I think `e5075ea9` is a little easier to reason about.


Was the negative sign commonly used for delta times?


The epoch was set very close to the start of UNIX (it started development in 1969, before epoch 0). Negative epoch dates are quite common, e.g. many people's birthdays.


In 1970, I suppose there are many people whose birthdate is before 1901-12-13?


0 UNIX epoch is 1970-01-01. So everyone between 1901 and 1970 have negative birthdays in UNIX time. E.g. Brian Keringhan's birthday is -881107200 in UNIX time.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: