-
-
Save yjkogan/7885169 to your computer and use it in GitHub Desktop.
This file contains hidden or 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. | |
// Updated by Yonatan Kogan @Optimizely on 2013-12-9 | |
// | |
#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 hidden or 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. | |
// Updated by Yonatan Kogan @Optimizely on 2013-12-9 | |
// | |
#import "NSDateRFC1123.h" | |
@implementation NSDate (NSDateRFC1123) | |
+(NSDate*)dateFromRFC1123:(NSString*)value_ | |
{ | |
if(value_ == nil) | |
return nil; | |
static NSDateFormatter *rfc1123 = nil; | |
static NSString *localeIdentifier = @"en_GB"; | |
if(rfc1123 == nil) | |
{ | |
rfc1123 = [[NSDateFormatter alloc] init]; | |
rfc1123.locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]; | |
rfc1123.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
rfc1123.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss z"; | |
} | |
NSDate *ret = [rfc1123 dateFromString:value_]; | |
if (ret == nil) { | |
localeIdentifier = [[self class] getOtherLocaleIdentifier:localeIdentifier]; | |
rfc1123.locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]; | |
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"]; | |
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; | |
} | |
return [df stringFromDate:self]; | |
} | |
+ (NSString *)getOtherLocaleIdentifier:(NSString *)currentLocaleIdentifier | |
{ | |
if ([currentLocaleIdentifier isEqualToString:@"en_US"]) { | |
return @"en_GB"; | |
} else { | |
return @"en_US"; | |
} | |
} | |
@end |
This file contains hidden or 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. | |
// Updated by Yonatan Kogan @Optimizely on 2013-12-9 | |
// | |
#import <SenTestingKit/SenTestingKit.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 : SenTestCase | |
@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"); | |
} | |
-(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"]; | |
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 = @"Mon, 01 Jan 0001 08:00:00 GMT"; | |
STAssertEqualObjects(s, [rfc1123 stringFromDate:[rfc1123 dateFromString:s]], @"fail"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment