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