0

I am using Core-Datato store date-time object.

Eg. 2015-07-28T07:16:52+0000 this is date in ISO format in GMT timezone.

But when I save this date in database

 NSString* dateString=@"2015-07-28T07:16:52+0000";
 NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter dateFromString:dateString];

the resulting date which I see in database is 2015-07-28 12:46:52

which is in IST according to my device's timezone

I tried to set timezone as well in dateFormatter but again the same response

 NSString* dateString=@"2015-07-28T07:16:52+0000";
 NSDateFormatter* dateTimeformatter=[[NSDateFormatter alloc] init];
[dateTimeformatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateTimeformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateTimeformatter dateFromString:dateString];

Even when I save the [NSDate date] directly in database , it saves the converted date according to device's timezone.

Why Core-data is not taking the timezone of NSDateFormatter into account? Can anyone tell me how can I save the date in GMT in database irrespective of my device's timezone?

SandeepAggarwal
  • 1,273
  • 3
  • 14
  • 38

2 Answers2

2

I think the behavior you're seeing is the 3rd party software converting it to your timezone for display. Timezones don't really matter except for display purposes (or if you need to convert between them for calculations, maybe). And, NSDates on their own don't really correspond to any particular timezone, though internally they're represented as though they were GMT. The doc says:

The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT.

If you look at how the value is actually stored (which you can do with the sqlite3 command-line tool), you can see Core Data is storing it as the number of seconds since 1/1/01; there's no timezone involved. I have an app which does a ton of date manipulation and stores dates in a Core Data store. It looks something like:

zach$ sqlite3 TaskLog.sqlite
sqlite> select ZSTARTTIME from ZTASK;
459924925.598104
459925327.3355
459925356.467429
...
zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • Thanks for your reply, I want to know that if I save date '2015-07-28T07:16:52+0000' then since, it is in 'GMT' , will remain saved as 'GMT' and '2015-07-28T07:16:52+0530' is in 'IST' will also first converted back to 'GMT' and save as 'GMT' ? – SandeepAggarwal Jul 30 '15 at 08:57
  • Yes, that's basically correct. Regardless of which timezone you see when you output an `NSDate`, internally it's all GMT. – zpasternack Aug 04 '15 at 00:05
0

I managed to solve problem I was facing by doing some research and using zpasternack's answer .I mentioned some findings for anyone who may face such problems related to timezone.

Findings:

  1. The date which is saved in database (originally in any timezone) will be saved in 'GMT/UTC' timezone.

  2. The date which is seen in local timezone in database directly is because of third party tool which is used to see the data in database.

  3. When date is taken out of database , then also it is in 'GMT/UTC' timezone.

  4. To see the date in specific timezone , the NSDateFormatter is used with the specific timezone specified.

  5. The most important is NSDate object is always irrespective of any timezone and is always in 'GMT/UTC'

Community
  • 1
  • 1
SandeepAggarwal
  • 1,273
  • 3
  • 14
  • 38