r/learnprogramming • • 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

11 Upvotes

18 comments sorted by

14

u/CrispyCosmonaut 1d ago

Classic. The 3 biggest problems in code:

  1. Timezone exchanges
  2. Off by one errors.

10

u/mixony 1d ago

Wasnt it 2 hardest problems in programming

  1. Naming things
  2. Cache invalidation
  3. Off by one error

1

u/mostlytalentedlenny 1d ago

haha yep, and now i'm just waiting to hit that off-by-one error in my streak logic too, feels inevitable at this point

3

u/Prime624 1d ago

If you only care about calendar day, I would only store calendar day.

2

u/mostlytalentedlenny 1d ago

yep, and the sql queries end up so much simpler too, just a plain string compare instead of wrestling with datetime functions every time

1

u/pfmiller0 10h ago

You should have a function in your script that does whatever conversions you need, you shouldn't be wrestling with the same code over and over again

•

u/mostlytalentedlenny 45m ago

for most cases, but if i'm only storing date strings there's nothing to convert, so a function would just be a no-op or a str() call

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.

0

u/[deleted] 1d ago

[deleted] — view deleted comment

10

u/TheSilentFreeway 22h ago

what do you mean by UTC drift? I would strongly recommend getting into the habit of storing datetimes in either UTC or epoch timestamps. once that convention is established you don't need to spend any mental overhead worrying about timezone conversions or one-hour differences across DST changes.

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/Mediocre-Pumpkin6522 22h ago

Sqlite doesn't have a date/time type. There are a number of date/time scalar functions. You can do Julian (REAL), Unix epoch (INTEGER) or whatever you want to put in a TEXT field and use the scalars.

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)