gpt4 book ai didi

ios - 为 DST 将 NSDate 调整一小时

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:59 25 4
gpt4 key购买 nike

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

更新:

我尝试了一种不同的方法来在 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 而不是美国时间。

请不要投票关闭它。这并不太局限,它适用于美国所有实行夏令时的州。

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

enter image description here

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

enter image description here

更新 2:

我尝试在我的代码中手动将时区设置为中部,但它仍然做同样的事情。在 cellForRowAtIndexPath 我有:

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。

最佳答案

建议使用

[formatter setTimeZone:localTimeZone];

相反。

关于ios - 为 DST 将 NSDate 调整一小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com