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