Skip to content
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

to_hash method timezone offset #110

Closed
chanpory opened this issue Nov 11, 2010 · 14 comments
Closed

to_hash method timezone offset #110

chanpory opened this issue Nov 11, 2010 · 14 comments

Comments

@chanpory
Copy link

I have a datetime string within an XML response:

2010-11-10T13:20:00

Calling Savon's to_hash method converts the string to a DateTime object:

Wed, 10 Nov 2010 13:20:00 +0000

This is great if the original datetime was in UTC. However, in my case, the original datetime is actually already in the local timezone which is Pacific Time (US & Canada). So after calling to_hash, the correct time should be:

Wed, 10 Nov 2010 13:20:00 +0800

The offset, would of course, be different if the original datetime was in daylight savings period.

How do I get the to_hash method to treat the original datetime as being in the local timezone, instead of UTC?

Much help would be appreciated, thanks!

@rubiii
Copy link
Contributor

rubiii commented Nov 23, 2010

savon respects xs:datetime strings with timezones like:
2010-11-10T13:20:00+02:00

if your service does not send a timezone, then that means it's utc.
one reason the service is sending utc could be to not force its users to a certain timezone. that's something you probably need to handle in your app.

protip: if you're using rails, take a look at: in_time_zone.

@chanpory
Copy link
Author

Thanks, I tried in_time_zone, but it converts the original time string to the local time zone, rather than adding the offset to the string. I ended up using nokogiri to parse the savon response, and using Time.zone.parse(xml_datetime_string_without_offset) to achieve what I needed to. So 2010-11-10T13:20:00 becomes Wed, 10 Nov 2010 13:20:00 +0800

@rubiii
Copy link
Contributor

rubiii commented Nov 23, 2010

just tried it again and to me it looks like it works perfectly fine:

"2010-11-08T11:22:33".to_datetime.in_time_zone
# => Mon, 08 Nov 2010 12:22:33 CET +01:00

my config is set to:

config.time_zone = "Berlin"

@chanpory
Copy link
Author

Sorry, don't think I was clear. The problem with in_time_zone is that it assumes the datetime string is UTC if it has no offset attached. I want it to assume that it's the local time instead. So for Berlin time zone, I want to convert the string 2010-11-08T11:22:33 to "2010-11-08T11:22:33+01:00. Notice that it's 11:22:33 in both strings. The way I achieved it is essentially this:

Time.zone.parse("2010-11-08T11:22:33")
=> Mon, 08 Nov 2010 11:22:33 PST -08:00

My config is set to:

config.time_zone="Pacific Time (US & Canada)"

Notice, that the time remains 11:22:33 in both cases.

Hope this clarified what I was after. Thanks!

@rubiii
Copy link
Contributor

rubiii commented Nov 27, 2010

did you see that it remains 12:22:33 in my example? really can't see a difference.

@chanpory
Copy link
Author

Somehow, we're not seeing the same thing. Looking at your example again, and I see you have 11:22:33 changing to 12:22:33 with in_time_zone method. The time string changes. I want 11:22:33 to remain 11:22:33 but with offset. I posted a screenshot of your example with the strings highlighted to show that they are different. http://oi52.tinypic.com/14v74zo.jpg

@rubiii
Copy link
Contributor

rubiii commented Dec 2, 2010

you're totally right. i'm sorry!

but talking about savon ... if a service returns a datetime without a timezone, it's utc. that's just a standard. and if the datetime is actually meant to have a timezone but doesn't, it seems to me that either the service should change to return the right value or your app needs to know and deal with this.

guess i need some more opinions on this.

@rubiii
Copy link
Contributor

rubiii commented Apr 5, 2011

no opinions. closing the issue. sorry.

@rubiii rubiii closed this as completed Apr 5, 2011
@cschiewek
Copy link

Assuming that a datetime without a timezone is UTC is incorrect.

According to ISO8601:

If no UTC relation information is given with a time representation, the time is assumed to be in local time.
If the time is in UTC, add a 'Z' directly after the time without a space. 'Z' is the zone designator for the zero UTC offset.

I am experiencing the same issue. The web service is not specifying timezone information, and savon is assuming that it's UTC, when it should be the local time zone.

Could you please re-open the issue.

Thanks!

@rubiii rubiii reopened this Jul 24, 2012
@cschiewek
Copy link

I did a little digging and it turns out the Time.parse() and DateTime.parse() behave differently. Time.parse() will properly set the offset to the local offset if none is specified, while DateTime.parse() will assume it's UTC if no offset is specified. Weird. Seems that rails uses Time.parse() over DateTime.parse(), but not sure if this is why.

@rubiii
Copy link
Contributor

rubiii commented Jul 24, 2012

interesting. unfortunately, i don't have time to look into this right now and i'd really appreciate if you could help me out.

@rubiii
Copy link
Contributor

rubiii commented Jul 24, 2012

also, this sounds like a change that could cause trouble for quite a few users. i'm all for changes to improve
the existing implementation, but we should find the best way to fix it without causing too many problems.

@cschiewek
Copy link

It seems like it's actually the Nori library doing the parsing. Is that accurate?

@cschiewek
Copy link

I answered my own question. It is in fact parsed in the Nori library. The typecasting is done in the xml_utility_node class. Close this issue, I'll open one on Nori.

Issue moved to rubiii/nori#22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants