Created
December 10, 2009 23:47
-
-
Save mro/253828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDateRFC1123TC.m | |
// Filmfest | |
// | |
// Created by Marcus Rohrmoser on 19.08.09. | |
// Copyright 2009 __MyCompanyName__. All rights reserved. | |
// | |
#import "LogicTC.h" | |
#import "NSDateRFC1123.h" | |
/** | |
http://blog.mro.name/2009/08/nsdateformatter-http-header/ and | |
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 | |
*/ | |
@interface NSDateRFC1123TC : LogicTC | |
{ | |
} | |
@end | |
@implementation NSDateRFC1123TC | |
-(void)testAsctime | |
{ | |
STAssertEqualObjects(@"Sun, 06 Nov 1994 08:49:37 GMT", [[NSDate dateFromRFC1123:@"Sun Nov 6 08:49:37 1994"] rfc1123String], @"fail"); | |
STAssertEqualObjects(@"Wed, 16 Nov 1994 08:49:37 GMT", [[NSDate dateFromRFC1123:@"Wed Nov 16 08:49:37 1994"] rfc1123String], @"fail"); | |
} | |
-(void)testRFC850 | |
{ | |
STAssertEqualObjects(@"Sun, 06 Nov 1994 08:49:37 GMT", [[NSDate dateFromRFC1123:@"Sunday, 06-Nov-94 08:49:37 GMT"] rfc1123String], @"fail"); | |
} | |
-(void)testRFC1123 | |
{ | |
NSString *s = @"Fri, 14 Aug 2009 14:45:31 GMT"; | |
STAssertEqualObjects(s, [[NSDate dateFromRFC1123:s] rfc1123String], @"fail"); | |
s = @"Sun, 06 Nov 1994 08:49:37 GMT"; | |
STAssertEqualObjects(s, [[NSDate dateFromRFC1123:s] rfc1123String], @"fail"); | |
STAssertEqualObjects(@"Sun, 06 Nov 1994 07:49:37 GMT", | |
[[NSDate dateFromRFC1123:@"SUN, 06 Nov 1994 08:49:37 CET"] rfc1123String], @"fail"); | |
STAssertEqualObjects(@"Sun, 06 Nov 1994 07:49:37 GMT", | |
[[NSDate dateFromRFC1123:@"Sunday, 06 Nov 1994 08:49:37 CET"] rfc1123String], @"fail"); | |
STAssertEqualObjects(@"Sun, 06 Nov 1994 07:49:37 GMT", | |
[[NSDate dateFromRFC1123:@"Sun, 06 Nov 1994 08:49:37 CET"] rfc1123String], @"fail"); | |
// a strange asymmetry: | |
STAssertEqualObjects(@"Sat, 01 Jan 0001 01:27:48 GMT", | |
[[NSDate dateFromRFC1123:@"Sat, 01 Jan 0001 00:00:00 GMT"] rfc1123String], @"fail"); | |
} | |
-(void)testRFC1123Mismatch | |
{ | |
STAssertNil([NSDate dateFromRFC1123:nil], @"fail"); | |
STAssertNil([NSDate dateFromRFC1123:@"Hello, world!"], @"fail"); | |
STAssertNil([NSDate dateFromRFC1123:@"Fru, 14 Aug 2009 14:45:31 GMT"], @"fail"); | |
STAssertNil([NSDate dateFromRFC1123:@"Fri, 29 Feb 2009 14:45:31 GMT"], @"fail"); | |
STAssertNil([NSDate dateFromRFC1123:@"Sunday, 06-Nov-94 08:49:37 FOO"], @"fail"); | |
STAssertNil([NSDate dateFromRFC1123:@"Sunda, 06-Nov-94 08:49:37 GMT"], @"fail"); | |
} | |
-(void)testRawRFC1123 | |
{ | |
NSDateFormatter *rfc1123 = [[NSDateFormatter alloc] init]; | |
rfc1123.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; | |
rfc1123.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
rfc1123.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; | |
NSString *s = @"Fri, 14 Aug 2009 14:45:31 GMT"; | |
STAssertEqualObjects(s, [rfc1123 stringFromDate:[rfc1123 dateFromString:s]], @"fail"); | |
s = @"Sun, 06 Nov 1994 08:49:37 GMT"; | |
STAssertEqualObjects(s, [rfc1123 stringFromDate:[rfc1123 dateFromString:s]], @"fail"); | |
s = @"Sat, 01 Jan 0001 08:00:00 GMT"; | |
STAssertEqualObjects(s, [rfc1123 stringFromDate:[rfc1123 dateFromString:s]], @"fail"); | |
s = @"Sat, 01 Jan 0001 08:00:00 GMT"; | |
STAssertEqualObjects(s, [rfc1123 stringFromDate:[rfc1123 dateFromString:s]], @"fail"); | |
[rfc1123 release]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDateRFC1123.h | |
// Filmfest | |
// | |
// Created by Marcus Rohrmoser on 19.08.09. | |
// Copyright 2009 __MyCompanyName__. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
/** Category on NSDate to add rfc1123 dates. Donated from the Filmfest App for free use as in free beer. | |
http://blog.mro.name/2009/08/nsdateformatter-http-header/ and | |
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 | |
*/ | |
@interface NSDate (NSDateRFC1123) | |
/** | |
Convert a RFC1123 'Full-Date' string (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1) into NSDate. | |
@param value_ something like either @"Fri, 14 Aug 2009 14:45:31 GMT" or @"Sunday, 06-Nov-94 08:49:37 GMT" or @"Sun Nov 6 08:49:37 1994" | |
@return nil if not parseable. | |
*/ | |
+(NSDate*)dateFromRFC1123:(NSString*)value_; | |
/** | |
Convert NSDate into a RFC1123 'Full-Date' string (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1). | |
@return something like @"Fri, 14 Aug 2009 14:45:31 GMT" | |
*/ | |
-(NSString*)rfc1123String; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDateRFC1123.m | |
// Filmfest | |
// | |
// Created by Marcus Rohrmoser on 19.08.09. | |
// Copyright 2009 __MyCompanyName__. All rights reserved. | |
// | |
#import "NSDateRFC1123.h" | |
@implementation NSDate (NSDateRFC1123) | |
+(NSDate*)dateFromRFC1123:(NSString*)value_ | |
{ | |
if(value_ == nil) | |
return nil; | |
static NSDateFormatter *rfc1123 = nil; | |
if(rfc1123 == nil) | |
{ | |
rfc1123 = [[NSDateFormatter alloc] init]; | |
rfc1123.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; | |
rfc1123.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
rfc1123.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss z"; | |
} | |
NSDate *ret = [rfc1123 dateFromString:value_]; | |
if(ret != nil) | |
return ret; | |
static NSDateFormatter *rfc850 = nil; | |
if(rfc850 == nil) | |
{ | |
rfc850 = [[NSDateFormatter alloc] init]; | |
rfc850.locale = rfc1123.locale; | |
rfc850.timeZone = rfc1123.timeZone; | |
rfc850.dateFormat = @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z"; | |
} | |
ret = [rfc850 dateFromString:value_]; | |
if(ret != nil) | |
return ret; | |
static NSDateFormatter *asctime = nil; | |
if(asctime == nil) | |
{ | |
asctime = [[NSDateFormatter alloc] init]; | |
asctime.locale = rfc1123.locale; | |
asctime.timeZone = rfc1123.timeZone; | |
asctime.dateFormat = @"EEE MMM d HH':'mm':'ss yyyy"; | |
} | |
return [asctime dateFromString:value_]; | |
} | |
-(NSString*)rfc1123String | |
{ | |
static NSDateFormatter *df = nil; | |
if(df == nil) | |
{ | |
df = [[NSDateFormatter alloc] init]; | |
df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; | |
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; | |
} | |
return [df stringFromDate:self]; | |
} | |
#if 0 | |
// Fri, 14 Aug 2009 14:45:31 GMT | |
NSLogD(@"Last-Modified: %@", [vc.response.allHeaderFields objectForKey:@"Last-Modified"]); | |
// df.calendar = @"gregorian"; | |
NSLogD(@"Now: %@", [NSDate date]); | |
for(NSString* fmt in [NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil]) | |
{ | |
rfc1123.dateFormat = [NSString stringWithFormat:@"%@%@%@", fmt, fmt, fmt]; | |
rfc1123.dateFormat = fmt; | |
NSLogD(@"Now (%@): %@", rfc1123.dateFormat, [rfc1123 stringFromDate:[NSDate date]]); | |
rfc1123.dateFormat = [rfc1123.dateFormat uppercaseString]; | |
NSLogD(@"Now (%@): %@", rfc1123.dateFormat, [rfc1123 stringFromDate:[NSDate date]]); | |
} | |
#endif | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I forked this for a project and updated it to use ARC.
I also fixed some broken tests and tried to make it recognize more timezones.
https://gist.github.com/yjkogan/7885169