Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current time milliseconds returns minus value for 32 bit in iOS

I need to get the current time from system in milliseconds. I'm using XCode 5.1.1 and I tried with the following,

long timePassed_ms = ([[NSDate date] timeIntervalSince1970] * 1000);

This is working fine with iPad Retina(64-bit) emulator. But when I'm running this on iPad(32 bit) emulator, it returns minus value.

Output

In 32bit iPad : -2147483648

In 64bit iPad : 1408416635774(This is the correct time)

Can anyone help with this?

Thanks in Advance!

like image 307
codebot Avatar asked Aug 19 '14 02:08

codebot


2 Answers

The interval for values of long type is:

–2,147,483,648 to 2,147,483,647

Try to use long long instead, which uses 8 bytes, and has the interval:

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

If you are getting the wrong results using long long, try to use double, but make sure you format the result properly in case you need to place it somewhere.

like image 132
ppalancica Avatar answered Oct 13 '22 22:10

ppalancica


long is only 4 bytes on the 32-bit iPad (max value 2147483647) so it's overflowing. You should be using long long or double instead (double is actually what timeIntervalSince1970 returns so it's probably the best choice):

double timePassed_ms = ([[NSDate date] timeIntervalSince1970] * 1000);

This answer has a good list of the available types and their sizes.

like image 41
Mike S Avatar answered Oct 13 '22 23:10

Mike S