Skip to content

Reviewed DateFormatter used for date conversion #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions KeyValueObjectMapping/DCNSDateConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@interface DCNSDateConverter()
@property(nonatomic, strong) NSString *pattern;
- (BOOL) validDouble: (NSString *) doubleValue;
- (NSDateFormatter *)dateFormatter;
@end

@implementation DCNSDateConverter
Expand All @@ -32,8 +33,7 @@ - (id)transformValue:(id)value forDynamicAttribute:(DCDynamicAttribute *)attribu
if(validDouble){
return [NSDate dateWithTimeIntervalSince1970:[value doubleValue]];
}else{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = self.pattern;
NSDateFormatter *formatter = [self dateFormatter];
return [formatter dateFromString:value];
}
}
Expand All @@ -48,4 +48,20 @@ - (BOOL) canTransformValueForClass: (Class) class {
- (BOOL) validDouble: (NSString *) doubleValue {
return [[[NSNumberFormatter alloc] init] numberFromString:doubleValue] != nil;
}


#pragma mark - Private Methods

- (NSDateFormatter *)dateFormatter {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:self.pattern];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

return dateFormatter;
}

@end