Factor date formatting into reusable convenience functions

This commit is contained in:
Blake Watters
2012-09-28 14:55:54 -04:00
parent a04e861669
commit ecb6af4776
3 changed files with 74 additions and 29 deletions

View File

@@ -76,6 +76,9 @@ extern NSString * const RKObjectMappingNestingAttributeKeyName;
@property (nonatomic, strong) RKObjectMapping *objectMapping; // The concrete mapping
@end
// Defined in RKObjectMapping.h
NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters);
@implementation RKMappingOperation
- (id)initWithSourceObject:(id)sourceObject destinationObject:(id)destinationObject mapping:(RKMapping *)objectOrDynamicMapping
@@ -98,35 +101,7 @@ extern NSString * const RKObjectMappingNestingAttributeKeyName;
- (NSDate *)parseDateFromString:(NSString *)string
{
RKLogTrace(@"Transforming string value '%@' to NSDate...", string);
NSDate *date = nil;
if (![string isEqualToString:@""]) {
for (NSFormatter *dateFormatter in self.objectMapping.dateFormatters) {
BOOL success;
@synchronized(dateFormatter) {
if ([dateFormatter isKindOfClass:[NSDateFormatter class]]) {
RKLogTrace(@"Attempting to parse string '%@' with format string '%@' and time zone '%@'", string, [(NSDateFormatter *)dateFormatter dateFormat], [(NSDateFormatter *)dateFormatter timeZone]);
}
NSString *errorDescription = nil;
success = [dateFormatter getObjectValue:&date forString:string errorDescription:&errorDescription];
}
if (success && date) {
if ([dateFormatter isKindOfClass:[NSDateFormatter class]]) {
RKLogTrace(@"Successfully parsed string '%@' with format string '%@' and time zone '%@' and turned into date '%@'",
string, [(NSDateFormatter *)dateFormatter dateFormat], [(NSDateFormatter *)dateFormatter timeZone], date);
} else if ([dateFormatter isKindOfClass:[NSNumberFormatter class]]) {
NSNumber *formattedNumber = (NSNumber *)date;
date = [NSDate dateWithTimeIntervalSince1970:[formattedNumber doubleValue]];
}
break;
}
}
}
return date;
return RKDateFromStringWithFormatters(string, self.objectMapping.dateFormatters);
}
- (id)transformValue:(id)value atKeyPath:(NSString *)keyPath toType:(Class)destinationType

View File

@@ -346,3 +346,29 @@
+ (void)setPreferredDateFormatter:(NSFormatter *)dateFormatter;
@end
///----------------
/// @name Functions
///----------------
/**
Returns an date representation of a given string value by attempting to parse the string with all default date formatters in turn.
@param dateString A string object encoding a date value.
@return An `NSDate` object parsed from the given string, or `nil` if the string was found to be unparsable by all default date formatters.
@see [RKObjectMapping defaultDateFormatters]
*/
NSDate *RKDateFromString(NSString *dateString);
/**
Returns a string representation of a given date formatted with the preferred date formatter.
This is a convenience function that is equivalent to the following example code:
NSString *string = [[RKObjectMapping preferredDateFormatter] stringForObjectValue:date]
@param date The date object to be formatted.
@return An `NSString` object representation of the given date formatted by the preferred date formatter.
@see [RKObjectMapping preferredDateFormatter]
*/
NSString *RKStringFromDate(NSDate *date);

View File

@@ -30,6 +30,9 @@
NSString * const RKObjectMappingNestingAttributeKeyName = @"<RK_NESTING_ATTRIBUTE>";
static NSUInteger RKObjectMappingMaximumInverseMappingRecursionDepth = 100;
// Private declaration
NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters);
@interface RKObjectMapping ()
@property (nonatomic, weak, readwrite) Class objectClass;
@property (nonatomic, strong) NSMutableArray *mutablePropertyMappings;
@@ -404,3 +407,44 @@ static NSDateFormatter *preferredDateFormatter = nil;
}
@end
#pragma mark - Functions
NSDate *RKDateFromStringWithFormatters(NSString *dateString, NSArray *formatters)
{
NSDate *date = nil;
for (NSFormatter *dateFormatter in formatters) {
BOOL success;
@synchronized(dateFormatter) {
if ([dateFormatter isKindOfClass:[NSDateFormatter class]]) {
RKLogTrace(@"Attempting to parse string '%@' with format string '%@' and time zone '%@'", dateString, [(NSDateFormatter *)dateFormatter dateFormat], [(NSDateFormatter *)dateFormatter timeZone]);
}
NSString *errorDescription = nil;
success = [dateFormatter getObjectValue:&date forString:dateString errorDescription:&errorDescription];
}
if (success && date) {
if ([dateFormatter isKindOfClass:[NSDateFormatter class]]) {
RKLogTrace(@"Successfully parsed string '%@' with format string '%@' and time zone '%@' and turned into date '%@'",
dateString, [(NSDateFormatter *)dateFormatter dateFormat], [(NSDateFormatter *)dateFormatter timeZone], date);
} else if ([dateFormatter isKindOfClass:[NSNumberFormatter class]]) {
NSNumber *formattedNumber = (NSNumber *)date;
date = [NSDate dateWithTimeIntervalSince1970:[formattedNumber doubleValue]];
}
break;
}
}
return date;
}
NSDate *RKDateFromString(NSString *dateString)
{
return RKDateFromStringWithFormatters(dateString, [RKObjectMapping defaultDateFormatters]);
}
NSString *RKStringFromDate(NSDate *date)
{
return [[RKObjectMapping preferredDateFormatter] stringForObjectValue:date];
}