1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24eda14cbcSMatt Macy * Use is subject to license terms.
25eda14cbcSMatt Macy */
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy #include <stdio.h>
28eda14cbcSMatt Macy #include <time.h>
29eda14cbcSMatt Macy #include <langinfo.h>
30eda14cbcSMatt Macy #include "statcommon.h"
31eda14cbcSMatt Macy
32eda14cbcSMatt Macy #ifndef _DATE_FMT
33eda14cbcSMatt Macy #ifdef D_T_FMT
34eda14cbcSMatt Macy #define _DATE_FMT D_T_FMT
35eda14cbcSMatt Macy #else /* D_T_FMT */
36eda14cbcSMatt Macy #define _DATE_FMT "%+"
37eda14cbcSMatt Macy #endif /* !D_T_FMT */
38eda14cbcSMatt Macy #endif /* _DATE_FMT */
39eda14cbcSMatt Macy
40eda14cbcSMatt Macy /*
41eda14cbcSMatt Macy * Print timestamp as decimal reprentation of time_t value (-T u was specified)
42eda14cbcSMatt Macy * or in date(1) format (-T d was specified).
43eda14cbcSMatt Macy */
44eda14cbcSMatt Macy void
print_timestamp(uint_t timestamp_fmt)45eda14cbcSMatt Macy print_timestamp(uint_t timestamp_fmt)
46eda14cbcSMatt Macy {
47eda14cbcSMatt Macy time_t t = time(NULL);
48716fd348SMartin Matuska static const char *fmt = NULL;
49eda14cbcSMatt Macy
50eda14cbcSMatt Macy /* We only need to retrieve this once per invocation */
51eda14cbcSMatt Macy if (fmt == NULL)
52eda14cbcSMatt Macy fmt = nl_langinfo(_DATE_FMT);
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy if (timestamp_fmt == UDATE) {
55eda14cbcSMatt Macy (void) printf("%lld\n", (longlong_t)t);
56eda14cbcSMatt Macy } else if (timestamp_fmt == DDATE) {
57eda14cbcSMatt Macy char dstr[64];
58716fd348SMartin Matuska struct tm tm;
59eda14cbcSMatt Macy int len;
60eda14cbcSMatt Macy
61716fd348SMartin Matuska len = strftime(dstr, sizeof (dstr), fmt, localtime_r(&t, &tm));
62eda14cbcSMatt Macy if (len > 0)
63eda14cbcSMatt Macy (void) printf("%s\n", dstr);
64eda14cbcSMatt Macy }
65eda14cbcSMatt Macy }
66ce4dcb97SMartin Matuska
67ce4dcb97SMartin Matuska /*
68ce4dcb97SMartin Matuska * Return timestamp as decimal reprentation (in string) of time_t
69ce4dcb97SMartin Matuska * value (-T u was specified) or in date(1) format (-T d was specified).
70ce4dcb97SMartin Matuska */
71ce4dcb97SMartin Matuska void
get_timestamp(uint_t timestamp_fmt,char * buf,int len)72ce4dcb97SMartin Matuska get_timestamp(uint_t timestamp_fmt, char *buf, int len)
73ce4dcb97SMartin Matuska {
74ce4dcb97SMartin Matuska time_t t = time(NULL);
75ce4dcb97SMartin Matuska static const char *fmt = NULL;
76ce4dcb97SMartin Matuska
77ce4dcb97SMartin Matuska /* We only need to retrieve this once per invocation */
78ce4dcb97SMartin Matuska if (fmt == NULL)
79ce4dcb97SMartin Matuska fmt = nl_langinfo(_DATE_FMT);
80ce4dcb97SMartin Matuska
81ce4dcb97SMartin Matuska if (timestamp_fmt == UDATE) {
82ce4dcb97SMartin Matuska (void) snprintf(buf, len, "%lld", (longlong_t)t);
83ce4dcb97SMartin Matuska } else if (timestamp_fmt == DDATE) {
84ce4dcb97SMartin Matuska struct tm tm;
85ce4dcb97SMartin Matuska strftime(buf, len, fmt, localtime_r(&t, &tm));
86ce4dcb97SMartin Matuska }
87ce4dcb97SMartin Matuska }
88ce4dcb97SMartin Matuska
89ce4dcb97SMartin Matuska /*
90ce4dcb97SMartin Matuska * Format the provided time stamp to human readable format
91ce4dcb97SMartin Matuska */
92ce4dcb97SMartin Matuska void
format_timestamp(time_t t,char * buf,int len)93ce4dcb97SMartin Matuska format_timestamp(time_t t, char *buf, int len)
94ce4dcb97SMartin Matuska {
95ce4dcb97SMartin Matuska struct tm tm;
96ce4dcb97SMartin Matuska static const char *fmt = NULL;
97ce4dcb97SMartin Matuska
98ce4dcb97SMartin Matuska if (t == 0) {
99ce4dcb97SMartin Matuska snprintf(buf, len, "-");
100ce4dcb97SMartin Matuska return;
101ce4dcb97SMartin Matuska }
102ce4dcb97SMartin Matuska
103ce4dcb97SMartin Matuska /* We only need to retrieve this once per invocation */
104ce4dcb97SMartin Matuska if (fmt == NULL)
105ce4dcb97SMartin Matuska fmt = nl_langinfo(_DATE_FMT);
106ce4dcb97SMartin Matuska strftime(buf, len, fmt, localtime_r(&t, &tm));
107ce4dcb97SMartin Matuska }
108