NSDateComponents组件:fromDate和Time Zones [英] NSDateComponents components:fromDate and Time Zones

查看:91
本文介绍了NSDateComponents组件:fromDate和Time Zones的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method which extracts the hour and second components form a NSDate by breaking it down into its NSDateComponents. My code is as follows...

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。

My problem is that I'm losing an hour in the conversion. I have a suspicion that it's due to timezones but as far as I can see both the date being passed in and the calendar being used are both GMT.

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

For example if I pass in the following NSDate object (this is a log of [NSDate description])...

2010-08-02 08:00:00 +0100

我希望我的小时数为8,分钟数为0,但我我的小时实际上回来了7。

I expect that I get 8 for hours and 0 for minutes, but I actually get back 7 for my hours.

我的系统时间是GMT,因此上面的NSDate目前是英国夏令时的+0100。

My system time is GMT, hence the NSDate above is currently +0100 for British Summer Time.

推荐答案

事情是,GMT不等于英国夏令时。日期 2010-08-02 08:00:00 +0100 等于 2010-08-02 07:00:00 GMT 所以你应该期待7个小时。

The thing is, GMT is not equal to British Summer Time. The date 2010-08-02 08:00:00 +0100 is equal to 2010-08-02 07:00:00 GMT so 7 hours is the result you should expect.

这篇关于NSDateComponents组件:fromDate和Time Zones的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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