-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjavatime.c
48 lines (43 loc) · 995 Bytes
/
javatime.c
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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
uint64_t nanos(void) {
struct timespec tp;
if (0 != clock_gettime(CLOCK_MONOTONIC, &tp)) {
perror("clock_gettime");
exit(1);
}
return ((uint64_t)tp.tv_sec) * (1000 * 1000 * 1000) + (uint64_t)tp.tv_nsec;
}
uint64_t millis(void) {
struct timeval time;
if (0 != gettimeofday(&time, NULL)) {
perror("gettimeofday");
exit(1);
}
return ((uint64_t)time.tv_sec) * 1000 + ((uint64_t)time.tv_usec)/1000;
}
int main(int argc, char *argv[]) {
int opt;
uint64_t (*fn)(void) = nanos;
while ((opt = getopt(argc, argv, "mn")) != -1) {
switch (opt) {
case 'n':
fn = nanos;
break;
case 'm':
fn = millis;
break;
default:
break;
}
}
uint64_t time = fn();
char *msg = optind < argc ? argv[optind] : "prestart";
printf("STARTUPTIME %" PRIu64 " %s\n", time, msg);
return 0;
}