Skip to content

Commit

Permalink
Merge pull request #78 from Brok3nHalo/master
Browse files Browse the repository at this point in the history
Added millisecond support to date parsing
  • Loading branch information
ibireme committed Apr 15, 2016
2 parents 9af57fc + a86d88c commit 323ffa7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Framework/Info.plist
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2</string>
<string>1.0.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion YYModel.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = 'YYModel'
s.summary = 'High performance model framework for iOS/OSX.'
s.version = '1.0.2'
s.version = '1.0.3'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.authors = { 'ibireme' => 'ibireme@gmail.com' }
s.social_media_url = 'http://blog.ibireme.com'
Expand Down
41 changes: 39 additions & 2 deletions YYModel/NSObject+YYModel.m
Expand Up @@ -134,7 +134,7 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
/// Parse string to date.
static force_inline NSDate *YYNSDateFromString(__unsafe_unretained NSString *string) {
typedef NSDate* (^YYNSDateParseBlock)(NSString *string);
#define kParserNum 32
#define kParserNum 34
static YYNSDateParseBlock blocks[kParserNum + 1] = {0};
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand All @@ -153,6 +153,8 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
/*
2014-01-20 12:24:48
2014-01-20T12:24:48 // Google
2014-01-20 12:24:48.000
2014-01-20T12:24:48.000
*/
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
formatter1.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
Expand All @@ -163,6 +165,16 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter2.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter2.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
formatter3.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter3.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter3.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

NSDateFormatter *formatter4 = [[NSDateFormatter alloc] init];
formatter4.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter4.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter4.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";

blocks[19] = ^(NSString *string) {
if ([string characterAtIndex:10] == 'T') {
Expand All @@ -171,30 +183,55 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
return [formatter2 dateFromString:string];
}
};

blocks[23] = ^(NSString *string) {
if ([string characterAtIndex:10] == 'T') {
return [formatter3 dateFromString:string];
} else {
return [formatter4 dateFromString:string];
}
};
}

{
/*
2014-01-20T12:24:48Z // Github, Apple
2014-01-20T12:24:48+0800 // Facebook
2014-01-20T12:24:48+12:00 // Google
2014-01-20T12:24:48.000Z
2014-01-20T12:24:48.000+0800
2014-01-20T12:24:48.000+12:00
*/
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";

NSDateFormatter *formatter2 = [NSDateFormatter new];
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter2.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";

blocks[20] = ^(NSString *string) { return [formatter dateFromString:string]; };
blocks[24] = ^(NSString *string) { return [formatter dateFromString:string]; };
blocks[24] = ^(NSString *string) { return [formatter dateFromString:string]?: [formatter2 dateFromString:string]; };
blocks[25] = ^(NSString *string) { return [formatter dateFromString:string]; };
blocks[28] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
blocks[29] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
}

{
/*
Fri Sep 04 00:12:21 +0800 2015 // Weibo, Twitter
Fri Sep 04 00:12:21.000 +0800 2015
*/
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";

NSDateFormatter *formatter2 = [NSDateFormatter new];
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
formatter2.dateFormat = @"EEE MMM dd HH:mm:ss.SSS Z yyyy";

blocks[30] = ^(NSString *string) { return [formatter dateFromString:string]; };
blocks[34] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
}
});
if (!string) return nil;
Expand Down
24 changes: 24 additions & 0 deletions YYModelTests/YYTestAutoTypeConvert.m
Expand Up @@ -320,6 +320,30 @@ - (void)testDate {
json = @"{\"v\" : \"Fri Sep 04 00:12:21 +0800 2015\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"2014-05-06 07:08:09.000\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"2014-05-06T07:08:09.000\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"2014-01-20T12:24:48.000Z\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"2014-01-20T12:24:48.000Z\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"2014-01-20T12:24:48.000+12:00\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

json = @"{\"v\" : \"Fri Sep 04 00:12:21.000 +0800 2015\"}";
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
XCTAssert([model.date isKindOfClass:[NSDate class]]);

model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSDate new]}];
XCTAssert([model.date isKindOfClass:[NSDate class]]);
Expand Down

0 comments on commit 323ffa7

Please sign in to comment.