Ios NSCalendar获取日期组件有问题
所以我有一个NSCalendar并做了一些测试: 首先,将系统时区更改为Ios NSCalendar获取日期组件有问题,ios,nscalendar,nsdatecomponents,Ios,Nscalendar,Nsdatecomponents,所以我有一个NSCalendar并做了一些测试: 首先,将系统时区更改为America/Los_Angeles(GMT-8),给出unix时间戳145555200000,将其转换为NSDate:by [NSDate dateWithTimeIntervalSince1970:1455552000000 / 1000]; 它返回2016-02-15 16:00:00+0000 然后使用日历生成日期组件: +(void)applyCalendarToCurrentTimeZone:(NSCalen
America/Los_Angeles(GMT-8)
,给出unix时间戳145555200000
,将其转换为NSDate:by
[NSDate dateWithTimeIntervalSince1970:1455552000000 / 1000];
它返回2016-02-15 16:00:00+0000
然后使用日历生成日期组件:
+(void)applyCalendarToCurrentTimeZone:(NSCalendar *)calendar {
if (![calendar.timeZone isEqualToTimeZone:[NSTimeZone localTimeZone]]) {
calendar.timeZone = [NSTimeZone localTimeZone];
}
}
+ (NSDateComponents *)getDateComponentsFromDate:(NSDate *)date {
NSDateComponents *components;
static NSCalendar *ISO8601 = nil;
static NSCalendarUnit unit;
if (!ISO8601) {
ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
ISO8601.firstWeekday = 2; // Sunday = 1, Saturday = 7
ISO8601.minimumDaysInFirstWeek = 7;
unit = NSCalendarUnitYear|NSCalendarUnitYearForWeekOfYear|NSCalendarUnitQuarter|NSCalendarUnitWeekOfYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
}
[ChartModel applyCalendarToCurrentTimeZone:ISO8601];
components = [ISO8601 components:unit fromDate:date];
return components;
}
但在调试时,我发现了问题
首先,一切看起来都很好,日期组件是正确的,Day
值在GMT-8时区为15
(lldb) po ISO8601.timeZone
America/Los_Angeles (GMT-8) offset -28800
(lldb) po date
2016-01-18 16:00:00 +0000
(lldb) po ISO8601.timeZone
America/Los_Angeles (GMT-8) offset -28800
(lldb) po date
2016-02-15 16:00:00 +0000
(lldb) po [ISO8601 components:unit fromDate:date];
<NSDateComponents: 0x13e4ae530>
Calendar Year: 2016
Month: 2
Leap month: no
Day: 15
Hour: 8
Minute: 0
Second: 0
Quarter: 0
Year for Week of Year: 2016
Week of Year: 7
(lldb)
(lldb) po ISO8601.timeZone
Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
(lldb) po date
2016-02-15 16:00:00 +0000
(lldb) po [ISO8601 components:unit fromDate:date];
<NSDateComponents: 0x13ec15150>
Calendar Year: 2016
Month: 2
Leap month: no
Day: 16
Hour: 0
Minute: 0
Second: 0
Quarter: 0
Year for Week of Year: 2016
Week of Year: 7
(lldb)
同样,在不重新启动应用程序的情况下,将系统时区改回GMT-8,现在奇怪的事情发生了,GMT-8时区的Day
值是16
,这是不正确的!我错过了什么?请帮忙
(lldb) po ISO8601.timeZone
Local Time Zone (America/Los_Angeles (GMT-8) offset -28800)
(lldb) po date
2016-02-15 16:00:00 +0000
(lldb) po [ISO8601 components:unit fromDate:date];
<NSDateComponents: 0x13f888260>
Calendar Year: 2016
Month: 2
Leap month: no
Day: 16
Hour: 0
Minute: 0
Second: 0
Quarter: 0
Year for Week of Year: 2016
Week of Year: 7
(lldb)
(lldb)po ISO8601.0时区
当地时区(美国/洛杉矶(GMT-8)偏移量-28800)
(lldb)采购订单日期
2016-02-15 16:00:00 +0000
(lldb)采购订单[ISO8601组件:单位起始日期:日期];
日历年:2016年
月份:2
闰月:没有
日期:16
小时:0
分钟:0
秒:0
季度:0
年度每周的年度:2016年
年内星期:7
(lldb)