News for hackers. Stuff that matters.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Sunday November 19, 2023 @11:31AM (#38334209)
I like your premise but disagree with your guidance.

You should not convert the integer early, you should actually wait until the very last moment.

Nowadays a timestamp will be marshalled and demashalled by (at the very least) two runtimes until it reaches human eyeballs opening up a wide spectrum of bug potential, difficult to understand code and development slowdowns. Keeping it simple and unambiguous is very important. Time libraries have a habit of coming and going, ints are always there and 99% of the date arithmetic is trivial any way.

Don't use a lib, just pretty print numbers and you'll be fine.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by fluoridation on Sunday November 19, 2023 @12:29PM (#38335032)
An int is not unambiguous, though. "2023-11-19T14:22:34-03:00" is clearly a timestamp and represents an unambiguous point in time, while a number by itself doesn't mean anything. You have to know that it's a timestamp (probably from context) and you have to know what timezone it's in.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by Izkata on Sunday November 19, 2023 @12:49PM (#38335398)
That datetime is unambiguously represented by 1700414554 as a unix timestamp, and you'd convert to the user's local time zone on display.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by rusk on Sunday November 19, 2023 @01:42PM (#38336132)
Not in the future it’s not. There’s no way to know the exact epoch time until it has happened. Timestamps “float” and can accommodate local fluctuations or even changes.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @04:18AM (#38345259)
I have never heard of epoch time "floating" any links?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by rusk on Monday November 20, 2023 @05:29AM (#38345965)
Haha. I’m sorry if the terminology is catching you out. It’s not epoch time that floats. Epoch time is an inalienable absolute (at least in theory). However actual cosmological and time as perceived by humans does drift wrt to epoch time. Which of these is right is a philosophical discussion but at the present instant they do indeed converge and so epoch time between now and some time in the early 1960s is “the correctest” from a systems perspective.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by pletnes on Sunday November 19, 2023 @01:41PM (#38336128)
It’s a nightmare for the backend developers, compared to iso-strings, in my experience.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by jagged-chisel on Sunday November 19, 2023 @01:45PM (#38336188)
But 1700414554 is not unambiguously a date. Could be a count. Or an identifier. It needs metadata to tell you how to interpret it. For this to represent a datetime, the human needs to know that it’s a datetime, and what epoch and timezone to use. The ISO date string removes the ambiguity because the context for interpreting it is within the string.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by yen223 on Sunday November 19, 2023 @04:35PM (#38338378)
Crucially, that number could represent the number of seconds, or the number of milliseconds since Unix epoch (e.g. Date.getTime() in Java)

You kinda have to guess from context that if it's a 10-digit number, it's probably seconds.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @12:21AM (#38342659)
Fair point, types should be documented, but them again that's hardly a requirement nowadays, right?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by bornfreddy on Sunday November 19, 2023 @04:44PM (#38338505)
I never understood this argument. The ISO8601 date string is as much a convention as a unix timestamp is - without context neither are decipherable. If anything, unix timestamp is easier to explain to an alien than a date string. It has a starting point (1970/01/01 00:00 UTC) and it counts seconds from then. Care to explain how an ISO8601 date string is constructed?

Also calculating amount of time between two ISO8601 strings without libraries is nor trivial, or any other operation actually. When dealing with dates, there is only one simple way to work with non-localized times, and it's not ISO-something.

EDIT:

> ...and what epoch and timezone to use.

This is also not correct. Unix timestamp has a well defined epoch and it doesn't deal with timezones (though the epoch itself is, of course, usually defined as 1970/01/01 00:00:00 UTC). You are free to define other timestamps, but unix timestamp is well defined.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @04:58PM (#38338699)
Seconds since epoch requires careful and precise handling of datatypes and assumptions. This works great inside a system where that can be managed but for the purpose of exchange between systems the ISO formats contain all relevant information. Either side can use a library to parse the string into their local representation.

Comprehension by extraterrestrial life is a localization problem.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by nerdponx on Sunday November 19, 2023 @07:19PM (#38340286)
I'm not sure I understand. How is this different from any other quantity with an associated unit? What's the possible failure mode here, interpreting the "created_time_us" field as a distance in km instead of a timedelta in microseconds?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @09:32PM (#38341483)
Is the unit seconds or microseconds? How are leap seconds handled? What base type is used? Integer, number, or float? How many bits? Whats the precision? What’s the CPU architecture? Is it signed? How should overflows be handled?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @12:18AM (#38342628)
> How are leap seconds handled?

Unix time has uniform handling over leap seconds, regardless of the precision.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Monday November 20, 2023 @12:47AM (#38342952)
Yes that is true of Unix time. But is it true of every implementation? Are you sure? It might look like seconds since Uinx epoch but is it?
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @04:22AM (#38345317)
Most stdlibs provide ways to work with Unix time, I don't think there isuch room for significant issues here.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by defrost on Monday November 20, 2023 @04:42AM (#38345513)
Aside from the obvious issue with engineering, physics, etc.

A unix second is allowed to be different from a "real" elapsed second.

The unix day has 86400 "unix seconds" which are actually 86401 real seconds (for days with added leap seconds).

Any application logging real world events against unix time will screw up velocities, force, energy, etc when computing "instrument reading" per "unix second".

It might not matter to most people but it's an issue for surveyors, geophysicists, astrophysicists, engineers, etc.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @04:36PM (#38355194)
> Any application logging real world events against unix time will screw up velocities, force, energy, etc when computing "instrument reading" per "unix second".

That is a fair point for situations that depend on short term observations. But, UTC has the same issues there as Unix epoch. I think it is a valid edge case but if you are doing, say, GPS based speed calc I would wager you are already pulling your time reference from a low level source that you can depend on will be running steadily for the X minutes/hours you need it.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by defrost on Monday November 20, 2023 @05:01PM (#38355533)
Sure, this is what responsible STEM people do for any time lapsed measurements, be those position, nine axis magnetic field recordings, gravity, radiometrics, microwave return, etc - they use an independant epoch based clock source that counts true lapsed time rather than conventional human time.

Point being, it's an area of UnixTime that many overlook - to date, since the 1980s I've had long term multichannel data aquisition running throughout every leap second adjustment which would have had data glitches had the time channel been UnixTime, UTC, etc.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by tsimionescu on Sunday November 19, 2023 @06:11PM (#38339592)
This is the biggest general problem: you can't accurately compare the duration between two date times without having proper context of how the datetimes were stored or generated, especially for future dates. For example, you can't say today what the Unix timestamp will be for 1 January 2030, 10:00:00 AM in Bucharest, Romania. Romania may very possibly change timezone by then. So, if a user sets a meeting for that time, converting to the Unix timestamp according to the current timezone info is wrong. You'll end up alerting them on 1 January 2030 at 09:00:00 AM, or perhaps 08:59:58 AM if some leap seconds get added. Or it may even be entirely different if there is some calendar change for whatever reason by then.

Note that UTC has a similar problem - which is why you actually need a local time string for this type of events.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @12:13AM (#38342581)
Correct, neither iso strings mor numbers can get you out of this dilema.

While ISO uses UTC offset and time zone interchangeably it actually is only defined over offsets, not locations.

I've had this problem discussed before. Typically the situation is firing events based on times at specific locations. It is neither straight forward with date libraries nor ist it easy with numeric computation. Sometimes it makes sense to render it down to a specific point in time at storage and not expect the country change timezone overnight. Some times the location itself may be changing randomly (moving objects) but in that case it typically just meant recalculating offsets on movement events.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by kortex on Sunday November 19, 2023 @10:16PM (#38341833)
There's a lot more context baked into an ISO string than a unix timestamp though. Write a regex to find all the strict ISO8601-RFC3339 strings in a directory. Now write one for unix timestamps.

> Also calculating amount of time between two ISO8601 strings without libraries is nor trivial, or any other operation actually.

That's true of any datetime that actual humans use (as opposed to computers, financial systems, sysadmins and programmers).

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @12:02AM (#38342485)
> Write a regex to find all the strict ISO8601-RFC3339 strings in a directory

That is a weird edge case. I've had this before and the person proposing it hinged on some forensic practice which was based on analyzing human readable data. As I said in my original post, pretty printing ints to ISO timestamps is actually my original opinion.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by mulmen on Sunday November 19, 2023 @04:53PM (#38338626)
What library did you use? What is your epoch? Are those seconds or milliseconds? Is it UTC or TAI?

ISO8601 and friends are literally for formatting dates for the purpose of exchange.

Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by theK on Monday November 20, 2023 @04:17AM (#38345248)
The good thing about ints is that typically the stdlibs themselves deliver quite good tooling to work with Unix timestamps so in most cases you don't really use a library for the timestamps themselves.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by fluoridation on Monday November 20, 2023 @12:27PM (#38351049)
You're assuming everyone is using the same standard library as you. Not every library uses even the same indivisible unit to tell time.
Re:It's Time for a Change: Datetime.utcnow() Is Now Deprecated by Dylan16807 on Sunday November 19, 2023 @06:37PM (#38339847)
> You have to know that it's a timestamp

So what? That's true of all formats. How is it relevant to the choice of which format to use at which stage of the process?

> and you have to know what timezone it's in.

You don't.

> clearly a timestamp

Not very helpful. It's easy to set up types in advance. Don't figure out if things look like dates at parse time, or you'll be causing the same problems that turn gene names into dates and choke on people named Null.

And people definitely will use strings that resemble dates but are not dates.