Reputation: 490
For my project(a web and iPhone based application) i need to collect a user's timezone so as to show him/her the relevant data.Some of the possibilities that I can think of are :
1.Getting it using user's IP (But what if the user is behind a proxy server)
2.Sending it with request params(But that will require extra param to be attached with request. Or may be collecting once for a session)
3.Passing it through cookies(Has same implication as setting in the parameters)
4.Based on user's zip code which I am collecting at the time of registeration but for unregistered user's that can browse through the app will not be able to see the relevant data.
So all in all each approach that I can think of now has one or more drawbacks. I just want to know what other approaches are there and which one is the best to use in such a case where user experience is not affected and user is not explicitly asked about his/her timezone.Also the approach should apply to both web and iPhone.
Upvotes: 4
Views: 899
Reputation: 40277
For the iPhone SDK on getting timezone... This will get you what you need and adjust it for daylight savings.
NSTimeZone *systimeZone = [NSTimeZone systemTimeZone];
NSString *timeZoneString = [systimeZone localizedName:NSTimeZoneNameStyleShortStandard locale:currentLocale];
if([systimeZone isDaylightSavingTimeForDate:[NSDate date]]){
timeZoneString = [systimeZone localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:currentLocale];
}
There's great info on the TimeZone objects at the apple docs.
Upvotes: 2
Reputation: 169051
With JavaScript, you can use new Date().getTimezoneOffset()
, if that's of any help. According to MDC, that'll get you the offset in minutes... In my case (Finland), -180.
Upvotes: 1
Reputation: 3070
For the web, you should be able to get it from the user's browser. For the iPhone, I would imagine there's an API for that. At least, ISTR that my iPhone shows the correct local time when I change time zones (I imagine it gets this information from the local cell tower when it connects to it).
Upvotes: 0
Reputation: 14921
I don't know much about iPhone development, but there might be something in the API that allows you to access the user's preferences, or even the current time displayed to them.
As for the web in general, it's covered extensively in this question.
Upvotes: 0
Reputation: 449485
I would collect the user's current time using JavaScript, and send it along the first request to last for the whole session.
You'll have to allow for deviations (Maybe +- 15 minutes) but it should be possible to determine the time zone pretty reliably this way.
The next best thing is probably indeed running the user's IP through a geolocation service to find out the country. However, you'll have to maintain a country-to-timezone map to do this properly, and you will have problems with countries that have multiple time zones like the United States or Russia.
Upvotes: 0