xref: /illumos-gate/usr/src/lib/libc/port/gen/ctime_r.c (revision c559157643fef9f9afb0414e00a3579407ba3052)
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.
34  * The epoch is 0000 Jan 1 1970 GMT.
35  * The argument time is in seconds since then.
36  * The localtime(t) entry returns a pointer to an array
37  * containing
38  *  seconds (0-59)
39  *  minutes (0-59)
40  *  hours (0-23)
41  *  day of month (1-31)
42  *  month (0-11)
43  *  year-1970
44  *  weekday (0-6, Sun is 0)
45  *  day of the year
46  *  daylight savings flag
47  *
48  * The routine corrects for daylight saving
49  * time and will work in any time zone provided
50  * "timezone" is adjusted to the difference between
51  * Greenwich and local standard time (measured in seconds).
52  * In places like Michigan "daylight" must
53  * be initialized to 0 to prevent the conversion
54  * to daylight time.
55  * There is a table which accounts for the peculiarities
56  * undergone by daylight time in 1974-1975.
57  *
58  * The routine does not work
59  * in Saudi Arabia which runs on Solar time.
60  *
61  * asctime(tvec)
62  * where tvec is produced by localtime
63  * returns a ptr to a character string
64  * that has the ascii time in the form
65  *	Thu Jan 01 00:00:00 1970\n\0
66  *	01234567890123456789012345
67  *	0	  1	    2
68  *
69  * ctime(t) just calls localtime, then asctime.
70  *
71  * tzset() looks for an environment variable named
72  * TZ.
73  * If the variable is present, it will set the external
74  * variables "timezone", "altzone", "daylight", and "tzname"
75  * appropriately. It is called by localtime, and
76  * may also be called explicitly by the user.
77  */
78 
79 #include "lint.h"
80 #include <time.h>
81 #include <sys/types.h>
82 #include <errno.h>
83 #include <thread.h>
84 #include <synch.h>
85 #include <mtlib.h>
86 #include "libc.h"
87 
88 
89 /*
90  * POSIX.1c Draft-6 version of the function ctime_r.
91  * It was implemented by Solaris 2.3.
92  */
93 char *
94 ctime_r(const time_t *t, char *buffer, int buflen)
95 {
96 	struct tm res;
97 
98 	if (localtime_r(t, &res) == NULL)
99 		return (NULL);
100 
101 	if (asctime_r(&res, buffer, buflen) == NULL)
102 		return (NULL);
103 
104 	return (buffer);
105 }
106 
107 /*
108  * POSIX.1c standard version of the function ctime_r.
109  * User gets it via static ctime_r from the header file.
110  */
111 char *
112 __posix_ctime_r(const time_t *t, char *buffer)
113 {
114 	struct tm res;
115 
116 	if (localtime_r(t, &res) == NULL)
117 		return (NULL);
118 
119 	if (__posix_asctime_r(&res, buffer) == NULL)
120 		return (NULL);
121 
122 	return (buffer);
123 }
124