-
Notifications
You must be signed in to change notification settings - Fork 71
Timezone information is lost with ZonedTimes #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm using a Anyway, it shouldn't matter what local time zone PGSQL is using to internally store the timestamp as long as the original timezone (and DST) is preserved correctly. The docs say that when using a timezone abbreviation or offset, the daylight savings state needs to be specified. I can't find where that's happening in the
|
Yes, Here, I'll show you what I mean: (Note that if the offset is not specified, it is computed from the timezone connection variable, based upon the timezone's offset at that time. In most cases, you should provide the offset; postgresql-simple automatically includes an offset when sending
As for However, the meaning of |
Wow thanks a lot. I did not realize PGSQL worked that way. That's unfortunate. I actually need to store the timezone myself then. Thanks for explaining this! |
Well, if you want some time trivia for future reference, here's a difference between choosing different cities in the same time zone:
1883-11-18 at noon is when Standard Railway Time went live, which is significant in the US at least. Very old timestamps from around the world exhibit similar behaviors based on local time standards; e.g. 1941-10-01 at midnight is when Kolkata seems to have adopted Indian Standard Time. Postgresql-simple does not (yet?) support retrieving these timestamps, though. That will be tricky, because Here's another example dealing with historical timestamps. In 1942 parts of Indiana, including Indianapolis, moved from central to eastern time, and in 2006 Indiana adopted daylight savings time:
|
Idea for people with time on their hands: define a |
That's essentially what I'm doing by hand now. Good suggestion. I'd be nice if we could get the PG devs to make |
What do you mean "reference the timezone by ID"? The pg_timezone_abbrev view is just a convenience wrapper over the internal pg_timezone_abbrev() function. |
Since the tz information isn't stored in a table with a pkey id, it can't be referenced by a forgein relation. Instead a string of the tz name, offset, and dst status need to be stored everytime the original tz is necessary. |
If it were a table, the primary key would be the abbrev column, so depending on your use case you may be ok to just store that. I agree it is annoying to not be able to use a foreign key, but storing the row doesn't really solve that. pg_timezone_abbrevs is implemented as a function because you can configure it at runtime to restrict which timezones are available, if it were a table you wouldn't be able to remove timezones any more. It sounds like what you want is a snapshot of a particular state of the pg_timezone_abbrevs function stored as a table. You can already do that:
|
Timezone abbreviations are not enough to uniquely identify timezones so using it as a primary key wouldn't make sense: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations CST has heavy overlap for example. Taking a snapshot of the function in the manor you suggestion also wouldn't make sense because the table wouldn't reflect timezone changes over time. If you want to discuss this further, I suggest email or Luke on IRC. |
Postgresql loads timezone data from text files. It refuses to load it if there are duplicate definitions for an abbreviation. So the abbreviation is unique in the function/view in question. If it was not unique, then the primary key would be all 3 columns, so you'd be storing them all anyways. Taking a snapshot is precisely what you would have to do to be able to have a foreign key, that's what I mean. The output of that function can change, entirely external to the database. There is no way to safely represent that as a table which you could have a foreign key to. What would happen to your references to CST when someone deletes CST from the text file? If you want to be able to reference it safely, then you have to reference a snapshot of the data as it was at a particular time. If you want to reference data that changes external to the database, then a foreign key isn't an option, you'd have to copy the offset anyways to preserve it in case the timezone is deleted. I don't think there is a way for the pg devs to make it work as a table without it being a snapshot that doesn't reflect timezone changes over time. |
I think this is an ok place to discuss it further, either here or the database-devel mailing list. This way the conversation is easier to find later. =) |
Regarding using timezone abbreviations as unique keys: It doesn't matter how PG handles timezone abbreviations. They aren't unique. If I try to reference a timezone with CST, it could mean multiple timezones. Using a timezone abbreviation will never work as a unique key. Regarding the table to facilitate a foreign key reference: Imagine a function |
Oh, I recently-ish ran across As for reading the documentation, I strongly recommend the github repo. Arguably, there would also be use cases for |
I ended up using this:
Which you're right, @lpsmith that does look like what I was looking for. |
Well, your type does not correspond to the The difference is that There are a couple of tradeoffs between storing a timezone versus storing an offset. I can definitely see use cases for both variants, though. |
The timezones CDT and EST are both -5 offsets from UTC. When serializing ZonedTimes timezones as an offset, the original timezone is lost. It looks like this is what's being done in postgresql-simple:
timeZoneToBuilder :: TimeZone -> Builder
It looks like deserialization has the same problem.
This has some practical problems. For example:
I'm currently in CDT (daylight savings) so when I store a ZonedTime of the current time and then pull it back out of postgresql-simple, it's been shifted by an hour.
I'm also making the assumption that PGSQL actually stores the full timezone info (it looks like it does in the docs):
http://www.postgresql.org/docs/9.2/static/datatype-datetime.html
The text was updated successfully, but these errors were encountered: