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