在sqlite数据库中以“GMT”时区保存日期 [英] Save date in 'GMT' timezone in sqlite database

查看:579
本文介绍了在sqlite数据库中以“GMT”时区保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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的回答解决我面临的问题<



发现:


  1. 保存在数据库(原始在任何时区)的日期将保存在'GMT / UTC '时区


  2. 当地时区中显示的日期数据库直接是因为第三方工具,用于查看数据库中的数据。


  3. 日期从数据库中取出,然后也在'GMT / UTC'时区。

    / li>
  4. 要查看特定时区中的日期, NSDateFormatter 与指定的特定时区一起使用。


  5. 最重要的是 NSDate 对象总是不管任何 timezone 并始终在'GMT / UTC'



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?

解决方案

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'

这篇关于在sqlite数据库中以“GMT”时区保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆