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