NSDateComponents返回奇怪的小时 [英] NSDateComponents returning strange hour

查看:50
本文介绍了NSDateComponents返回奇怪的小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing some code to acquire images that live on a server. The image filenames are in yyyyMMdd_HHmm format (in UTC) and generated every 3 hours (1200,1500,1800,..etc). When the code starts, I get the current UTC date and extract the time. I then create an NSDateComponent from this, and looking at the current time, set the hour of the NSDateComponent to the hour of the next image. Very strangely, if I set the hour to 3, the NSDate I get back from NSCalendar dateFromComponents has a different hour... what gives? (I have added in the console output as well).

// 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"吗?

The console output produced from the NSLogs is this. I get a value of 3 for hour but it gets returned as '1700'?

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

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

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

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

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

2010-01-25 12:25:02.052 Forecast[8447:207] hour 3

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

2010-01-25 12:25:02.053 Forecast[8447:207] minute 0

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

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

(我知道内存释放存在一些问题,但我想先使其发挥作用.)

(I'm aware there are some memory release issues but I want to get it functional first..)

推荐答案

可能是时区问题?

这篇关于NSDateComponents返回奇怪的小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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