iOS 当前时间需要时区?

iOS current time require timezone?

在下面的示例中,它会始终显示用户设备上的当前时间(无论用户身在何处)还是只显示 GMT,我是否需要获取和设置时区?

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                    initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components =
[gregorian components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:today];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];

NSLog(@"hour %d", hour);
NSLog(@"minute %d", minute);
NSLog(@"second %d", second);

如果要显示用户设备的当前时间,应添加以下代码行。

components.timeZone = [NSTimeZone systemTimeZone];

From苹果指南:

By default, NSCalendar uses the default time zone for the application—or process—when the calendar object is created. Unless the default time zone has been otherwise set, it is the time zone set in System Preferences.

因此,您不需要对代码进行任何更改。