Adjust NSDate by one hour for DST

折月煮酒 提交于 2020-01-13 14:03:51

问题


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.

UPDATE:

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];

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.

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.

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:

Update 2:

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];

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


回答1:


advise the use of

[formatter setTimeZone:localTimeZone];

instead.




回答2:


Seems like the issue is that they're showing all dates with "CST" but they are doing the DST correction. CST will never return YES for isDaylightSavingTime because that's how it's defined; "Central Time" changes from CST to CDT when daylight savings time comes into effect.

The easiest way to fix this is by ignoring the timezone identifier from the feed and always taking the date as relative to [NSTimeZone timeZoneWithName:@"America/Menominee"]. (Or some other timezone in Central Time.)

Edit: To be clear, you need to set the time zone of the NSDateFormatter when you're parsing the date, not when you're formatting it for display. That is, the change you need to make is not in the code you show.



来源:https://stackoverflow.com/questions/16741480/adjust-nsdate-by-one-hour-for-dst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!