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