The current millisecond time returns a minus value for 32 bits in iOS

I need to get the current time from the system in milliseconds. I am using Xcode 5.1.1 and I tried the following:

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

This works great with the iPad Retina(64-bit) emulator iPad Retina(64-bit) . But when I run this on an iPad(32 bit) emulator iPad(32 bit) , it returns a minus value.

Exit

In 32bit iPad : -2147483648

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

Can anyone help with this?

Thank Advance

+5
source share
2 answers

Interval for long type values:

-2,147,483,648 to 2,147,483,647

Try using long long instead, which uses 8 bytes and has an interval:

-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807

If you get the wrong results using a long time, try using double, but make sure you format the result correctly if you need to place it somewhere.

+3
source

long is only 4 bytes on a 32-bit iPad (maximum value 2147483647 ), so it overflows. Instead, you should use long long or double ( double actually returns timeIntervalSince1970 , so this is probably the best choice):

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

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

+2
source

Source: https://habr.com/ru/post/1200637/


All Articles