在sqlite数据库中以“GMT”时区保存日期 [英] Save date in 'GMT' timezone in sqlite database
问题描述
我使用 Core-Data
来存储日期时间对象。
2015-07-28T07:16:52 + 0000
这是ISO格式的 GMT
时区。
但是当我将这个日期保存在数据库中时
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];
我在数据库中看到的日期是 2015-07-28 12 :46:52
,位于 IST
p>
我尝试在dateFormatter中设置时区,但是同样的响应
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];即使当我保存 [NSDate date]
$ b >直接在数据库中,它根据设备的时区保存转换的日期。
为什么Core-data不考虑NSDateFormatter的时区?
任何人都可以告诉我如何在数据库 GMT
中保存日期,而不考虑我的设备的时区?
我设法通过做一些研究和使用 zpasternack的回答解决我面临的问题<
发现:
-
保存在
数据库
(原始在任何时区)的日期将保存在'GMT / UTC '时区
-
在
当地时区
中显示的日期数据库直接是因为第三方工具
,用于查看数据库中的数据。 -
日期从
/ li>数据库
中取出,然后也在'GMT / UTC'
时区。
-
要查看
特定时区
中的日期,NSDateFormatter
与指定的特定时区一起使用。 -
最重要的是
NSDate
对象总是不管任何timezone
并始终在'GMT / UTC'
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?
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'
这篇关于在sqlite数据库中以“GMT”时区保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!