Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a few NSDateFormatter helpers for Objective-C
/* f@s2n.io */
// DateFormat Reference : http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
#import <Foundation/Foundation.h>
extern NSString* const DATEFORMAT_RFC3339;
@interface NSDate (FMDateFormatters)
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format locale:(NSLocale*)locale timezone:(NSTimeZone*)zone;
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format;
+ (NSDate*)dateWithPosixString:(NSString*)dateString format:(NSString*)format;
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone locale:(NSLocale*)locale;
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone;
- (NSString*)formattedString:(NSString*)format withOptions:(void(^)(NSDateFormatter* formatter))optionsBlock;
- (NSString*)formattedString:(NSString*)format;
- (NSString*)posixFormattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone;
- (NSString*)posixFormattedString:(NSString*)format;
@end
/* f@s2n.io */
#import "NSDate+Formatters.h"
NSString* const DATEFORMAT_RFC3339 = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
@implementation NSDate (FMDateFormatters)
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone locale:(NSLocale*)locale {
NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
if (locale)
formatter.locale = locale;
formatter.dateFormat = format;
if (zone)
formatter.timeZone = zone;
return [formatter stringFromDate:self];
}
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone {
return [self formattedString:format withTimeZone:zone locale:NSLocale.currentLocale];
}
- (NSString*)formattedString:(NSString*)format withOptions:(void(^)(NSDateFormatter* formatter))optionsBlock {
__block NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
[formatter setDateFormat:format];
optionsBlock(formatter);
return [formatter stringFromDate:self];
}
+ (NSTimeZone*)posixTimeZone {
return [NSTimeZone timeZoneForSecondsFromGMT:0];
}
- (NSString*)formattedString:(NSString*)format {
return [self formattedString:format withTimeZone:nil locale:NSLocale.currentLocale];
}
- (NSString*)posixFormattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone {
NSLocale* posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease;
return [self formattedString:format withTimeZone:zone locale:posixLocale];
}
- (NSString*)posixFormattedString:(NSString*)format {
NSLocale* posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease;
return [self formattedString:format withTimeZone:self.class.posixTimeZone locale:posixLocale];
}
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format {
return [self dateWithString:dateString format:format locale:NSLocale.currentLocale timezone:nil];
}
+ (NSDate*)dateWithPosixString:(NSString*)dateString format:(NSString*)format {
return [self dateWithString:dateString
format:format
locale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease
timezone:self.class.posixTimeZone];
}
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format locale:(NSLocale*)locale timezone:(NSTimeZone*)zone {
if (!dateString || !format) return nil;
NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
if (locale)
formatter.locale = locale;
formatter.dateFormat = format;
if (zone)
formatter.timeZone = zone;
return [formatter dateFromString:dateString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment