at_yasu's blog

ロード的なことを

NSDate と HTTP Date

HTTPヘッダのIf-Modified-Sinceの日付型は、「Sun, 06 Nov 1994 08:49:37 GMT」と言う形。そして、NSDate型でその形にするカテゴリクラス。

@interface NSDate (NSDate)
+ (NSDateFormatter *) HTTPDateFormatter;
+ (NSDate*)   httpToDate:(NSString*)httpDate;
- (NSString*) dateToHttp;
@end

@implementation NSDate (NSDate)
+ (NSDateFormatter *) HTTPDateFormatter
{
    // Returns a formatter for dates in HTTP format (i.e. RFC 822, updated by RFC 1123).
    // e.g. "Sun, 06 Nov 1994 08:49:37 GMT"
    return [[[NSDateFormatter alloc]
             initWithDateFormat:@"%a, %d %b %Y %H:%M:%S GMT" 
             allowNaturalLanguage:NO] autorelease];
}

+ (NSDate*)   httpToDate:(NSString*)httpDate
{
    NSDateFormatter* f = [NSDate _HTTPDateFormatter];
    return [f dateFromString:httpDate];
}

- (NSString*) dateToHttp
{
    NSDateFormatter* f = [NSDate _HTTPDateFormatter];
    return [f stringFromDate:self];
}
@end

出元:MGTwitterEngine
参考:studyinghttp.net - このウェブサイトは販売用です! - 解説 仕様書 利用 技術 である 手法 日本語訳 プログラミング リソースおよび情報