问题
I am using Core-Data
to 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?
回答1:
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, NSDate
s 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
...
回答2:
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:
The date which is saved in
database
(originally in any timezone) will be saved in'GMT/UTC'
timezone.The date which is seen in
local timezone
in database directly is because ofthird party tool
which is used to see the data in database.When date is taken out of
database
, then also it is in'GMT/UTC'
timezone.To see the date in
specific timezone
, theNSDateFormatter
is used with the specific timezone specified.The most important is
NSDate
object is always irrespective of anytimezone
and is always in'GMT/UTC'
来源:https://stackoverflow.com/questions/31697771/save-date-in-gmt-timezone-in-sqlite-database