1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include "mach_date.h"
#include <stdio.h>
#include <sys/time.h>
#include <mach/mach.h>
#include <mach/clock.h>
static void format(mach_timespec_t *mts)
{
char buffer[128] ;
time_t clock = mts->tv_sec ;
struct tm *lt = localtime(&clock);
strftime(buffer, 127, "%Y-%m-%d %H:%M:%S",lt);
sprintf(buffer+strlen(buffer),".%d",mts->tv_nsec) ;
printf("%s\n",buffer) ;
}
int main(int argc, char **argv)
{
host_name_port_t myself = mach_host_self() ;
kern_return_t kr ;
clock_serv_t clock_serv ;
clock_id_t clock_id = CALENDAR_CLOCK ;
mach_timespec_t timespec ;
kr = host_get_clock_service(myself, clock_id, &clock_serv) ;
if (kr != KERN_SUCCESS) {
fprintf(stderr,"Fails to get clock service CALENDAR (%d)\n", kr) ;
mach_port_deallocate(mach_task_self(), myself) ;
return 1 ;
}
kr = clock_get_time(clock_serv, ×pec) ;
if (kr != KERN_SUCCESS) {
fprintf(stderr,"Fails to get time of service CALENDAR (%d)\n", kr) ;
mach_port_deallocate(mach_task_self(), myself) ;
mach_port_deallocate(mach_task_self(), clock_serv) ;
return 1 ;
}
format(×pec) ;
mach_port_deallocate(mach_task_self(), myself) ;
mach_port_deallocate(mach_task_self(), clock_serv) ;
return 0 ;
} |
Partager