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