xref: /netbsd-src/lib/libc/compat/gen/compat_timezone.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /*	$NetBSD: compat_timezone.c,v 1.5 2024/01/20 14:52:45 christos Exp $	*/
25b84b398Schristos 
35b84b398Schristos /*
45b84b398Schristos  * Copyright (c) 1987, 1993
55b84b398Schristos  *	The Regents of the University of California.  All rights reserved.
65b84b398Schristos  *
75b84b398Schristos  * Redistribution and use in source and binary forms, with or without
85b84b398Schristos  * modification, are permitted provided that the following conditions
95b84b398Schristos  * are met:
105b84b398Schristos  * 1. Redistributions of source code must retain the above copyright
115b84b398Schristos  *    notice, this list of conditions and the following disclaimer.
125b84b398Schristos  * 2. Redistributions in binary form must reproduce the above copyright
135b84b398Schristos  *    notice, this list of conditions and the following disclaimer in the
145b84b398Schristos  *    documentation and/or other materials provided with the distribution.
155b84b398Schristos  * 3. Neither the name of the University nor the names of its contributors
165b84b398Schristos  *    may be used to endorse or promote products derived from this software
175b84b398Schristos  *    without specific prior written permission.
185b84b398Schristos  *
195b84b398Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205b84b398Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215b84b398Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225b84b398Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235b84b398Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245b84b398Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255b84b398Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265b84b398Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275b84b398Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285b84b398Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295b84b398Schristos  * SUCH DAMAGE.
305b84b398Schristos  */
315b84b398Schristos 
325b84b398Schristos #include <sys/cdefs.h>
335b84b398Schristos #if defined(LIBC_SCCS) && !defined(lint)
345b84b398Schristos #if 0
355b84b398Schristos static char sccsid[] = "@(#)timezone.c	8.1 (Berkeley) 6/4/93";
365b84b398Schristos #else
37*ace5b9b5Schristos __RCSID("$NetBSD: compat_timezone.c,v 1.5 2024/01/20 14:52:45 christos Exp $");
385b84b398Schristos #endif
395b84b398Schristos #endif /* LIBC_SCCS and not lint */
405b84b398Schristos 
415b84b398Schristos #define __LIBC12_SOURCE__
42*ace5b9b5Schristos #define	__TIMEZONE_FUNCTION__
435b84b398Schristos 
445b84b398Schristos #include "namespace.h"
455b84b398Schristos #include <sys/types.h>
465b84b398Schristos #include <time.h>
475b84b398Schristos #include <compat/include/time.h>
485b84b398Schristos #include <stdio.h>
495b84b398Schristos #include <stdlib.h>
505b84b398Schristos #include <string.h>
515b84b398Schristos #include <tzfile.h>
525b84b398Schristos 
535b84b398Schristos __warn_references(timezone,
545b84b398Schristos      "warning: reference to compatibility timezone; include <time.h> to generate correct reference")
555b84b398Schristos 
565b84b398Schristos /*
575b84b398Schristos  * timezone --
585b84b398Schristos  *	The arguments are the number of minutes of time you are westward
595b84b398Schristos  *	from Greenwich and whether DST is in effect.  It returns a string
605b84b398Schristos  *	giving the name of the local timezone.  Should be replaced, in the
615b84b398Schristos  *	application code, by a call to localtime.
625b84b398Schristos  */
635b84b398Schristos 
645b84b398Schristos static char	czone[TZ_MAX_CHARS];		/* space for zone name */
655b84b398Schristos 
665b84b398Schristos char *
timezone(int zone,int dst)67461a86f9Schristos timezone(int zone, int dst)
685b84b398Schristos {
695b84b398Schristos 	char	*beg,
705b84b398Schristos 			*end;
715b84b398Schristos 
725b84b398Schristos 	if ((beg = getenv("TZNAME")) != NULL) {	/* set in environment */
735b84b398Schristos 		if ((end = strchr(beg, ',')) != NULL) {	/* "PST,PDT" */
745b84b398Schristos 			if (dst)
755b84b398Schristos 				return(++end);
765b84b398Schristos 			*end = '\0';
775b84b398Schristos 			(void)strlcpy(czone, beg, sizeof(czone));
785b84b398Schristos 			*end = ',';
795b84b398Schristos 			return(czone);
805b84b398Schristos 		}
815b84b398Schristos 		return(beg);
825b84b398Schristos 	}
835b84b398Schristos 	return(_tztab(zone,dst));	/* default: table or created zone */
845b84b398Schristos }
855b84b398Schristos 
865b84b398Schristos static const struct zone {
875b84b398Schristos 	int		offset;
8803256c6eSchristos 	const char	*stdzone;
8903256c6eSchristos 	const char	*dlzone;
905b84b398Schristos } zonetab[] = {
915b84b398Schristos 	{ -1*60,	"MET",	"MET DST" },	/* Middle European */
925b84b398Schristos 	{ -2*60,	"EET",	"EET DST" },	/* Eastern European */
935b84b398Schristos 	{ 4*60,		"AST",	"ADT" },	/* Atlantic */
945b84b398Schristos 	{ 5*60,		"EST",	"EDT" },	/* Eastern */
955b84b398Schristos 	{ 6*60,		"CST",	"CDT" },	/* Central */
965b84b398Schristos 	{ 7*60,		"MST",	"MDT" },	/* Mountain */
975b84b398Schristos 	{ 8*60,		"PST",	"PDT" },	/* Pacific */
985b84b398Schristos #ifdef notdef
995b84b398Schristos 	/* there's no way to distinguish this from WET */
1005b84b398Schristos 	{ 0,		"GMT",	0 },		/* Greenwich */
1015b84b398Schristos #endif
1025b84b398Schristos 	{ 0*60,		"WET",	"WET DST" },	/* Western European */
1035b84b398Schristos 	{ -10*60,	"EST",	"EST" },	/* Aust: Eastern */
1045b84b398Schristos      	{ -10*60+30,	"CST",	"CST" },	/* Aust: Central */
1055b84b398Schristos  	{ -8*60,	"WST",	0 },		/* Aust: Western */
1065b84b398Schristos 	{ -1,		NULL,	NULL }
1075b84b398Schristos };
1085b84b398Schristos 
1095b84b398Schristos /*
1105b84b398Schristos  * _tztab --
1115b84b398Schristos  *	check static tables or create a new zone name; broken out so that
1125b84b398Schristos  *	we can make a guess as to what the zone is if the standard tables
1135b84b398Schristos  *	aren't in place in /etc.  DO NOT USE THIS ROUTINE OUTSIDE OF THE
1145b84b398Schristos  *	STANDARD LIBRARY.
1155b84b398Schristos  */
1165b84b398Schristos char *
_tztab(int zone,int dst)1175688dfcfSmatt _tztab(int zone, int dst)
1185b84b398Schristos {
1195b84b398Schristos 	const struct zone	*zp;
1205b84b398Schristos 	char	sign;
1215b84b398Schristos 
1225b84b398Schristos 	for (zp = zonetab; zp->offset != -1;++zp)	/* static tables */
1235b84b398Schristos 		if (zp->offset == zone) {
1245b84b398Schristos 			if (dst && zp->dlzone)
12503256c6eSchristos 				return __UNCONST(zp->dlzone);
1265b84b398Schristos 			if (!dst && zp->stdzone)
12703256c6eSchristos 				return __UNCONST(zp->stdzone);
1285b84b398Schristos 		}
1295b84b398Schristos 
1305b84b398Schristos 	if (zone < 0) {					/* create one */
1315b84b398Schristos 		zone = -zone;
1325b84b398Schristos 		sign = '+';
1335b84b398Schristos 	}
1345b84b398Schristos 	else
1355b84b398Schristos 		sign = '-';
1365b84b398Schristos 	(void)snprintf(czone, TZ_MAX_CHARS, "GMT%c%d:%02d", sign, zone / 60,
1375b84b398Schristos 	     zone % 60);
1385b84b398Schristos 	return(czone);
1395b84b398Schristos }
140