Thread with 12 posts
jump to expanded postinteresting 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
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)
@hikari does "I've researched how libraries do this before and I'm pretty sure I could reimplement that from memory" count
@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?
@hikari musl approach: simple, all fairly intelligible numbers; lots of modulo; some explicit branching for edges: https://github.com/bminor/musl/blob/f5f55d6589940fd2c2188d76686efe3a530e64e0/src/time/__secs_to_tm.c#L35
the libcxx approach: just do a bunch of divides by constants lol https://github.com/llvm/llvm-project/blob/51eeea67c67cb3e622730ee1fa8c9b939268429b/libcxx/include/__chrono/year_month_day.h#L75
@rcombs heh, thanks. both pretty elegant in their own ways!
@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 ah, right, my particular kind of curious mind meant I already knew the answer to that (no) but most people wouldn't
@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