Get the latest tech news
Rethinking the C Time API
Out of all the components of C, its time API is probably the one most plagued with legacy cruft. To the point almost every regularly used element of it has some design decision that’s been obsolete for decades.
time() unnecessarily takes a pointer argument to write to strftime() has to write to a string of a fixed length it can not dynamically allocate(This is less legacy than it is bad design) localtime() needs the pointer to a time_t value even though it does not change it because of register size concerns on PDP-11’s sleep() cannot sleep for sub-second amounts of time, usleep() is deprecated and it’s alternative nanosleep() requires you to define variables Set TZ to the timezone name Call tzset()(which secretly provides good data to localtime) Give the time_t form of the time to localtime_r(so the global variable localtime keeps doesn’t get overwritten) Get tm_gmtoff and tm_zone(On musl, tm_zone is overwritten whenever a new timezone is loaded, which means the string has to be duplicated and therefore it must be the users job to free it) Set TZ back to whatever it was The time library in C is largely terrible because it is made entirely out of non-tessellating ideas and hacks, and has constantly resisted improvement by standardization.
Or read this on Hacker News