Comment by ๐Ÿš€ stack

=> Re: "Converting Unix timestamp to date/time" | In: s/programming

Interesting. It seems like my way is a lot easier. Since the only weird year between 1970 and 2037 is 2000, and it happens to be a leap year, we can ignore the 100/400-year exception - it is just like the rest of the leap years. I already have days since 1970, so I can continue the calculation in days.

As noted previously, I adjust to March 1972 as the base year, to shift the leap day to the last day of a 4-year bundle. I can then divide by the number of days in such bundles, yielding number of days within the final quad as remainder.

The rest of the calculation is division by 365 to find the year within a quad (which also detects if we won the leap year lottery),

Then, to figure out the months, I go into a loop subtracting days in each month. That is the only iteration required.

=> ๐Ÿš€ stack [OP]

2023-09-11 ยท 1 year ago

4 Later Comments โ†“

=> ๐Ÿš€ mbays ยท 2023-09-11 at 05:30:

So you plan to be part of the Y2038 problem?

=> ๐Ÿ‰ gyaradong ยท 2023-09-11 at 06:43:

some notes:

I'm never touching a time library.

=> ๐Ÿฆ€ jeang3nie ยท 2023-09-11 at 15:03:

Crap, don't get me started on daylight savings time.

=> ๐Ÿš€ stack [OP] ยท 2023-09-11 at 17:05:

I am passing the buck to the user, of course. A variable will keep the timezone adjustment offset, and whoever cares about this will set it to the right amount.

At this point, it is very likely that I will be the only user anyway.

X

Original Post

=> ๐ŸŒ’ s/programming

Converting Unix timestamp to date/time โ€” Without any outside libraries. All we have is a Unix timestamp, seconds since Jan. 1, 1970. I'm looking for a minimalistic solution for my tiny nForth, but really curious if anyone has tricks up their sleeve for this kind of a task. I'm willing to ignore leap seconds for now. So far I got the time part: add timezone in seconds, divide by 86400 to get days, and use the remainder for time in seconds. The rest is trivial, dividing by 60 for minutes and 60...

=> ๐Ÿ’ฌ stack ยท 10 comments ยท 1 like ยท 2023-09-10 ยท 1 year ago

Proxy Information
Original URL
gemini://bbs.geminispace.org/u/stack/5197
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
35.352917 milliseconds
Gemini-to-HTML Time
0.586697 milliseconds

This content has been proxied by September (3851b).