将 NSDate 调整为 DST 一小时

将 NSDate 调整为 DST 一小时

问题描述:

我的应用程序使用来自 localendar.com 的提要来创建即将发生的事件的表格视图.但问题是来自 localendar 的提要没有考虑 DST,因此它们最终将提要的 pubDate 显示为比事件实际开始晚 1 小时.我的应用程序解析提要并将日期带入单元格中的详细信息行,这使它变得混乱.我试图让我的应用程序检测用户是否处于夏令时,如果是,则从日期中减去一小时.但是,当我使用下面的代码时,没有任何变化,并且永远不会在 isDaylightSavingsTime 属性标记上调用 NSLog.

My app uses a feed from localendar.com to create a tableview of upcoming events. The issue though, is that the feeds from localendar do not account for DST, so they end up showing the pubDate of the feed as 1 hour later than the event actually starts. My app parses the feed and takes the date into the detail line in the cell, which makes it confusing. I am trying to get my app to detect whether or not the user is in DST, and if they are, subtract one hour from the date. However, when I use the code below, nothing changes, and a NSLog never gets called on the isDaylightSavingsTime property tag.

更新:

我在 cellForRowAtIndexPath 中尝试了不同的方法来改变它:

I have tried a different approach in changing it up in the cellForRowAtIndexPath:

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];


    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
    NSLog(@"after formatter %@", articleDateString);



       cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", articleDateString];

问题是 RSS 提要 pubDate 中的时间没有针对 DST 进行调整,因此它比一年中的半年有一个小时.格式化程序会按时区正确执行所有操作,因为提要中的时间已经错误,因为它使用 GMT 而不是美国时间.

The issue is that the time in the RSS feed pubDate does not adjust for DST, so it is an hour off half of the year. The formatter does everything correct by time zone, because the time in the feed is already wrong as it uses GMT and not US Time.

请不要投票关闭此功能.这不是太本地化,它适用于美国有夏令时的每个州.

PLEASE, do NOT vote to close this. This is not too localized, it applies to every state in the USA that has daylight savings time.

这里有两张图片可以更好地描述这个问题.第一个显示 RSS Feed 中的内容,第二个显示日历中的正确时间.

Here are two pics to describe the issue better. The first shows what is in the RSS Feed, and the second shows what is in the calendar which is the correct time.

如您所见,Feed 中的时间显示为 10:00,而显示每个地点的实际时间但 RSS Feed 显示为:

As you see, that time in the feed shows as 10:00, while the actual time that displays every place BUT the RSS feed shows as:

更新 2:

我尝试在我的代码中手动将时区设置为 Central,但它仍然执行相同的操作.在 cellForRowAtIndexPath 我有:

I tried setting the timezone to Central manually in my code, but it still does the same thing. In cellForRowAtIndexPath I have:

RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Menominee"]]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
       cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", articleDateString];

然而,它没有让 detailTextLabel.text 显示 5/27/13 10:00 PM,而是显示 5/27/13 11:00 PM.

However, instead of getting the detailTextLabel.text to show 5/27/13 10:00 PM, it shows 5/27/13 11:00 PM still.

建议使用

[formatter setTimeZone:localTimeZone];

相反.