Question

I'm getting a curious issue when creating NSDates using dateWithTimeIntervalSince1970 on a simulator versus an iPhone device. I am simply translating an ISO8601 timestamp from an NSDate to milliseconds and then back to a NSDate.

NSDateFormatter *iso8601Formatter = [[NSDateFormatter alloc] init];
iso8601Formatter.dateFormat = TSMISO8601FormatString; // "yyyy-MM-dd'T'HH:mm:ssZZZZZ"

NSDate startDate = [iso8601Formatter dateFromString: @"2014-03-19T09:46:00-06:00"];
long startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;
startDate = [NSDate dateWithTimeIntervalSince1970:(startTimeMilliseconds / 1000)];


When I run this on the simulator, I get the same date back but when I run it on my iPhone, I get back this. I'm really confused as to why I'm getting back a wildly different day

1970-01-25 20:31:23 +0000  // iPhone device results

I have checked to make sure that my timezone, date, and time format are the same on both just to make sure that this isn't the issue. And both are running iOS 7.1. I have also attempted to set both the locale and timezone for the NSDateFormatter without any success.

Any help would be appreciated

Was it helpful?

Solution

This:

long startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;

Should Be:

double startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;

Here's why,

The maximum long can be found with LONG_MAX and it will log:

2147483647

Your current time interval is:

1395243960000.000000

Long can't go big enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top