Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Classes/NSManagedObject+ActiveRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@

+ (NSString *)entityName;

#pragma mark - Date Formatting

+ (void)setFormatter:(NSString *)newFormatter;

@end
24 changes: 17 additions & 7 deletions Classes/NSManagedObject+ActiveRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ - (void)setSafeValue:(id)value forKey:(id)key {
value = [NSNumber numberWithDouble:[value doubleValue]];

else if (attributeType == NSDateAttributeType)
value = [self.defaultFormatter dateFromString:value];
value = [self.formatter dateFromString:value];
}

[self setValue:value forKey:key];
Expand All @@ -234,15 +234,25 @@ - (BOOL)isIntegerAttributeType:(NSAttributeType)attributeType {

#pragma mark - Date Formatting

- (NSDateFormatter *)defaultFormatter {
static NSDateFormatter *sharedFormatter;
static dispatch_once_t singletonToken;
static NSDateFormatter *sharedFormatter;
static dispatch_once_t singletonToken;

+(NSDateFormatter *)sharedFormatter{
dispatch_once(&singletonToken, ^{
sharedFormatter = [[NSDateFormatter alloc] init];
[sharedFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
if(!sharedFormatter){
sharedFormatter = [[NSDateFormatter alloc] init];
[sharedFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
}
});

return sharedFormatter;
}

- (NSDateFormatter *)formatter {
return [NSManagedObject sharedFormatter];
}

+ (void)setFormatter:(NSString *)newFormatter {
[sharedFormatter setDateFormat:newFormatter];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about name convenience?

- (void)setDateFormat:(NSString *)dateFormat {
    [sharedFormatter setDateFormat:dateFormat];
}

@end
12 changes: 12 additions & 0 deletions Example/SampleProjectTests/FindersAndCreatorsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
Person *person = [Person create:@{ @"anniversary": [formatta stringFromDate:date] }];
[[@([date timeIntervalSinceDate:person.anniversary]) should] beLessThan:@1];
});

it(@"converts string from specified date format", ^{
[NSManagedObject setFormatter:@"HH:mm:ss"];
Person *person = [Person create:@{ @"anniversary": @"12:24:05" }];
[person.anniversary shouldNotBeNil];
});

it(@"can't convert string from specified date format", ^{
[NSManagedObject setFormatter:@"yyyy HH:mm:ss"];
Person *person = [Person create:@{ @"anniversary": @"12:24:05" }];
[person.anniversary shouldBeNil];
});

it(@"doesn't update with nulls", ^{
Person *person = fetchUniquePerson();
Expand Down