Re: Uptime to Date/time
Re: Uptime to Date/time
- Subject: Re: Uptime to Date/time
- From: Greg Herlihy <email@hidden>
- Date: Sun, 30 Oct 2005 22:22:08 -0800
- Thread-topic: Uptime to Date/time
Subtracting the number of milliseconds of uptime from the current system
time (gettimeofday) should give a pretty good idea when the machine was last
booted.
I am not certain, but I would expect that that time would be the same as the
the kernel's boot time. The time the kernal was last booted is available via
sysctl (both the command line utility and the library).
I have written a little program that obtains the boot time from sysctl. Note
that on my machine, at least, the boot time appears to stored only with one
second accuracy (that is, timval.usec is always 0).
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysctl.h>
int main()
{
int mib[2];
struct timeval boottime;
size_t len;
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
len = sizeof(struct timeval);
sysctl(mib, 2, &boottime, &len, NULL, 0);
printf("Boot time: %s\n", ctime(&boottime.tv_sec));
return 0;
}
Greg
On 10/30/05 8:36 AM, "David" <email@hidden> wrote:
> I am using Uptime and converting it to milliseconds computer has been
> up. This value is being used to time stamp data every couple
> seconds. How to I convert the milliseconds of uptime to an actual
> date and time down to milliseconds? Or more precisely how does one
> acquire the date/time the Uptime routine is basing its calculation on?
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden