xref: /illumos-gate/usr/src/lib/libc/port/gen/cftime.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.15	*/
32 
33 /*
34  * This routine converts time as follows.  The epoch is 0000  Jan  1
35  * 1970  GMT.   The  argument  time  is  in seconds since then.  The
36  * localtime(t) entry returns a pointer to an array containing:
37  *
38  *		  seconds (0-59)
39  *		  minutes (0-59)
40  *		  hours (0-23)
41  *		  day of month (1-31)
42  *		  month (0-11)
43  *		  year
44  *		  weekday (0-6, Sun is 0)
45  *		  day of the year
46  *		  daylight savings flag
47  *
48  * The routine corrects for daylight saving time and  will  work  in
49  * any  time  zone provided "timezone" is adjusted to the difference
50  * between Greenwich and local standard time (measured in seconds).
51  *
52  *	 ascftime(buf, format, t)	-> where t is produced by localtime
53  *				           and returns a ptr to a character
54  *				           string that has the ascii time in
55  *				           the format specified by the format
56  *				           argument (see date(1) for format
57  *				           syntax).
58  *
59  *	 cftime(buf, format, t) 	-> just calls ascftime.
60  *
61  *
62  *
63  */
64 
65 #pragma weak ascftime = _ascftime
66 #pragma weak cftime = _cftime
67 
68 #include	"synonyms.h"
69 #include	<mtlib.h>
70 #include	<stddef.h>
71 #include	<time.h>
72 #include	<limits.h>
73 #include	<stdlib.h>
74 #include	<thread.h>
75 #include	<synch.h>
76 
77 int
78 cftime(char *buf, char *format, const time_t *t)
79 {
80 	struct tm res;
81 	struct tm *p;
82 
83 	p = localtime_r(t, &res);
84 	if (p == NULL) {
85 		*buf = '\0';
86 		return (0);
87 	}
88 	/* LINTED do not use ascftime() */
89 	return (ascftime(buf, format, p));
90 }
91 
92 int
93 ascftime(char *buf, const char *format, const struct tm *tm)
94 {
95 	/* Set format string, if not already set */
96 	if (format == NULL || *format == '\0')
97 		if (((format = getenv("CFTIME")) == 0) || *format == 0)
98 			format =  "%C";
99 
100 	return ((int)strftime(buf, LONG_MAX, format, tm));
101 }
102