Skip to content

Commit 323ffa7

Browse files
committed
Merge pull request #78 from Brok3nHalo/master
Added millisecond support to date parsing
2 parents 9af57fc + a86d88c commit 323ffa7

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

Framework/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0.2</string>
18+
<string>1.0.3</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

YYModel.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = 'YYModel'
33
s.summary = 'High performance model framework for iOS/OSX.'
4-
s.version = '1.0.2'
4+
s.version = '1.0.3'
55
s.license = { :type => 'MIT', :file => 'LICENSE' }
66
s.authors = { 'ibireme' => '[email protected]' }
77
s.social_media_url = 'http://blog.ibireme.com'

YYModel/NSObject+YYModel.m

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
134134
/// Parse string to date.
135135
static force_inline NSDate *YYNSDateFromString(__unsafe_unretained NSString *string) {
136136
typedef NSDate* (^YYNSDateParseBlock)(NSString *string);
137-
#define kParserNum 32
137+
#define kParserNum 34
138138
static YYNSDateParseBlock blocks[kParserNum + 1] = {0};
139139
static dispatch_once_t onceToken;
140140
dispatch_once(&onceToken, ^{
@@ -153,6 +153,8 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
153153
/*
154154
2014-01-20 12:24:48
155155
2014-01-20T12:24:48 // Google
156+
2014-01-20 12:24:48.000
157+
2014-01-20T12:24:48.000
156158
*/
157159
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
158160
formatter1.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
@@ -163,6 +165,16 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
163165
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
164166
formatter2.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
165167
formatter2.dateFormat = @"yyyy-MM-dd HH:mm:ss";
168+
169+
NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
170+
formatter3.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
171+
formatter3.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
172+
formatter3.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";
173+
174+
NSDateFormatter *formatter4 = [[NSDateFormatter alloc] init];
175+
formatter4.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
176+
formatter4.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
177+
formatter4.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
166178

167179
blocks[19] = ^(NSString *string) {
168180
if ([string characterAtIndex:10] == 'T') {
@@ -171,30 +183,55 @@ static force_inline BOOL YYEncodingTypeIsCNumber(YYEncodingType type) {
171183
return [formatter2 dateFromString:string];
172184
}
173185
};
186+
187+
blocks[23] = ^(NSString *string) {
188+
if ([string characterAtIndex:10] == 'T') {
189+
return [formatter3 dateFromString:string];
190+
} else {
191+
return [formatter4 dateFromString:string];
192+
}
193+
};
174194
}
175195

176196
{
177197
/*
178198
2014-01-20T12:24:48Z // Github, Apple
179199
2014-01-20T12:24:48+0800 // Facebook
180200
2014-01-20T12:24:48+12:00 // Google
201+
2014-01-20T12:24:48.000Z
202+
2014-01-20T12:24:48.000+0800
203+
2014-01-20T12:24:48.000+12:00
181204
*/
182205
NSDateFormatter *formatter = [NSDateFormatter new];
183206
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
184207
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
208+
209+
NSDateFormatter *formatter2 = [NSDateFormatter new];
210+
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
211+
formatter2.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
212+
185213
blocks[20] = ^(NSString *string) { return [formatter dateFromString:string]; };
186-
blocks[24] = ^(NSString *string) { return [formatter dateFromString:string]; };
214+
blocks[24] = ^(NSString *string) { return [formatter dateFromString:string]?: [formatter2 dateFromString:string]; };
187215
blocks[25] = ^(NSString *string) { return [formatter dateFromString:string]; };
216+
blocks[28] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
217+
blocks[29] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
188218
}
189219

190220
{
191221
/*
192222
Fri Sep 04 00:12:21 +0800 2015 // Weibo, Twitter
223+
Fri Sep 04 00:12:21.000 +0800 2015
193224
*/
194225
NSDateFormatter *formatter = [NSDateFormatter new];
195226
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
196227
formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
228+
229+
NSDateFormatter *formatter2 = [NSDateFormatter new];
230+
formatter2.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
231+
formatter2.dateFormat = @"EEE MMM dd HH:mm:ss.SSS Z yyyy";
232+
197233
blocks[30] = ^(NSString *string) { return [formatter dateFromString:string]; };
234+
blocks[34] = ^(NSString *string) { return [formatter2 dateFromString:string]; };
198235
}
199236
});
200237
if (!string) return nil;

YYModelTests/YYTestAutoTypeConvert.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,30 @@ - (void)testDate {
320320
json = @"{\"v\" : \"Fri Sep 04 00:12:21 +0800 2015\"}";
321321
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
322322
XCTAssert([model.date isKindOfClass:[NSDate class]]);
323+
324+
json = @"{\"v\" : \"2014-05-06 07:08:09.000\"}";
325+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
326+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
327+
328+
json = @"{\"v\" : \"2014-05-06T07:08:09.000\"}";
329+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
330+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
331+
332+
json = @"{\"v\" : \"2014-01-20T12:24:48.000Z\"}";
333+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
334+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
335+
336+
json = @"{\"v\" : \"2014-01-20T12:24:48.000Z\"}";
337+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
338+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
339+
340+
json = @"{\"v\" : \"2014-01-20T12:24:48.000+12:00\"}";
341+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
342+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
343+
344+
json = @"{\"v\" : \"Fri Sep 04 00:12:21.000 +0800 2015\"}";
345+
model = [YYTestAutoTypeModel yy_modelWithJSON:json];
346+
XCTAssert([model.date isKindOfClass:[NSDate class]]);
323347

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

0 commit comments

Comments
 (0)