6

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!

codebot
  • 2,540
  • 3
  • 38
  • 89

2 Answers2

4

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.

ppalancica
  • 4,236
  • 4
  • 27
  • 42
2

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.

Community
  • 1
  • 1
Mike S
  • 41,895
  • 11
  • 89
  • 84