使用NSDateFormatter和UIDatePicker时,我发现了一些奇怪的行为。 这是代码:
NSDate *now = [NSDate date];
NSDateFormatter *hourFormatter = [NSDateFormatter new];
NSDateComponents *dateComponents = [hourFormatter.calendar components:NSHourCalendarUnit fromDate:now];
UIDatePicker* picker = [UIDatePicker new];
NSLog(@"%@ %@ %@ %@", dateComponents, hourFormatter.calendar, picker.calendar, [NSCalendar currentCalendar]);
picker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSLog(@"%@ %@ %@ %@", dateComponents, hourFormatter.calendar, picker.calendar, [NSCalendar currentCalendar]);
dateComponents = [hourFormatter.calendar components:NSHourCalendarUnit fromDate:now];
NSLog(@"%@ %@ %@ %@", dateComponents, hourFormatter.calendar, picker.calendar, [NSCalendar currentCalendar]);
[UIDatePicker new];
dateComponents = [hourFormatter.calendar components:NSHourCalendarUnit fromDate:now];
NSLog(@"%@ %@ %@ %@", dateComponents, hourFormatter.calendar, picker.calendar, [NSCalendar currentCalendar]);
输出是下一个:
<NSDateComponents: 0x8b8e3e0>
Hour: 10 <__NSCFCalendar: 0x8b8ddc0> <_NSCopyOnWriteCalendarWrapper: 0x8b90580> <_NSCopyOnWriteCalendarWrapper: 0x8b907b0>
<NSDateComponents: 0x8b8e3e0>
Hour: 10 <__NSCFCalendar: 0x8b8ddc0> <_NSCopyOnWriteCalendarWrapper: 0x8dc99f0> <_NSCopyOnWriteCalendarWrapper: 0x8dc9af0>
<NSDateComponents: 0x8a89fb0>
Hour: 18 <__NSCFCalendar: 0x8b8ddc0> <_NSCopyOnWriteCalendarWrapper: 0x8a8af60> <_NSCopyOnWriteCalendarWrapper: 0x8a8ac10>
<NSDateComponents: 0x8dc9a40>
Hour: 10 <__NSCFCalendar: 0x8b8ddc0> <_NSCopyOnWriteCalendarWrapper: 0x8dca1c0> <_NSCopyOnWriteCalendarWrapper: 0x8dca1d0>
所以问题是 - 为什么在UIDatePicker中更改timeZone会导致创建不同的小时组件?
更新: 添加了日历记录