2 Generic time-keeping functions and Go time system related functions. All times
13 #include <mach/clock.h>
14 #include <mach/mach.h>
22 Returns a current time mark with millisecond precision. Will be monotonic if
23 supported by the system. Is thread-safe.
24 RETURNS time in milliseconds
26 u64
current_time_in_millis() {
35 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK
, &cclock
);
36 clock_get_time(cclock
, &mts
);
37 mach_port_deallocate(mach_task_self(), cclock
);
38 ts
.tv_sec
= mts
.tv_sec
;
39 ts
.tv_nsec
= mts
.tv_nsec
;
45 /* POSIX.1 conforming */
46 #ifdef _POSIX_MONOTONIC_CLOCK
47 clock_gettime(CLOCK_MONOTONIC
, &ts
);
49 clock_gettime(CLOCK_REALTIME
, &ts
);
53 u64 ret
= ts
.tv_sec
* 1000;
54 ret
+= ts
.tv_nsec
/ 1000000;
59 Returns the current nanoseconds count from the system clock. Is not monotonic.
62 u64
current_nanoseconds() {
71 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK
, &cclock
);
72 clock_get_time(cclock
, &mts
);
73 mach_port_deallocate(mach_task_self(), cclock
);
74 ts
.tv_sec
= mts
.tv_sec
;
75 ts
.tv_nsec
= mts
.tv_nsec
;
81 /* POSIX.1 conforming */
82 #ifdef _POSIX_MONOTONIC_CLOCK
83 clock_gettime(CLOCK_MONOTONIC
, &ts
);
85 clock_gettime(CLOCK_REALTIME
, &ts
);
93 Produces a textual timestamp based on the local timezone and system time.
98 time_t t
= time(NULL
);
99 struct tm tm
= *localtime(&t
);
101 snprintf(buffer
, MAX_PAGE_SIZ
, "%02u:%02u:%02u", tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);