CSLogItem timestamp format (NSTimeInterval since when?) - iphone

I'm logging CMAccelerometerData on the iphone. CMAcceleration is a subclass of CMLogItem, which defines a timestamp. The timestamp is very important to me, as I have to match CMAcceleration data to positions obtain via GPS. GPS gives me a NSDate object, which is good; but CMLogItem gives me a number that I cant relate to anything.
It's not UNIX time for sure. This morning (17/11/2010 at 11H17am, in Australia), it was at 4090 seconds.
Any ideas of what that could be?

Nevermind I found out, CMLogItem timestamp is a NSTimeInterval starting when the phone last boot up.

Related

NSDate date returns 2h less than it should

When i call [NSDate date] it returns 2 hours less than it should. I've checked my computer clock settings and they are all ok. I've checked settings in iPhone and time zone and clock are all OK. Even simulator shows correct time on top toolbar. But when I try to log current date it shows 2 hours less than it should. I ran out of ideas where to look.
All dates returned by [NSDate date] are in the GMT time zone.
When you use any NSDateFormatter, just set the time zone and it will print out the correct time.
Look carefully at the output of the NSLog() statement. You will see that the output always contains the timezone using standard UTC. Therefore, the date is actually correct, taking into account the timezone.
I found my solution here: How to convert time to the time zone of the iPhone device?
So if you ever want to change [NSDate date] to point to local time just use the code provided in the link above. And dont forget to change timeZoneWithAbbreviation on sourceTimeZone from EST to GMT (because NSDate is always in GMT)
Once again thanks everyone for helping out..

How can I create a count up timer with the format seconds:milliseconds?

I'm diving into iOS development and I'm trying to create a count up timer in one of my views. I have the NSTimer code figured out to call a selector once every 0.04 seconds that updates the UILabel. Where I'm having trouble is with the formatting of the current time (starting initially at 00:00). I figured the best way to do this was using the NSDate class, and related classes (NSDateFormatter, NSDateComponents, etc.), but the manipulating of the dates and formats is really confusing me and the code is getting unwieldy quickly. I was hoping there are some SO users that are comfortable using the NSDate class that could help me figure out a simple way to calculate the current time for a count up timer and convert it to an NString with the format 'seconds:milliseconds'.
I'd be happy to post my initial attempt at the NSDate code if requested, but I won't initially because it's really of no use and embarrassing :)
If you just want to display time elapsed since you started your timer you can store starting date somewhere (say, startDate variable) and calculate time interval using current date. Something like the following should work:
NSTimeInterval passed = [[NSDate date] timeIntervalSinceDate: startDate];
double intPart;
double fract = modf(passed, &intPart);
label.text = [NSString stringWithFormat:#"%d:%.2f", (int)intPart, fract];

Is there an objective-c/iPhone version of currentTimeMillis() from Java?

I need to time some events in the app I'm working on. In Java i used to be able to call currentTimeMillis() but there doesnt seem to be a version in Objective-c. Is there a way to check there current time without creating a NSDate and then parsing this object everytime i need this information?
Thanks
-Code
[[NSDate date] timeIntervalSince1970] * 1000 returns a the same value as currentTimeMillis()
CFAbsoluteTimeGetCurrent() and [NSDate timeIntervalSinceReferenceDate] both return a double starting at Jan 1 2001 00:00:00 GMT.
There's also CFAbsoluteTimeGetCurrent(), which will give you the time in double-precision seconds.
To get very cheap very precise time, you can use gettimeofday(), which is a C Function of the BSD kernel. Please read the man page for full details, but here's an simple example:
struct timeval t;
gettimeofday(&t, NULL);
long msec = t.tv_sec * 1000 + t.tv_usec / 1000;
Instead gettimeofday() you could also use [NSDate timeIntervalSinceReferenceDate] (the class method) and do your calculations with that. But they have the same problem: they operate on "wall clock time". That means your measurement can be off if leap seconds are added while your test is running or at the transition between daylight saving time.
You can use the Mach system call mach_absolute_time() on OS X and iOS. With the information returned by mach_timebase_info() this can be converted to nanoseconds.
The correct answer is [[NSDate date] timeIntervalSince1970]; this will give you current timestamp in milliseconds.
The answer given by #Noah Witherspoon returns current date but the year is not the current matching year.

Objective C: Compare timeInMillis with current time

In my iPhone application, I need to calculate the time difference between the time a message was created on the server, and the time my phone received it.
The server (Java) puts in a number returned by System.currentTimeMillis() as metadata along with the message.
How do I compare this number with the current time on the device? Could not find a suitable NSDate method to do this comparison.
Thanks in advance!
You might take a look at this SO answer and the -timeIntervalSinceDate: method.
You can use (NUInteger) ([[NSDate date] timeIntervalSince1970] * 1000)

Is it possible to get the atomic clock timestamp from the iphone GPS?

I'm looking for a reliable way to get the time. It can't be tampered with and it needs to work offline. So no internet time , no user time setup in settings and no BSD uptime time since last reboot. I was wondering since GPS works using atomic clock, whether I could access that information.
Thank you
This works to get the GPS time:
#import <CoreLocation/CoreLocation.h>
CLLocation* gps = [[CLLocation alloc]
initWithLatitude:(CLLocationDegrees) 0.0
longitude:(CLLocationDegrees) 0.0];
NSDate* now = gps.timestamp;
It doesn't seem to be tamper-proof though.
I tried this code on an iPhone 4 in airplane mode (iOS 6.1), and even then it gives a time all right. But unfortunately this time seems to change with the system clock. Ugh.
Funny thing that I found (still in airplane mode) is that if you tamper with the system clock (after turning to off Time & Date's Set Automatically), and then turn Set Automatically back to on, the machine restores the real (original) time without a hitch. this works even after cycling the phone's power. So it seems that there is something like a tamper-proof time the device maintains internally. But how to access this?
P.S. A discussion of this from 2010. The author of the penultimate comment tried this in a fallout shelter: so it's clear the phone is not getting the pristine time from any external source.
Addendum, July 2013
Found a few more posts (here, here and here) about another kind of time measure: system kernel boot time. It's accessed through a call something like this: sysctlbyname("kern.boottime", &boottime, &size, NULL, 0);. Unfortunately it too changes with the user-adjusted data and time, even without reboot. Another function gettimeofday() is similarly dependent on user-defined time.
NSDate and it's CF counterpart are all based on the user controllable time, and thereby aren't tamper proof.
As far as I know, there is no open API for either GPS time or carrier time directly. However, you can check the mach_absolute_time to get untampered time since last boot up, and perhaps use it to at least be aware of how much time has passed since the app has been awoken (without having the potential for that time to be tampered with while the app is running).
mach_absolute_time depends on the processor of the device. It returns ticks since the device was last rebooted (otherwise known as uptime). In order to get it in a human readable form, you have to modify it by the result from mach_timebase_info (a ratio), which will return billionth of seconds (or nanoseconds). To make this more usable I use a function like the one below:
#include <mach/mach_time.h>
int getUptimeInMilliseconds()
{
static const int64_t kOneMillion = 1000 * 1000;
static mach_timebase_info_data_t s_timebase_info;
if (s_timebase_info.denom == 0) {
(void) mach_timebase_info(&s_timebase_info);
}
// mach_absolute_time() returns billionth of seconds,
// so divide by one million to get milliseconds
return (int)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
}
Even if you can get hold of the time from GPS you should be aware that GPS time is not quite the same as UTC. The GPS receiver in the iPhone might take care of that for you though.
This gets you the current date and time:
NSDate *now = [NSDate date];
This will be as reliable as you can get. The internal clock on the iPhone will be updated when it can get access to an NTP server. If the phone uses GPS as a time sync source it'll also be used to update the same system-wide clock which is accessible via the above method.
The CoreFoundation equivalent is something like:
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
Which returns the CoreFoundation equivalent of the normal UNIX seconds-since-epoch timestamp.
The gold standard of timekeeping are the various government time observatories in the U.S. and worldwide. They provide Atomic time. That is used world wide. Apple should be using that. If the want to sync w/ the cell towers, there should be an Alternate internal time. If the tower or GPS system malfunctions all are left with incorrect time.