3

I have a string like Sat, 27 Oct 2012 23:47:57 -0700 and I want to get corresponding timezone of -0700 like PST ,UTC etc..

4

1 回答 1

5

Fundamentally, you can't. You've currently just got an offset from UTC... and that could map to multiple time zones. You could create a fixed-offset time zone, of course - but that's not the same thing as "the time zone the user was originally in when this date/time value was generated" which is what I suspect you want.

You could see step through every time zone and see whether it's valid in each one - but you'll need to be careful due to some times which are ambiguous. For example, on Sunday in the UK, both "Sun, 28 Oct 2012 01:30:00 +0100" and "Sun, 28 Oct 2012 01:30:00 +0000" would have been valid. You probably want to work out the absolute UTC instant, and check what the offset for that instant would have been in the candidate time zone. If that offset is the same as the one you started with, that time zone is one of the ones which fits.

Quite what you'll want to do if you find multiple time zones which fit, I don't know - we don't really have enough context to give advice.

于 2012-11-01T07:01:41.527 回答