2014-07-23 18 views
0

我真的非常难以理解真正简单的东西。我的应用程序的一个功能是每月重复事件,所以我需要一个日期,添加一个月,并存储它。我每个月都会重复一遍。CoreData&NSDate - 日期存储在2015年落后1天

我遇到的问题是我在测试时发现,当日期到2015年3月失去一天 - 所以3月22日变成3月21日... 4月... 5月等等。甚至陌生人事情一旦我从2016年3月份开始,它就会转回到第22位 - 不再错过一天。

据我所知,它不能是夏令时,因为它发生在12个月的时期,或闰年,因为2015年不是闰年。

我正在使用MagicalRecord来帮助CoreData,我敢肯定它可能与CoreData本身有关?这里的所谓的更新日期的简单代码:

// create date if we haven't already 
    if (!_dateEntity.theDate) { 
     NSDate *date = [NSDate date]; 
     _dateEntity.theDate = date; 
    } 

    NSLog(@"date before update - %@", _dateEntity.theDate); 

    // add a month to it 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [[NSDateComponents alloc] init]; 
    [components setMonth:1]; 
    _dateEntity.theDate = [calendar dateByAddingComponents:components toDate:_dateEntity.theDate options:0]; 

    NSLog(@"date after update - %@ \n ***** \n", _dateEntity.theDate); 

    // save the context 
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { 
     if (success) { 
      //NSLog(@"You successfully saved your context."); 
     } else if (error) { 
      //NSLog(@"Error saving context: %@", error.description); 
     } 
    }]; 

这是半须藤代码,从实际项目中剥离出来成为一个独立的项目只是为了测试(!以确保我不会疯)

这里(的要求)是原木的Xcode:

2014-07-23 10:48:55.001 dateTest[52136:60b] date before update - 2014-07-23 09:48:54 +0000 
2014-07-23 10:48:55.003 dateTest[52136:60b] date after update - 2014-08-23 09:48:54 +0000 
***** 
2014-07-23 10:48:59.994 dateTest[52136:60b] date before update - 2014-08-23 09:48:54 +0000 
2014-07-23 10:48:59.995 dateTest[52136:60b] date after update - 2014-09-23 09:48:54 +0000 
***** 
2014-07-23 10:49:04.685 dateTest[52136:60b] date before update - 2014-09-23 09:48:54 +0000 
2014-07-23 10:49:04.686 dateTest[52136:60b] date after update - 2014-10-23 09:48:54 +0000 
***** 
2014-07-23 10:49:08.127 dateTest[52136:60b] date before update - 2014-10-23 09:48:54 +0000 
2014-07-23 10:49:08.128 dateTest[52136:60b] date after update - 2014-11-23 10:48:54 +0000 
***** 
2014-07-23 10:49:12.594 dateTest[52136:60b] date before update - 2014-11-23 10:48:54 +0000 
2014-07-23 10:49:12.595 dateTest[52136:60b] date after update - 2014-12-23 10:48:54 +0000 
***** 
2014-07-23 10:49:16.014 dateTest[52136:60b] date before update - 2014-12-23 10:48:54 +0000 
2014-07-23 10:49:16.015 dateTest[52136:60b] date after update - 2015-01-23 10:48:54 +0000 
***** 
2014-07-23 10:49:20.188 dateTest[52136:60b] date before update - 2015-01-23 10:48:54 +0000 
2014-07-23 10:49:20.189 dateTest[52136:60b] date after update - 2015-02-23 10:48:54 +0000 
***** 
2014-07-23 10:49:32.956 dateTest[52136:60b] date before update - 2015-02-23 10:48:54 +0000 
2014-07-23 10:49:32.957 dateTest[52136:60b] date after update - 2015-03-23 10:48:54 +0000 
***** 
2014-07-23 10:49:44.061 dateTest[52136:60b] date before update - 2015-03-23 10:48:54 +0000 
2014-07-23 10:49:44.061 dateTest[52136:60b] date after update - 2015-04-23 09:48:54 +0000 
***** 

从日志中,一切都看起来不错 - 还是在23日(今天)。但是,如果我实际查看数据库本身,则最后两个日期2015-03-23和2015-04-23分别显示为2015-03-22和2015-04-22。

在数据库中的最新更新,如图中的SQL浏览器: 2015年4月22日10时48分54秒

他们将这样的表现,直到2016年3月,在这一点上,他们会去回到正确存储。这是我真的不明白的一点。

任何人都曾经历过类似的事情?我现在正在撕掉我的头发

+0

可否请您在添加月份前后登录您的'_dateEntity.theDate',然后将结果作为更新添加到帖子中? – Lebyrt

+0

当然,只是为你做。谢谢你的帮助! –

+0

请问您可以发布2015-03-23和2015-04-23最后两个日期,因为它们出现在数据库中,但也包含时间和时区? – Lebyrt

回答

0

我相信这确实是夏令时问题,而不是核心数据问题。当间隔跨越标准时间到DST或从标准时间到DST时,您的时间间隔计算会给您带来意想不到的结果。

试试这个例子中,无核心数据:

NSDate *dateNow = [NSDate date]; 

    // add a month to it 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [[NSDateComponents alloc] init]; 
    [components setMonth:1]; 
    components.calendar = calendar; 
    NSDate *dateNowPlusOneMonth = [calendar dateByAddingComponents:components toDate:dateNow options:0]; 

    NSLog(@"now %@", dateNow); 
    NSLog(@"now plus one month %@", dateNowPlusOneMonth); 

    NSDate *dateMarch01 = [NSDate dateWithTimeIntervalSince1970:1425206934]; 
    // add a month to it 
    NSDate *dateMarch01PlusOneMonth = [calendar dateByAddingComponents:components toDate:dateMarch01 options:1]; 
    NSLog(@"Interval straddles ST/DST changeover date in USA"); 
    NSLog(@"March 01 %@", dateMarch01); 
    NSLog(@"March 01 plus one month %@", dateMarch01PlusOneMonth); 

    NSDate *dateMarch23 = [NSDate dateWithTimeIntervalSince1970:1427107734]; 
    // add a month to it 
    NSDate *march23PlusOneMonth = [calendar dateByAddingComponents:components toDate:dateMarch23 options:0]; 
    NSLog(@"Interval is after ST/DST changeover date in USA"); 
    NSLog(@"March 23 %@", dateMarch23); 
    NSLog(@"March 23 plus one month %@", march23PlusOneMonth); 

下面是输出(与NSLog的时间戳删除):

now 2014-07-23 18:03:22 +0000 
now plus one month 2014-08-23 18:03:22 +0000 

Interval straddles ST/DST changeover date in USA 
March 01 2015-03-01 10:48:54 +0000 
March 01 plus one month 2015-04-01 09:48:54 +0000 

Interval is after ST/DST changeover date in USA 
March 23 2015-03-23 10:48:54 +0000 
March 23 plus one month 2015-04-23 10:48:54 +0000 

要添加到这个问题的卑鄙的性质,你会在程序中看到不同的结果,具体取决于用户的时区(真正的DST更改日期)。澳大利亚,英国和美国都会有轻微不同的错误行为!

日期和时间编程指南的清单10说明了“在本周获取星期日”。我认为你需要适应这个过程,比如“在下个月获取当前日期和时间”。