Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.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/5/objective-c/26.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 NSDateComponents组件:fromDate和时区_Iphone_Objective C_Nsdatecomponents - Fatal编程技术网

Iphone NSDateComponents组件:fromDate和时区

Iphone NSDateComponents组件:fromDate和时区,iphone,objective-c,nsdatecomponents,Iphone,Objective C,Nsdatecomponents,我有一种方法,它通过将小时和秒组件分解为NSDate组件,从而将其提取为NSDate组件。我的代码如下 unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone

我有一种方法,它通过将小时和秒组件分解为NSDate组件,从而将其提取为NSDate组件。我的代码如下

unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents* travelDateTimeComponents = [calendar components:hourAndMinuteFlags fromDate:travelDate];
NSString* hours = [NSString stringWithFormat:@"%02i", [travelDateTimeComponents hour]];
NSString* minutes = [NSString stringWithFormat:@"%02i", [travelDateTimeComponents minute]];
我的问题是我在转换过程中损失了一个小时。我怀疑这是由于时区造成的,但据我所知,传入的日期和使用的日历都是GMT

例如,如果我传入以下NSDate对象(这是[NSDate description]的日志)

我希望我的小时数是8,分钟数是0,但实际上我的小时数是7


我的系统时间是GMT,因此上面的NSDate对于英国夏季时间是+0100。

问题是,GMT不等于英国夏季时间。日期
2010-08-02 08:00:00+0100
等于
2010-08-02 07:00:00 GMT
,因此7小时是您应该期望的结果。

还请记住,
NSDate
对象始终根据GMT显示时间,而从该日期提取的组件则由
timeZoneWithName
部分更改

例如:

NSDate *today = [NSDate date];
NSLog(@"Today's date: %@",today);

unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents* travelDateTimeComponents = [calendar components:hourAndMinuteFlags fromDate:today];
NSString* hours = [NSString stringWithFormat:@"%02i", [travelDateTimeComponents hour]];
NSString* minutes = [NSString stringWithFormat:@"%02i", [travelDateTimeComponents minute]];

NSLog(@"Calendar: %@",calendar);
NSLog(@"Travel Components: %@",travelDateTimeComponents);
NSLog(@"Hours: %@",hours);
NSLog(@"Minutes: %@",minutes);
控制台输出:

[Session started at 2011-01-23 12:59:17 -0700.]
2011-01-23 12:59:19.755 testttt[5697:207] Today's date: 2011-01-23 19:59:19 +0000
2011-01-23 12:59:19.756 testttt[5697:207] Calendar: <__NSCFCalendar: 0x4b2ca70>
2011-01-23 12:59:19.757 testttt[5697:207] Travel Components: <NSDateComponents: 0x4b2de20>
2011-01-23 12:59:19.757 testttt[5697:207] Hours: 19
2011-01-23 12:59:19.758 testttt[5697:207] Minutes: 59
输出:

2011-01-23 13:05:29.896 testttt[5723:207] Today's date: 2011-01-23 20:05:29 +0000
2011-01-23 13:05:29.897 testttt[5723:207] Calendar: <__NSCFCalendar: 0x4e10020>
2011-01-23 13:05:29.897 testttt[5723:207] Travel Components: <NSDateComponents: 0x4e0f6d0>
2011-01-23 13:05:29.898 testttt[5723:207] Hours: 13
2011-01-23 13:05:29.898 testttt[5723:207] Minutes: 05
2011-01-23 13:05:29.896 testttt[5723:207]今天的日期:2011-01-23 20:05:29+0000
2011-01-23 13:05:29.897测试TTT[5723:207]日历:
2011-01-23 13:05:29.897测试TTT[5723:207]行程组件:
2011-01-23 13:05:29.898测试时间:13
2011-01-23 13:05:29.898测试TTT[5723:207]分钟:05

注意当地时间和分钟是如何变化的,但是“今天的日期:”部分仍然反映了GMT。如果你问我的话,对程序员来说有点误导。

我同意aqua。这是非常误导,并导致我的头撞在屏幕上的日子。其工作原理如下:[NSDate date]始终返回当前日期。因此,在上面的例子中,它将是2010-08-02 08:00:00。当我们转换成NSDateComponents时,实际时间会丢失,并转换成一组不再包含时区的整数(小时、年等)

当转换回NSDate对象时,它将获取年、月、日等值,并将其相对于GMT提供给您,因此会损失小时数。这只是框架中决定的方式。我所做的是:

+(NSDateComponents *)getComponentFromDate:(NSDate *)date {

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

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit;
    NSDateComponents* components = [gregorian components:unitFlags fromDate:date];

    NSTimeZone* timeZone = [NSTimeZone localTimeZone];
    if(timeZone.isDaylightSavingTime) components.hour = ((int)timeZone.daylightSavingTimeOffset/3600);

    [gregorian release];

    return components;
}

这将为我提供一个四舍五入到当前日期的组件的“更正”版本,当传递到日历以检索日期时,该组件将始终设置为作为参数传递的日期当天的00:00:00。

因此,如果我只想将现在的时间与小于2小时的开始和结束日期进行比较,测试失败,因为now的时间前面还有now的日期,而其他两个时间将以2000为基准年创建。那么在这种情况下如何比较呢?我认为转换组件->日期会考虑日历的时区。如果使用相同的日历,您应该能够进行无损双向转换。
2011-01-23 13:05:29.896 testttt[5723:207] Today's date: 2011-01-23 20:05:29 +0000
2011-01-23 13:05:29.897 testttt[5723:207] Calendar: <__NSCFCalendar: 0x4e10020>
2011-01-23 13:05:29.897 testttt[5723:207] Travel Components: <NSDateComponents: 0x4e0f6d0>
2011-01-23 13:05:29.898 testttt[5723:207] Hours: 13
2011-01-23 13:05:29.898 testttt[5723:207] Minutes: 05
+(NSDateComponents *)getComponentFromDate:(NSDate *)date {

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

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit;
    NSDateComponents* components = [gregorian components:unitFlags fromDate:date];

    NSTimeZone* timeZone = [NSTimeZone localTimeZone];
    if(timeZone.isDaylightSavingTime) components.hour = ((int)timeZone.daylightSavingTimeOffset/3600);

    [gregorian release];

    return components;
}