Kanbaru 🌟 (one hikari of too many)

interesting technical puzzle, perhaps an interview question: how would you convert a UNIX timestamp to a calendar date (YYYY-MM-DD)? with minimal research!

well, I now have my own answer to this :3 https://github.com/hikari-no-yume/touchHLE/commit/c94a8311fa8140068b7d187c34dfa3110955d31b

2 replies β†’
2 replies

Ridley @ WATCH LYCORECO

(replying to Kanbaru 🌟 (one hikari of too many))

@hikari does "I've researched how libraries do this before and I'm pretty sure I could reimplement that from memory" count

Kanbaru 🌟 (one hikari of too many)

(replying to Ridley @ WATCH LYCORECO)

@rcombs perhaps! I'm curious what strategy they use, do they also do (timestamp % 400yearperiod) but then perhaps divide by 365 and do some sort of trick to turn it into a correct integer?

Ridley @ WATCH LYCORECO

(replying to Kanbaru 🌟 (one hikari of too many))

@hikari musl approach: simple, all fairly intelligible numbers; lots of modulo; some explicit branching for edges: github.com/bminor/musl/blob/f5
the libcxx approach: just do a bunch of divides by constants lol github.com/llvm/llvm-project/b

Kanbaru 🌟 (one hikari of too many)

(replying to Ridley @ WATCH LYCORECO)

@rcombs heh, thanks. both pretty elegant in their own ways!


Saagar Jha

(replying to Kanbaru 🌟 (one hikari of too many))
@hikari > with minimal research

β€œI can’t do this because time is hard”

Kanbaru 🌟 (one hikari of too many)

(replying to Saagar Jha)

@saagar I did actually do a bit of research for this… I went to the Wikipedia page for the Gregorian calendar to check I remembered the leap year algorithm correctly, how long its cycles are, and also to remind me how long each month of the year is…

but the constants I ended up using can be trivially derived from things most people know (how long is a year, what is a leap year, how long is an hour, how long is a day)

Saagar Jha

(replying to Kanbaru 🌟 (one hikari of too many))
@hikari So like I know what all the constants are, the concern I would generally have is β€œdo I need to care about things like leap seconds”
2 replies β†’
2 replies

Kanbaru 🌟 (one hikari of too many)

(replying to Saagar Jha)

@saagar ah, right, my particular kind of curious mind meant I already knew the answer to that (no) but most people wouldn't

Kanbaru 🌟 (one hikari of too many)

(replying to Kanbaru 🌟 (one hikari of too many))

@saagar I think, as an interview question, I wouldn't want to penalise someone for not knowing, it's just interesting to see how they go about trying to solve it considering what they do know


I Can't Believe It's Not Zero!

(replying to Saagar Jha)

@saagar @hikari there’s a fixed small set of all leap seconds ever, so that’s just a table lookup.

Kanbaru 🌟 (one hikari of too many)

(replying to Kanbaru 🌟 (one hikari of too many))

it's surely not the most efficient way to do it, but it should be decently efficient at least, and I think it's pretty easy to understand! also I get great satisfaction over the use of const stuff here (like C++ constexpr)