Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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
Ios 在DST更改期间添加一个月时NSDate和小时丢失?_Ios_Objective C_Iphone_Nsdate - Fatal编程技术网

Ios 在DST更改期间添加一个月时NSDate和小时丢失?

Ios 在DST更改期间添加一个月时NSDate和小时丢失?,ios,objective-c,iphone,nsdate,Ios,Objective C,Iphone,Nsdate,当我试图在我能找到的所有stackoverflow问题上使用建议的“正确”方法添加一个月时,由于DST,我返回的日期总是丢失一个小时 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *component = [[NSDateComponents alloc] init]; [component setDa

当我试图在我能找到的所有stackoverflow问题上使用建议的“正确”方法添加一个月时,由于DST,我返回的日期总是丢失一个小时

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *component = [[NSDateComponents alloc] init];
[component setDay:29];
[component setMonth:3];
[component setYear:2015];
NSDate *date = [gregorian dateFromComponents:component];
NSLog(@"Date: %@", date);

NSDateComponents *monthComponent = [[NSDateComponents alloc] init];
monthComponent.month = 1;

NSDate *newDate = [gregorian dateByAddingComponents:monthComponent toDate:date options:0];

NSLog(@"newDate: %@", newDate);
此代码的输出为:

日期:2015-03-29 00:00:00+0000

新日期:2015-04-28 23:00:00+0000

如何在DST更改期间添加一个月,并将时间保持在午夜

谢谢你的帮助

响应下面gnasher729的示例:

在本例中,前两个日期设置为午夜,后两个日期设置为23:00。在将所有输出的日期保持在所需的午夜时间的同时,实现这样的功能的最佳实践是什么

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *initialDateComponent = [[NSDateComponents alloc] init];
[initialDateComponent setDay:29];
[initialDateComponent setMonth:1];
[initialDateComponent setYear:2015];
NSDate *date = [gregorian initialDateComponent];
NSLog(@"Date: %@", date);

NSDateComponents *monthComponent = [[NSDateComponents alloc] init];
monthComponent.month = 1;
monthComponent.hour = 0;
NSMutableArray *eventAlarmDate = [[NSMutableArray alloc] init];

int numberOfRepeatingMonths = 4;
for(int x = 0; 0 < numberOfRepeatingMonths; x++) {
   date = [gregorian dateByAddingComponents:monthComponent toDate:date options:0];
  [eventAlarmDate addObject:date];
}
NSCalendar*gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents*initialDateComponent=[[NSDateComponents alloc]init];
[初始日期组件设置日期:29];
[initialDateComponent setMonth:1];
[初始日期组件设置年份:2015];
NSDate*日期=[gregorian initialDateComponent];
NSLog(@“日期:%@”,日期);
NSDateComponents*monthComponent=[[NSDateComponents alloc]init];
monthComponent.month=1;
monthComponent.hour=0;
NSMutableArray*eventAlarmDate=[[NSMutableArray alloc]init];
int numberOfRepeatingMonths=4;
对于(int x=0;0
您实际上是在问我们如何在代码中引入bug

结果是正确的。在DST变更期间增加一个月将且必须增加一个小时,或比完整天数少一个小时


NSDate始终以UTC显示其日期,UTC是英国时间,无DST校正。计算的日期包含DST更正,具体取决于您所在的位置。如果您在印度,午夜是UTC上午5:30(或前一天UTC下午18:30,不确定)

因此,没有明显的方法可以在一年中不断增加时间,同时将时间保持在午夜。例如,如果有人希望从原始日期集开始,每月午夜重复一个日历事件?请参见上面问题编辑中的示例。在我上面添加的示例中,前两个日期设置为午夜,后两个日期设置为23:00。最好的解决方案是什么,将它们全部设置为所需的午夜时间?23:00是午夜。假设您坐在纽约运行代码。纽约时间比UTC晚五个小时。所以纽约的午夜是UTC早上5:00。在伦敦,冬季的午夜是凌晨0:00,而在春季,当您将时钟向前拨时,午夜是晚上23:00。我理解您关于使用UTC作为it参考点的NSDate的意思。但是,如果他们要努力返回当地时间,包括计算世界各地的DST,他们肯定还将包括一种方法,从设定时间的一天过渡到设定时间的第二天,考虑到DST带来的任何变化。从用户的角度来看,当他们指的是春季或冬季的午夜时,他们指的是当前当地时间的00:00am?NSLog显示UTC。正如我从一开始就说的,你得到的结果是正确的。