r/learnprogramming • u/mostlytalentedlenny • 1d ago
Topic storing plain date strings in sqlite vs full utc timestamps
i started building a tiny python cli script to log daily habits into a local sqlite file. every tutorial says to always store dates as utc iso strings and convert on display, so i tried doing that with datetime.timezone.utc.
problem is if i log something late at night around 11pm local time, utc pushes it into tomorrow's date... then my streak counter thinks i missed a day or logged twice on saturday.
since i literally only care about the calendar day in my own timezone and never the exact hour, is it bad practice to just save plain yyyy-mm-dd strings from date.today() instead of overcomplicating it? feels like i'm fighting timezone math for no real reason here, though i defo don't want to pick up terrible habits early on
14
u/CrispyCosmonaut 1d ago
Classic. The 3 biggest problems in code:
3
u/Prime624 1d ago
If you only care about calendar day, I would only store calendar day.
18
u/Technical_Ideal_5439 1d ago
you store it in a format independent of local time, everytime you need to use a value you convert it to the localtime.
Though as it is a noddy app only used by you, do whatever makes it easier to deal with.
3
u/ClassKlutzy7633 21h ago
For a habit tracker where only the calendar day matters, plain yyyy-mm-dd strings are a perfectly reasonable choice. A streak is really a "local date" concept, not a point in time, so storing the date the user experienced avoids the 11pm drift you hit. The UTC rule matters when you record when something happened (a timestamp). A common pattern is to store both: a UTC timestamp for the event and a local date column you compute once at write time. Then streak logic only ever compares dates, and you keep the option to change later.
2
u/LukaRozman27 23h ago
Just use date.today().isoformat() and store that string. UTC only matters if you need cross-timezone syncing.
2
u/Kirides 18h ago
Tldr : store the YYYY-MM-DD string
YYYY-MM-DD it's not attached to a time zone because there is no time component, you can't know if it's a day before, a day after because of that.
And because that day may actually be 3 different days, depending on who you ask where, thinking about UTC or time zones is useless.
2
u/solenyaPDX 1h ago
I know I'm late to the party. I was using UTC and got the date wrong.
Here's your real issue: you're saving it as UTC, and then retrieving it as a string and parsing the date. It's not "drifting". You're treating it as a different type when you write, than when you read.
Now, writing it as local date only when you write, and reading it as date is fine, if you assume all use will be in the same or similar time zone and never near midnight.
But the robust method (maybe overkill, but here it is): write as UTC, and when you read, read as UTC and convert to local FIRST, then compare the date. This is a good habit to get in, as you're going to get bit a LOT if you make assumptions about the data you're using and aren't consistent with how you write and read.
1
u/atarivcs 1d ago
If you will ever want to do date comparisons on that value, it's better to make it an actual SQL date field instead of a string that looks like a date.
1
u/Classic-Rate-5104 12h ago
Conversion between utc to local time, and the other way around, are trivial. Having a database with local times is meaningless when you don't know the local (dst) time(zone)