What is an appropriate data type to store a timezone?

后端 未结 5 1898
情深已故
情深已故 2021-02-03 22:55

I\'m thinking of simply using a string in the format \"+hh:mm\" (or \"-hh:mm\"). Is this both necessary and sufficient?

Note: I don\'t need to store the date or the time

5条回答
  •  情话喂你
    2021-02-03 23:15

    "+hh:mm" and "-hh:mm" are not time zones, they are UTC offsets. A good format to save those are as a signed integer with the offset in minutes. You can also use things like interval but that will only help you if you want to do date calculations directly in PostgreSQL, like in a query, etc. Usually though you do these calculations in another language, and then it depends on that language if it supports the interval type well and has a good date/time library or not. But converting an integer into some sort of interval-like type, like Pythons timedelta should be trivial, so I would personally just store it as an integer.

    Time zones have names, and although there are no standardized names for the time zones there is one de facto standard in the "tz" or "zoneinfo" database, and that's names like "Europe/Paris", "Americas/New_York" or "US/Pacific". Those should be stored as strings.

    Windows uses completely different names, such as "Romance time" (don't ask). You can store them as well as strings, but I would avoid it, these names aren't used outside Windows, and the names make no sense. Besides, translated versions of windows tend to use translated names for these timezones, making it even worse.

    Abbreviations like "PDT" and "EST" are not usable as time zone names, because they are not unique. There is four (I think, or was it five?) different time zones all called "CST", so that's not usable.

    In short: For time zones, store the name as a string. For UTC offsets, store the offset in minutes as a signed integer.

提交回复
热议问题