Skip to content

Commit

Permalink
Merge pull request #536 from 3lvis/support-for-deciseconds
Browse files Browse the repository at this point in the history
Add support for deciseconds
  • Loading branch information
3lvis authored Sep 17, 2018
2 parents 0f39cb8 + 7d90f22 commit c8ee970
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Source/DateParser/NSDate+PropertyMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ + (NSDate *)dateFromISO8601String:(NSString *)dateString {

char currentString[25] = "";
BOOL hasTimezone = NO;
BOOL hasDeciseconds = NO;
BOOL hasCentiseconds = NO;
BOOL hasMiliseconds = NO;
BOOL hasMicroseconds = NO;
Expand Down Expand Up @@ -106,6 +107,15 @@ + (NSDate *)dateFromISO8601String:(NSString *)dateString {
hasCentiseconds = YES;
}

// Copy all the date excluding the miliseconds and the Z.
// Current date: 2017-11-02T17:27:52.2Z
// Will become: 2014-03-30T09:13:00
// Unit test N
else if (originalLength == 22 && originalString[originalLength - 1] == 'Z') {
strncpy(currentString, originalString, 19);
hasDeciseconds = YES;
}

// Copy all the date excluding the miliseconds and the timezone also set `hasTimezone` to YES.
// Current date: 2015-06-23T12:40:08.000+02:00
// Will become: 2015-06-23T12:40:08
Expand Down Expand Up @@ -195,9 +205,15 @@ + (NSDate *)dateFromISO8601String:(NSString *)dateString {
time_t timeStruct = mktime(&tm);
double time = (double)timeStruct;

if (hasCentiseconds || hasMiliseconds || hasMicroseconds) {
if (hasDeciseconds || hasCentiseconds || hasMiliseconds || hasMicroseconds) {
NSString *trimmedDate = [dateString substringFromIndex:@"2015-09-10T00:00:00.".length];

if (hasDeciseconds) {
NSString *centisecondsString = [trimmedDate substringToIndex:@"0".length];
double centiseconds = centisecondsString.doubleValue / 10.0;
time += centiseconds;
}

if (hasCentiseconds) {
NSString *centisecondsString = [trimmedDate substringToIndex:@"00".length];
double centiseconds = centisecondsString.doubleValue / 100.0;
Expand Down
7 changes: 7 additions & 0 deletions Tests/DateParser/DateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class DateTests: XCTestCase {
XCTAssertNotNil(resultDate)
XCTAssertEqual(date, resultDate)
}

func testDateN() {
let date = Date.dateWithHourAndTimeZoneString(dateString: "2017-11-02T17:27:52.200")
let resultDate = NSDate(fromDateString: "2017-11-02T17:27:52.2Z")! as Date
XCTAssertNotNil(resultDate)
XCTAssertEqual(date, resultDate)
}
}

class TimestampDateTests: XCTestCase {
Expand Down

0 comments on commit c8ee970

Please sign in to comment.