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

8 Upvotes

14 comments sorted by

12

u/Technical_Ideal_5439 6h 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.

-2

u/mostlytalentedlenny 6h ago

my brain can only deal with so many off-by-one errors at once, so i'm sticking with plain date strings for now... utc drift just adds another layer of confusion

5

u/TheSilentFreeway 4h 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.

10

u/CrispyCosmonaut 6h ago

Classic. The 3 biggest problems in code:

  1. Timezone exchanges
  2. Off by one errors.

1

u/mostlytalentedlenny 6h 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

6

u/mixony 6h ago

Wasnt it 2 hardest problems in programming

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

2

u/Prime624 6h ago

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

2

u/mostlytalentedlenny 6h 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/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/Mediocre-Pumpkin6522 4h 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/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.