【问题标题】:NSDateComponents returning strange hourNSDateComponents 返回奇怪的时间
【发布时间】:2010-01-25 11:34:07
【问题描述】:

我正在编写一些代码来获取服务器上的图像。图像文件名采用 yyyyMMdd_HHmm 格式(UTC),每 3 小时生成一次(1200,1500,1800,..等)。当代码开始时,我得到当前的 UTC 日期并提取时间。然后我从中创建一个 NSDateComponent,并查看当前时间,将 NSDateComponent 的小时设置为下一张图像的小时。很奇怪,如果我将小时设置为 3,我从 NSCalendar dateFromComponents 返回的 NSDate 有一个 不同的小时...什么给出? (我也在控制台输出中添加了)。

// get current time in UTC
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *UTC = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:UTC];

//  get the current hour & min
[dateFormatter setDateFormat:@"HHmm"];
NSDate *today = [NSDate date];
NSString *str = [dateFormatter stringFromDate:today];
NSInteger hour = [str integerValue];
NSLog(str);

// find next image hour
NSCalendar *cal = [[NSCalendar alloc]   initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *currentImageDateComps = [cal components:unitFlags fromDate:today];

if (hour > 2100) {
    [currentImageDateComps setHour:0];
    // add an extra day as next image is start of new day
    [currentImageDateComps setDay:[currentImageDateComps day]+1];

} else if (hour < 300) {
    [currentImageDateComps setHour:3];
} else if (hour < 600) {
    [currentImageDateComps setHour:6];
} else if (hour < 900) {
    [currentImageDateComps setHour:9];
} else if (hour < 1200) {
    [currentImageDateComps setHour:12];
} else if (hour < 1500) {
    [currentImageDateComps setHour:15];
} else if (hour < 1800) {
    [currentImageDateComps setHour:18];
} else if (hour < 2100) {
    [currentImageDateComps setHour:21];
}
[currentImageDateComps setMinute:0];


NSLog(@"hour %d", [currentImageDateComps hour]);
NSLog(@"minute %d", [currentImageDateComps minute]);
// construct the date
NSDate *currentImageDate = [cal dateFromComponents:currentImageDateComps];
[dateFormatter setDateFormat:@"yyyyMMdd_hhmm"];
NSString *nextImage = [dateFormatter stringFromDate:currentImageDate];

self.currentFilename = [nextImage stringByAppendingString:@".png"];
NSLog(self.currentFilename);

从 NSLogs 产生的控制台输出是这样的。我得到一个 3 小时的值,但它返回为“1700”?

2010-01-25 12:25:02.049 预测[8447:207] viewDidLoad

2010-01-25 12:25:02.052 预测[8447:207] 0225

2010-01-25 12:25:02.052 预测[8447:207] 小时 3

2010-01-25 12:25:02.053 预测[8447:207] 分钟 0

2010-01-25 12:25:02.053 预测[8447:207] 20100124_1700.png

(我知道有一些内存释放问题,但我想先让它发挥作用..)

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    可能是时区问题?

    【讨论】:

    • 是的,看来这就是问题所在。我猜当您创建 NSCalendar 时,它会使用您的默认时区。我的 NSDateFormatter 将时区设置为 UTC,但是从 NSCalendar 派生的 NSDateComponents 没有,因此存在差异。我在创建 NSCalendar 对象后添加了 [cal setTimeZone:UTC],现在我所有的日期/时间都是我所期望的。
    【解决方案2】:

    有什么问题:

    NSString *nextImage = [NSString stringWithFormat:@"%04d%02d%02d_%02d%02d.png", [currentImageDateComps year], [currentImageDateComps month], [currentImageDateComps day], [currentImageDateComps hour], [currentImageDateComps minute]];

    self.currentFilename = nextImage;

    【讨论】:

    • 是的,它在您可以确定没有任何参数超出有效时间范围的情况下工作。事实上,由于 self.currentFilename 是一个 NSString,我可以简单地使用 'self.currentFilename = [NSString stringWithFormat:@"...。但如果你想生成日期,比如通过循环和增加小时数,你会得到无效的日期。在这种情况下, [cal dateFromComponents:..] 将产生有效的日期....给定您的时区是好的 :-)
    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 2020-09-19
    • 2012-05-28
    • 2011-07-05
    • 2020-07-18
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多