💾 Archived View for bbs.geminispace.org › u › jeang3nie › 5195 captured on 2024-07-09 at 06:14:32. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-26)

➡️ Next capture (2024-08-18)

🚧 View Differences

-=-=-=-=-=-=-

Comment by 🦀 jeang3nie

Re: "Converting Unix timestamp to date/time"

In: s/programming

Also, about leap seconds. The big takeaway is that so long as you are working in a hosted environment (your code is running within an operating system and getting it's time from what the OS says is system time) you can ignore leap seconds, because the OS handles it for you. The usual method is that if a year is supposed to get a leap second, the final second of the year just gets repeated. So on Dec 31st of that year you'll get two seconds which have the same timestamp. It's not perfect but it's the convention, and it has the benefit that as a programmer you just don't have to care unless you're working on kernel code.

Of course, in true asshole fashion, both Google and Meta decided to get fancy and in their systems they smeared the extra second out over all of the seconds in the day the past couple times it came up. Because why care about interopability?

Sorry for the long windedness, I just find the subject fascinating.

🦀 jeang3nie

2023-09-10 · 10 months ago

5 Later Comments ↓

🚀 stack [OP] · 2023-09-11 at 01:48:

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.

🚀 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:

- a country went into daylight savings and never came out

- countries and states routinely change the dates when coming into or out of daylight savings.

- a country one skipped a week or rewound a week. can't remember which.

- a small island went from one side of the international date line to the other. I think they are now beyond 12 hours forward.

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 · 10 months ago