Is there any convention to add in a local timezone (whatever the user has set it as) in a database?
For example, like I can do the following for UTC time:
2014-01-01 01:02:03Z
Is there something like the following to mean local time?
2014-01-01 01:02:03L
Or some other suffix where it can either pick up the user’s system time or take it from a variable that can be set? For example, something like (for Postgres):
ALTER DATABASE postgres SET timezone TO 'Europe/Berlin';
4
Answers
Just treat the undecorated time as a timestamp with time zone:
postgres would use the system timezone, or what you called local, if no timezone is specified. see manual
Your concept is flawed for a couple of reasons:
A user in Germany connects to a Web server in England that connects to a database server is America. What constitutes local?
More to the point Postgres does not store the time zone in
with time zone
fields. So you will not recover the entered time zone on data retrieval.If you are dealing with multiple time zones then the field you need to use is
timestamp with time zone
. This will rotate entered timestamp values to UTC for storage. You now have a fixed point in time that you can rotate to whatever ‘local’ time you want on retrieval.This has nothing to do with Postgres.
The format you’re asking about is ISO 8601. Specifically in that format, the absence of a
Z
or an offset such as-07:00
or+05:30
is defined as "local time".So what you are looking for is a string without an offset, such as
2014-01-01T01:02:03
.