In iOS, Why [[NSDate date]timeIntervalSince1970] is double internally? It should be long long though

557 views Asked by At

In iOS SDK, I observed that [[NSDate date]timeIntervalSince1970] returns NSTimeInterval which is double internally. Ideally shouldn't it be long long?

2

There are 2 answers

0
Krin-San On

From apple documentation:

NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.

NSTimeInterval represents a time dimension value which is not always a decimal number. So the NSTimeInterval must be fractional too.

In 64-bit systems CGFloat and long long has the same size – 8 bytes.

0
Sergey Kalinichenko On

Picking a representation for NSTimeInterval is a tradeoff between precision and ease of use.

Picking long long would require using a time unit other than second - say, a millisecond or a microsecond, and then providing functions or macros for extracting time units from the interval.

Cocoa designers went for ease of use, requiring that NSTimeInterval is always specified in seconds. This gives you sub-millisecond precision over a range of 10,000 years, which is good enough for most applications.