r/learnprogramming • u/mostlytalentedlenny • 7h 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
12
u/Technical_Ideal_5439 7h 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.
10
u/CrispyCosmonaut 7h ago
Classic. The 3 biggest problems in code:
2
u/Prime624 7h ago
If you only care about calendar day, I would only store calendar day.
1
u/atarivcs 6h 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/LukaRozman27 5h ago
Just use date.today().isoformat() and store that string. UTC only matters if you need cross-timezone syncing.
1
u/ClassKlutzy7633 3h 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.
•
u/Kirides 13m 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.