xref: /illumos-gate/usr/src/lib/libc/port/gen/ctime_r.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * This routine converts time as follows.
327c478bd9Sstevel@tonic-gate  * The epoch is 0000 Jan 1 1970 GMT.
337c478bd9Sstevel@tonic-gate  * The argument time is in seconds since then.
347c478bd9Sstevel@tonic-gate  * The localtime(t) entry returns a pointer to an array
357c478bd9Sstevel@tonic-gate  * containing
367c478bd9Sstevel@tonic-gate  *  seconds (0-59)
377c478bd9Sstevel@tonic-gate  *  minutes (0-59)
387c478bd9Sstevel@tonic-gate  *  hours (0-23)
397c478bd9Sstevel@tonic-gate  *  day of month (1-31)
407c478bd9Sstevel@tonic-gate  *  month (0-11)
417c478bd9Sstevel@tonic-gate  *  year-1970
427c478bd9Sstevel@tonic-gate  *  weekday (0-6, Sun is 0)
437c478bd9Sstevel@tonic-gate  *  day of the year
447c478bd9Sstevel@tonic-gate  *  daylight savings flag
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The routine corrects for daylight saving
477c478bd9Sstevel@tonic-gate  * time and will work in any time zone provided
487c478bd9Sstevel@tonic-gate  * "timezone" is adjusted to the difference between
497c478bd9Sstevel@tonic-gate  * Greenwich and local standard time (measured in seconds).
507c478bd9Sstevel@tonic-gate  * In places like Michigan "daylight" must
517c478bd9Sstevel@tonic-gate  * be initialized to 0 to prevent the conversion
527c478bd9Sstevel@tonic-gate  * to daylight time.
537c478bd9Sstevel@tonic-gate  * There is a table which accounts for the peculiarities
547c478bd9Sstevel@tonic-gate  * undergone by daylight time in 1974-1975.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * The routine does not work
577c478bd9Sstevel@tonic-gate  * in Saudi Arabia which runs on Solar time.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * asctime(tvec)
607c478bd9Sstevel@tonic-gate  * where tvec is produced by localtime
617c478bd9Sstevel@tonic-gate  * returns a ptr to a character string
627c478bd9Sstevel@tonic-gate  * that has the ascii time in the form
637c478bd9Sstevel@tonic-gate  *	Thu Jan 01 00:00:00 1970\n\0
647c478bd9Sstevel@tonic-gate  *	01234567890123456789012345
657c478bd9Sstevel@tonic-gate  *	0	  1	    2
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * ctime(t) just calls localtime, then asctime.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * tzset() looks for an environment variable named
707c478bd9Sstevel@tonic-gate  * TZ.
717c478bd9Sstevel@tonic-gate  * If the variable is present, it will set the external
727c478bd9Sstevel@tonic-gate  * variables "timezone", "altzone", "daylight", and "tzname"
737c478bd9Sstevel@tonic-gate  * appropriately. It is called by localtime, and
747c478bd9Sstevel@tonic-gate  * may also be called explicitly by the user.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
77*7257d1b4Sraf #include "lint.h"
787c478bd9Sstevel@tonic-gate #include <time.h>
797c478bd9Sstevel@tonic-gate #include <sys/types.h>
807c478bd9Sstevel@tonic-gate #include <errno.h>
817c478bd9Sstevel@tonic-gate #include <thread.h>
827c478bd9Sstevel@tonic-gate #include <synch.h>
837c478bd9Sstevel@tonic-gate #include <mtlib.h>
847c478bd9Sstevel@tonic-gate #include "libc.h"
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function ctime_r.
897c478bd9Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate char *
ctime_r(const time_t * t,char * buffer,int buflen)927c478bd9Sstevel@tonic-gate ctime_r(const time_t *t, char *buffer, int buflen)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	struct tm res;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (localtime_r(t, &res) == NULL)
977c478bd9Sstevel@tonic-gate 		return (NULL);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (asctime_r(&res, buffer, buflen) == NULL)
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	return (buffer);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * POSIX.1c standard version of the function ctime_r.
1077c478bd9Sstevel@tonic-gate  * User gets it via static ctime_r from the header file.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate char *
__posix_ctime_r(const time_t * t,char * buffer)1107c478bd9Sstevel@tonic-gate __posix_ctime_r(const time_t *t, char *buffer)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	struct tm res;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (localtime_r(t, &res) == NULL)
1157c478bd9Sstevel@tonic-gate 		return (NULL);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (__posix_asctime_r(&res, buffer) == NULL)
1187c478bd9Sstevel@tonic-gate 		return (NULL);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (buffer);
1217c478bd9Sstevel@tonic-gate }
122