保存到plist时的NSDate行为 [英] NSDate behaviour when saving to plist

查看:116
本文介绍了保存到plist时的NSDate行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从[NSdate date]创建一个日期并将其保存到plist,这是我创建日期的方式

I am creating a date from [NSdate date] and saving it to plist, Here is how I am creating a date

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   // [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];


    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit  | NSMinuteCalendarUnit)fromDate:[NSDate date]];
    NSInteger day    = [weekdayComponents day];
    NSInteger month  = [weekdayComponents month]; 
    NSInteger year   = [weekdayComponents year];

    NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
    [timeZoneComps setDay:day];
    [timeZoneComps setMonth:month];
    [timeZoneComps setYear:year];
    [timeZoneComps setHour:00];
    [timeZoneComps setMinute:00];
    [timeZoneComps setSecond:01];    


    NSDate *date = [gregorian dateFromComponents:timeZoneComps];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"file.plist"];

    NSMutableDictionary *d = [NSMutableDictionary new];
    [d setObject:date forKey:@"my-date"];

    [d writeToFile:filePath atomically:YES];
}

我已经测试了很少的情况

I have tested few cases

情况1:未将时区设置为上面的NSCalendar对象gregorian(默认情况下将采用本地时区)并将设备中的时区设置为印度,我将日期保存到了plist中,这就是我所得到的

NSLog日期显示为当前日期:2014-07-08 18:30:01 +0000

NSLog of date shows CurrentDate:2014-07-08 18:30:01 +0000

案例2:现在将设备时区设置为美国圣何塞

NSLog日期显示为 2014-07-09 07:00:01 +0000

NSLog of date shows 2014-07-09 07:00:01 +0000

案例3:将NSCalendar对象的时区设置为"UTC",并将上面的格里高利历时设置为印度,这就是我在plist中得到的

NSLog日期显示为 2014-07-09 00:00:01 +0000

NSLog of date shows 2014-07-09 00:00:01 +0000

案例4:设置美国San Jose的设备时间

NSLog日期显示为 2014-07-09 00:00:01 +0000

NSLog of date shows 2014-07-09 00:00:01 +0000

任何人都可以请我解释一下在所有这些情况下发生的事情.

Can anybody please explain me what is happening in all these cases.

问候 兰吉特.

推荐答案

这是因为您处理NSDate的方式.一旦将某些内容转换为NSDate,您将丢失所有时区信息,当您将其写入plist时,它将被写入您当前的时区,因为它将转换回字符串.因此,让我们一一讲解这些场景.

This is because of the way you are handling NSDate. Once you convert something into an NSDate then you lose all time zone information, and when you write it to the plist it will be written in your current time zone as it is converted back into a string. So let's go through the scenarios one by one.

所有方案都遵循此流程 NSDate-> NSDateComponents-> NSDate->字符串

All scenarios follow this flow NSDate -> NSDateComponents -> NSDate -> String

1) NSDate:2014-07-09 XX:XX:XX IST -> NSDateComponents 2014-07-09 00:00:01 IST -> NSDate 2014-07-09 00 :00:01 IST -> 2014年7月9日上午12:00:01

1) NSDate: 2014-07-09 XX:XX:XX IST -> NSDateComponents 2014-07-09 00:00:01 IST -> NSDate 2014-07-09 00:00:01 IST -> 09-Jul-2014 12:00:01 am

2) NSDate:2014-07-09 XX:XX:XX PST -> NSDateComponents 2014-07-09 00:00:01 PST -> NSDate 2014-07-09 12 :30:01 IST -> 2014年7月9日下午12:30:01

2) NSDate: 2014-07-09 XX:XX:XX PST -> NSDateComponents 2014-07-09 00:00:01 PST -> NSDate 2014-07-09 12:30:01 IST -> 09-Jul-2014 12:30:01 pm

3) NSDate:2014-07-09 XX:XX:XX UTC -> NSDateComponents:2014-07-09 00:00:01 UTC -> NSDate:2014-07- 09 05:30:01 IST -> 2014年7月9日上午5:30:01

3) NSDate: 2014-07-09 XX:XX:XX UTC -> NSDateComponents: 2014-07-09 00:00:01 UTC -> NSDate: 2014-07-09 05:30:01 IST -> 09-Jul-2014 5:30:01 am

4) 与3相同,因为您手动指定了时区

4) Same as 3 since you manually specified the time zone

请记住,NSDate 不在乎时区.这只是一个时间点. 2014-07-09 00:00:01 UTC 和2014-07-09 05:30:01 IST 是在同一时间的确切 NSDate的条件(格林尼治标准时间2001-01-01 00:00:00之前的426556801秒).它们在plist中不同的原因是,您正在(隐式)选择NSDate的本地时间表示.如果需要更多控制,请使用NSDateFormatter.

Just remember that NSDate does not care about time zones. It is just a point in time. 2014-07-09 00:00:01 UTC and 2014-07-09 05:30:01 IST are the exact same time in terms of NSDate (426556801 seconds ahead of 2001-01-01 00:00:00 GMT). The reason why they differ in the plist is because you are choosing (implicitly) the local time representation of your NSDate. If you need more control, use NSDateFormatter.

编辑考虑一下,您可能会认为结果应该有所不同,但是NSDate输出到plist的方式是专有的,因此由于某种原因,它似乎继续使用印度语标准时间.推测可能包括您没有在设置时区或其他时间之间杀死应用程序.

EDIT Come to think of it you could argue that the results should be different, but the way that NSDate outputs to a plist is proprietary so it looks like for some reason it keeps using Indian Standard Time. Speculation could include maybe that you didn't kill your app between setting the time zone or something.

第二编辑实际上,我对原始plist的内容很感兴趣.您正在使用编辑器进行查看,并且显示时它可能会将其转换为您的 Mac的本地时间.

SECOND EDIT Actually I'm interested in what the raw plist is holding. You are using an editor to view it, and it might be converting it to your Mac's local time when displaying it.

这篇关于保存到plist时的NSDate行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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