1/*
2 * Copyright (c) 2023 Darren Tucker <dtucker@openssh.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $OpenBSD: timestamp.c,v 1.1 2023/03/01 09:29:32 dtucker Exp $ */
18
19/*
20 * Print a microsecond-granularity timestamp to stdout in an ISO8601-ish
21 * format, which we can then use as the first component of the log file
22 * so that they'll sort into chronological order.
23 */
24
25#include <sys/time.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <time.h>
30
31int
32main(void)
33{
34	struct timeval tv;
35	struct tm *tm;
36	char buf[1024];
37
38	if (gettimeofday(&tv, NULL) != 0)
39		exit(1);
40	if ((tm = localtime(&tv.tv_sec)) == NULL)
41		exit(2);
42	if (strftime(buf, sizeof buf, "%Y%m%dT%H%M%S", tm) <= 0)
43		exit(3);
44	printf("%s.%06d\n", buf, (int)tv.tv_usec);
45	exit(0);
46}
47