Skip to content

Instantly share code, notes, and snippets.

@ryandevore
Created July 30, 2013 06:22
Show Gist options
  • Save ryandevore/6110715 to your computer and use it in GitHub Desktop.
Save ryandevore/6110715 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
extern NSString * const kUURFC3339DateFormatter;
@interface UUDateFormatter : NSObject
+ (NSDate*) dateFromRfc3339String:(NSString*)string;
+ (NSDate*) dateFromString:(NSString*)string withFormat:(NSString*)format;
+ (NSString*) dateToRfc3339String:(NSDate*)date;
+ (NSString*) dateToString:(NSDate*)date withFormat:(NSString*)format;
@end
#import "UUDateFormatter.h"
static NSMutableDictionary* theSharedDateFormatterCache = nil;
NSString * const kUURFC3339DateFormatter = @"yyyy-MM-dd'T'HH:mm:ssZZ";
@implementation UUDateFormatter
+ (NSDate*) dateFromRfc3339String:(NSString*)string
{
return [self dateFromString:string withFormat:kUURFC3339DateFormatter];
}
+ (NSDate*) dateFromString:(NSString*)string withFormat:(NSString*)format
{
NSDateFormatter* df = [self getDateFormatter:format];
return [df dateFromString:string];
}
+ (NSString*) dateToRfc3339String:(NSDate*)date
{
return [self dateToString:date withFormat:kUURFC3339DateFormatter];
}
+ (NSString*) dateToString:(NSDate*)date withFormat:(NSString*)format
{
NSDateFormatter* df = [self getDateFormatter:format];
return [df stringFromDate:date];
}
+ (NSMutableDictionary*) sharedCache
{
if (theSharedDateFormatterCache == nil)
{
theSharedDateFormatterCache = [[NSMutableDictionary alloc] init];
}
return theSharedDateFormatterCache;
}
+ (NSDateFormatter*) getDateFormatter:(NSString*)formatter
{
NSMutableDictionary* cache = [self sharedCache];
NSDateFormatter* df = [cache valueForKey:formatter];
if (df == nil)
{
df = [[NSDateFormatter alloc] init];
[df setDateFormat:formatter];
[cache setValue:df forKey:formatter];
}
return df;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment