xref: /illumos-gate/usr/src/cmd/stat/common/timestamp.c (revision 26fd7700)
1*26fd7700SKrishnendu Sadhukhan - Sun Microsystems /*
2*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * CDDL HEADER START
3*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  *
4*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * The contents of this file are subject to the terms of the
5*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Common Development and Distribution License (the "License").
6*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * You may not use this file except in compliance with the License.
7*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  *
8*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * or http://www.opensolaris.org/os/licensing.
10*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * See the License for the specific language governing permissions
11*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * and limitations under the License.
12*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  *
13*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * When distributing Covered Code, include this CDDL HEADER in each
14*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * If applicable, add the following below this CDDL HEADER, with the
16*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * fields enclosed by brackets "[]" replaced with your own identifying
17*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * information: Portions Copyright [yyyy] [name of copyright owner]
18*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  *
19*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * CDDL HEADER END
20*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  */
21*26fd7700SKrishnendu Sadhukhan - Sun Microsystems /*
22*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Use is subject to license terms.
24*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  */
25*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
26*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
27*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
28*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <langinfo.h>
29*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
30*26fd7700SKrishnendu Sadhukhan - Sun Microsystems /*
31*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Print timestamp as decimal reprentation of time_t value (-T u was specified)
32*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * or in date(1) format (-T d was specified).
33*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  */
34*26fd7700SKrishnendu Sadhukhan - Sun Microsystems void
print_timestamp(uint_t timestamp_fmt)35*26fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(uint_t timestamp_fmt)
36*26fd7700SKrishnendu Sadhukhan - Sun Microsystems {
37*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	time_t t = time(NULL);
38*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	static char *fmt = NULL;
39*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
40*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	/* We only need to retrieve this once per invocation */
41*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	if (fmt == NULL)
42*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		fmt = nl_langinfo(_DATE_FMT);
43*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
44*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	if (timestamp_fmt == UDATE) {
45*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		(void) printf("%ld\n", t);
46*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	} else if (timestamp_fmt == DDATE) {
47*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		char dstr[64];
48*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		int len;
49*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
50*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		len = strftime(dstr, sizeof (dstr), fmt, localtime(&t));
51*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (len > 0)
52*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			(void) printf("%s\n", dstr);
53*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	}
54*26fd7700SKrishnendu Sadhukhan - Sun Microsystems }
55