this post was submitted on 24 Dec 2023
97 points (96.2% liked)
Today I learned
7870 readers
1 users here now
founded 4 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I find the lack of dates the most annoying
Lol I just make sure they're formatted as ISO dates or timestamps and store them as text, you can do good enough date operations on them
This. Lexical sort works on dates stored as text in ISO format. For times, better standardize in storing in UTC and converting to and from local time. Although storage will suffer as this consumes way more data than storing as number.
Yeah fuck timezones. UTC in and out please
Sounds like you know enough to know you don't know that much, which means you're a better programmer than you think!
This comment made me chuckle at 6am. I was expecting some sort of profound reason to store them at text.
I've gotten used to storing dates as numbers and then just using some sort of library to convert them back to human readable text.
Dates are much easier to work with when they are numbers.
I don't know man, I'd far prefer storing a string and have whatever date library I'm using figure it out than have to deal with whatever the database thinks about dates and timestamps
you wouldn't be able to sort or compare in your queries though
why not? assuming you're saving them all in UTC they should be perfectly sortable and comparable (before, equal, after) as strings, even with varying amounts of precision when you compare substrings. You can't really do math with them of course, but that's what I meant about how DBs interpret dates and time: if you use it do to math and then you also use your application's date library to do math, you'll likely run into situations where the two come to different answers due to timezone settings, environments, DB drivers and the like. Of course if I could rely on the DB to do the math exactly the way I'd expect it to, then having that ability is awesome, however that requires more knowledge about databases and their environments than I currently have
obviously it depends on your requirements but what I meant was you can't compare them at the DB level within queries.. you have to pull the data out into your application layer to use your date library.
The problem there is you effectively need to grab more data than you need to answer queries like "which record has the most recent date" or "give me all the records between the values of the result of this subquery". it can quickly become a massive bottleneck and may even prevent you from doing certain types of queries at all due to memory limitations.
But, you totally can? When you store all your dates as an ISO 8601 string (UTC, so with Z at the end), you can simply compare the strings themselves with no further complications, if the strings match, the dates match, if one string is less than the other, the date therein is before the other. Their lexical order is equal to their chronological order
I agree that it's a massive and unnecessary overhead that you should definitely avoid if possible, but for anything where this overhead is negligible it's a very viable and safe way of storing date and time
edit: I forgot, there's also a format that's output by functions like toUTCstring that's totally different and doesn't have any logical order, but I honestly forgot about that format because nobody in their right mind would use it
Is it me or isn't ISO 8601 string more than enough?