1 /*
2 * Print time_t and nanoseconds in symbolic format.
3 *
4 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2017-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include <stdio.h>
13 #include <time.h>
14
15 static void
16 print_time_t_ex(const time_t t, const unsigned long long part_sec,
17 const unsigned int max_part_sec, const int width,
18 const int comment)
19 {
20
21 if ((!t && !part_sec) || part_sec > max_part_sec)
22 return;
23
24 const struct tm *const p = localtime(&t);
25 char buf[256];
26 if (!p || !strftime(buf, sizeof(buf), "%FT%T", p))
27 return;
28
29 if (comment)
30 fputs(" /* ", stdout);
31
32 fputs(buf, stdout);
33
34 if (part_sec)
35 printf(".%0*llu", width, part_sec);
36
37 if (strftime(buf, sizeof(buf), "%z", p))
38 fputs(buf, stdout);
39
40 if (comment)
41 fputs(" */", stdout);
42 }
43
44 void
45 print_time_t_nsec(const time_t t, const unsigned long long nsec, int comment)
46 {
47 print_time_t_ex(t, nsec, 999999999, 9, comment);
48 }
49
50 void
51 print_time_t_usec(const time_t t, const unsigned long long usec, int comment)
52 {
53 print_time_t_ex(t, usec, 999999, 6, comment);
54 }