forked from per-erik-nordbo/ams-han-port
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm2.c
36 lines (28 loc) · 763 Bytes
/
tm2.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
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
void print_current_time_with_ms (void);
int main()
{
print_current_time_with_ms();
return 0;
}
void print_current_time_with_ms (void)
{
long ms; // Milliseconds
time_t s; // Seconds
struct timespec spec;
char buf[100], buf2[100];
clock_gettime(CLOCK_REALTIME, &spec); /* OR: CLOCK_MONOTIC */
s = spec.tv_sec;
ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
printf("64 bit Current time: %"PRIdMAX".%03ld seconds since the Epoch\n", (intmax_t)s, ms);
strftime(buf, 20, "%Y-%m-%d %H:%M:%S", localtime(&s));
ms = 1;
sprintf(buf2, ".%03ld", ms);
strcat(buf, buf2);
printf("%s\n", buf);
return;
}