DTFoundation / DTFoundation / NSDate+DTHTTPDateStrings.m

//
//  NSDate+DTHTTPDateStrings.m
//  DTFoundation
//
//  Created by Daniel Tull on 18.12.2009.
//  Copyright 2009 Daniel Tull. All rights reserved.
//

#import "NSDate+DTHTTPDateStrings.h"



/*
 Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
 Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
 */


@implementation NSDate (DTHTTPDateStrings)

+ (NSDate *)dateFromHTTPFormattedDateString:(NSString *)string {
	
    if (!string) return nil;
	
	NSDate *returnDate = [self dateFromRFC1123String:string];
	if (returnDate) return returnDate;
	
	returnDate = [self dateFromRFC1036String:string];
	if (returnDate) return returnDate;
	
	return [self dateFromANSICString:string];
}

+ (NSDate *)dateFromRFC1123String:(NSString *)string {
	NSDateFormatter *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 z";
    NSDate *returnDate = [df dateFromString:string];
	[df release];
	return returnDate;	
}

+ (NSDate *)dateFromRFC1036String:(NSString *)string {
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
	df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	df.dateFormat = @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z";
    NSDate *returnDate = [df dateFromString:string];
	[df release];
	return returnDate;	
}

+ (NSDate *)dateFromANSICString:(NSString *)string {
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
	df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	df.dateFormat = @"EEE MMM d HH':'mm':'ss yyyy";
    NSDate *returnDate = [df dateFromString:string];
	[df release];
	return returnDate;
}

- (NSString *)HTTPFormattedDateString {
	return [self RFC1123String];
}

- (NSString *)RFC1123String {
	NSDateFormatter *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];
}

- (NSString *)RFC1036String {
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
	df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	df.dateFormat = @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z";
    return [df stringFromDate:self];
}

- (NSString *)ANSICString {
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
	df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	df.dateFormat = @"EEE MMM d HH':'mm':'ss yyyy";
    return [df stringFromDate:self];
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.