Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 向NSDate对象添加周组件时,为其设置所需时间失败_Iphone_Ios_Datetime - Fatal编程技术网

Iphone 向NSDate对象添加周组件时,为其设置所需时间失败

Iphone 向NSDate对象添加周组件时,为其设置所需时间失败,iphone,ios,datetime,Iphone,Ios,Datetime,我试图通过循环以下代码,将通知安排在每两周中午12:00的时间 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; // Allocate new temporary calendar object with default timezone NSCalendar *tmpCalendar =

我试图通过循环以下代码,将通知安排在每两周中午12:00的时间

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

// Allocate new temporary calendar object with default timezone
NSCalendar *tmpCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[tmpCalendar setTimeZone:[NSTimeZone defaultTimeZone]];

// Allocate new temporary date components from the date to fire
NSDateComponents *tmpDateComps = [tmpCalendar components:unitFlags fromDate:dateToFire];

// Set time to '12.00'
[tmpDateComps setHour:12];
[tmpDateComps setMinute:0];

// Get new date to fire object based on date components
dateToFire = [tmpCalendar dateFromComponents:tmpDateComps];

NSLog(@"dateToFire %@", dateToFire);
// On 6th iteration = xx-xx-xxxx 09:00 +00:00
// On 11th iteration = xx-xx-xxxx 10:00 +00:00

// Allocate new temporary offset date components
NSDateComponents *tmpOffsetComps = [[NSDateComponents alloc] init];    

// Add a number of weeks
[tmpOffsetComps setWeek:2];

// Get new date to fire object based on offset date components
dateToFire = [tmpCalendar dateByAddingComponents:tmpOffsetComps toDate:dateToFire options:0];

NSLog(@"dateToFire %@", dateToFire);
//On 6th iteration = xx-xx-xxxx 10:00 +00:00
//On 11th iteration = xx-xx-xxxx 09:00 +00:00
不知何故,当我在循环中记录dateToFire对象时,在第六次迭代中,我首先得到正确的xx xx xxxx 09:00+00:00,但在添加偏移组件后的第二次日志中,我得到xx xx xxxx 10:00+00:00。奇怪的是,它在第11次迭代中回到了正确的时间

我做错了什么?如何纠正这种奇怪的行为

更新
正如亚当所说,这是由于夏令时而发生的,因此这是一种正常行为。

由于日期跨越夏令时/标准时间或您所在地区的某些等效时间之间的边界,额外的一小时正在出现/消失。

您的单位标志是什么样子的?您的单位标志中是否有与夏令时/标准时间等效的内容地方我猜这就是增加/减少额外时间的原因。当时间改变时,它实际打印的日期是什么?你真是个天才!没想到。10月31日之后,时间确实会发生变化,3月31日之后又会回来。泰@UIAdam你同意我删除这个问题吗?@UIAdam&micadelli:这个问题/答案可能会帮助其他人,我们经常忘记夏令时:最好是UIAdam复制他的评论作为答案。