I'm making a small Python habit tracker with SQLite. I only care which local calendar day a habit was logged, but using the UTC date can push a late-night entry into the next day. Is storing a plain YYYY-MM-DD date a bad idea?
Can I just store local dates in SQLite for a habit tracker?
Answers
No, a plain date fits what you're tracking. If the habit belongs to the day on your local calendar, date.today().isoformat() gives you a YYYY-MM-DD string you can save as TEXT.
A date isn't an instant, so it doesn't need a UTC conversion. Just make sure the machine's local timezone is the one you intend to use when creating the date.
Reply to this answer
UTC timestamps aren't wrong here, but you need to convert the timestamp to the relevant local timezone before taking its date. Taking the UTC date directly answers a different question.
Use a timestamp if you also need to know when the event happened. You could store both the UTC timestamp and the calendar date assigned to the entry, especially if you want that assigned date to stay fixed when timezone settings change.
Reply to this answer
SQLite doesn't have a dedicated date storage type, so TEXT is a normal choice. Fixed-width YYYY-MM-DD strings sort in calendar order, which makes range queries straightforward. Keep the format consistent and validate dates before saving them.
Reply to this answer
The tricky bit isn't really SQLite, it's deciding what a day means if you travel or add syncing later. A stored date preserves the day assigned to the entry, but it can't tell you the exact time or timezone where it happened. If those become requirements, you'll need more information.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Olivia:
That also means changing the machine's timezone can change which date a new entry gets. Decide whether that's acceptable for the tracker.