Skip to content
Advertisement

Storing struct_time in SQL

Some code I am writing in Python takes in a date from a server in the struct_time format (with the 9 args).

How can I store this date in an SQL database, and be able to read back this date as a struct_time while keeping the timezone and all additional information coming from struct_time?

I tried putting the struct_time directly in the SQL

I can put the time in the database using the below, but I don’t see where the timezone is kept when converting to strftime.

Advertisement

Answer

I’d highly suggest doing one of these (in this specific order):

  1. Use built-in DATETIME data type and store all dates in UTC
  2. Use LONG/BIGINT type to store date in epoch
  3. Use built-in DATETIME format that can store time zone information

Don’t store dates as strings, don’t couple it with struct_time or any other struct/class, you’ll regret it later 🙂

Your application should have a data layer, which would handle data serialization/deserialization.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement