1df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin/*
239bac055674d23770b9a724221b728e443196ea7Elliott Hughes * Print time_t and nanoseconds in symbolic format.
339bac055674d23770b9a724221b728e443196ea7Elliott Hughes *
439bac055674d23770b9a724221b728e443196ea7Elliott Hughes * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * All rights reserved.
6df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *
7df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * Redistribution and use in source and binary forms, with or without
8df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * modification, are permitted provided that the following conditions
9df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * are met:
10df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * 1. Redistributions of source code must retain the above copyright
11df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *    notice, this list of conditions and the following disclaimer.
12df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * 2. Redistributions in binary form must reproduce the above copyright
13df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *    notice, this list of conditions and the following disclaimer in the
14df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *    documentation and/or other materials provided with the distribution.
15df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * 3. The name of the author may not be used to endorse or promote products
16df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *    derived from this software without specific prior written permission.
17df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin *
18df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin */
29df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin
3039bac055674d23770b9a724221b728e443196ea7Elliott Hughes#include "tests.h"
31df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin#include <stdio.h>
3239bac055674d23770b9a724221b728e443196ea7Elliott Hughes#include <time.h>
33df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin
3439bac055674d23770b9a724221b728e443196ea7Elliott Hughesstatic void
3539bac055674d23770b9a724221b728e443196ea7Elliott Hughesprint_time_t_ex(const time_t t, const unsigned long long part_sec,
3639bac055674d23770b9a724221b728e443196ea7Elliott Hughes		const unsigned int max_part_sec, const int width,
3739bac055674d23770b9a724221b728e443196ea7Elliott Hughes		const int comment)
38df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin{
39df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin
4039bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if ((!t && !part_sec) || part_sec > max_part_sec)
4139bac055674d23770b9a724221b728e443196ea7Elliott Hughes		return;
4239bac055674d23770b9a724221b728e443196ea7Elliott Hughes
4339bac055674d23770b9a724221b728e443196ea7Elliott Hughes	const struct tm *const p = localtime(&t);
4439bac055674d23770b9a724221b728e443196ea7Elliott Hughes	char buf[256];
4539bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if (!p || !strftime(buf, sizeof(buf), "%FT%T", p))
4639bac055674d23770b9a724221b728e443196ea7Elliott Hughes		return;
4739bac055674d23770b9a724221b728e443196ea7Elliott Hughes
4839bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if (comment)
4939bac055674d23770b9a724221b728e443196ea7Elliott Hughes		fputs(" /* ", stdout);
5039bac055674d23770b9a724221b728e443196ea7Elliott Hughes
5139bac055674d23770b9a724221b728e443196ea7Elliott Hughes	fputs(buf, stdout);
5239bac055674d23770b9a724221b728e443196ea7Elliott Hughes
5339bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if (part_sec)
5439bac055674d23770b9a724221b728e443196ea7Elliott Hughes		printf(".%0*llu", width, part_sec);
5539bac055674d23770b9a724221b728e443196ea7Elliott Hughes
5639bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if (strftime(buf, sizeof(buf), "%z", p))
5739bac055674d23770b9a724221b728e443196ea7Elliott Hughes		fputs(buf, stdout);
5839bac055674d23770b9a724221b728e443196ea7Elliott Hughes
5939bac055674d23770b9a724221b728e443196ea7Elliott Hughes	if (comment)
6039bac055674d23770b9a724221b728e443196ea7Elliott Hughes		fputs(" */", stdout);
61df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin}
62df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin
63df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levinvoid
6439bac055674d23770b9a724221b728e443196ea7Elliott Hughesprint_time_t_nsec(const time_t t, const unsigned long long nsec, int comment)
65df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin{
6639bac055674d23770b9a724221b728e443196ea7Elliott Hughes	print_time_t_ex(t, nsec, 999999999, 9, comment);
6739bac055674d23770b9a724221b728e443196ea7Elliott Hughes}
68df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin
6939bac055674d23770b9a724221b728e443196ea7Elliott Hughesvoid
7039bac055674d23770b9a724221b728e443196ea7Elliott Hughesprint_time_t_usec(const time_t t, const unsigned long long usec, int comment)
7139bac055674d23770b9a724221b728e443196ea7Elliott Hughes{
7239bac055674d23770b9a724221b728e443196ea7Elliott Hughes	print_time_t_ex(t, usec, 999999, 6, comment);
73df7aa2b19e6f69c19fbe09180bf1ec4fb52e2615Dmitry V. Levin}
74