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