Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix iOS objective-c timezone issue?

I am in the process of developing SMS based app. When my app launches I select the last-message-date-time from database and send it to the server and I get back all the messages with date-time > last-message-date-time but because the app and server may not be in the same timezone I can't get the correct result. What is the best way to fix this issue? Please help. Thank you.

This question is the follow up of update SQLite with content of push notification when the app is not running.

like image 871
chamlingd Avatar asked Dec 20 '25 14:12

chamlingd


1 Answers

One of the options is to store the time stamp in UTC/GMT on server, and when you get the date from server, convert it to local date object using GMT as formate, then show the user to their local timezone using formatters. ie.

  1. Store the date in UTC/GMT on server.
  2. Get the date string in app, use NSDateFormatter with GMT/UTC as timezone to convert it to NSDate object in Objective-C
  3. Use NSDateFormatter with local timezone to show it to user.

Keep in mind that NSDate holds the date as independent of timezones so when showing it to user, use any NSDateFormatter to convert it to local or any timezone.

  • Here's apple's guide to managing timezones: Using Time Zones
  • A useful discussion here: A Fast, Accurate Way to Handle Dates from Servers
like image 176
Adil Soomro Avatar answered Dec 22 '25 04:12

Adil Soomro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!