0

My app ingests data from a web service (PHP) which provides dates in this format:

        endDate =             {
            date = "2020-09-30 16:16:08.000000";
            timezone = "-04:00";
            "timezone_type" = 1;
        };

This is the code I have been using to convert to NSDate, and it works as far as I can tell, in every test, but it fails on a few devices according to user reports and debug logs.

Note that the correct conversion of this date determines if content is unlocked in the app, so when it fails, customers contact us about it.

             NSDictionary* dateDict = [responseDict objectForKey:@"endDate"];
             NSString* strEndDate = [dateDict objectForKey:@"date"];
             NSString* strOffset = [dateDict objectForKey:@"timezone"];

             NSTimeInterval zoneSeconds = 0;
             NSRange rng = [strOffset rangeOfString:@":"];
             if (rng.location != NSNotFound && rng.location >= 1)
             {
                 NSString* hoursOnly = [strOffset substringToIndex:rng.location];   
                 NSInteger offsetValue = [hoursOnly integerValue];
                 zoneSeconds = (3600 * offsetValue);
             }

             NSDateFormatter* df = [[NSDateFormatter alloc] init];
             NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:zoneSeconds];
             [df setTimeZone:timeZone];
             [df setDateFormat:@"yyyy-MM-dd HH:mm:ss.000000"];

             NSDate* newEndDate = [df dateFromString:strEndDate];

However, debug logs from a few users show that the dateFromString call is failing and returning nil.

We have one user who has 2 iOS devices, and using the same account (same date) the app performs as expected on one of them, but fails on the other. Same Apple ID, both running iOS12. Debug logs show both devices received the same date from the server, yet one of them failed to convert the date from a string to NSDate.

My assumption so far is that there is some setting or configuration on the device(s) where this fails that is different. But I have fiddled with calendar and date settings all day, and cannot get this to fail. I know the user in question has both devices configured to the same time zone.

Is there a better, more correct way to do this date conversion which might be more robust?

1

2 Answers 2

0

When using an arbitrary date format it's highly recommended to set the locale of the date formatter to the fixed value en_US_POSIX.

Rather than calculating the seconds from GMT it might be more efficient to strip the milliseconds with regular expression, append the string time zone and use an appropriate date format.

This code uses more contemporary syntax to set date formatter properties with dot notation and dictionary literal key subscription

NSDictionary *dateDict = responseDict[@"endDate"];
NSString *strEndDate = dateDict[@"date"];
NSString *strTimeZone = dateDict[@"timezone"];
NSString *dateWithoutMilliseconds = [strEndDate stringByReplacingOccurrencesOfString:@"\\.\\d+" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, strEndDate.length)];
NSString *dateWithTimeZone = [NSString stringWithFormat:@"%@%@", dateWithoutMilliseconds, strTimeZone];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
df.dateFormat = @"yyyy-MM-dd HH:mm:ssZZZZZ"];
NSDate *newEndDate = [df dateFromString:dateWithTimeZone];
0

The question was actually similar to (What is the best way to deal with the NSDateFormatter locale "feechur"?) as was suggested originally, but it was this other question (NSDateFormatter fails to return a datetime for UK region with 12 hour clock set) which really made it click for me - its the UK region with the 12hour clock which causes the code to fail, but the dateFormatter was easily fixed by simply setting the locale to "un_US_POSIX" as suggested in the answer to that question (it was also suggested below by vadian - I did not try his code however). Thank you to everyone who contributed hints and leads!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.