Files
amiga-tz/localtime.c
T

382 lines
8.1 KiB
C
Raw Normal View History

1986-01-13 17:21:20 -05:00
#
/*LINTLIBRARY*/
1986-12-02 19:31:05 -05:00
#include "sys/param.h"
1986-02-15 15:23:29 -05:00
#include "tzfile.h"
1986-01-13 17:21:20 -05:00
#include "time.h"
1986-12-25 18:27:17 -05:00
#ifndef lint
#ifndef NOID
1986-01-13 17:21:20 -05:00
static char sccsid[] = "%W%";
1986-12-25 18:27:17 -05:00
#endif /* !NOID */
#endif /* !lint */
1986-01-13 17:21:20 -05:00
1986-03-02 22:03:44 -05:00
#ifndef TRUE
1986-03-04 10:43:24 -05:00
#define TRUE 1
#define FALSE 0
1986-12-26 15:22:05 -05:00
#endif /* !TRUE */
1986-03-02 22:03:44 -05:00
1986-03-04 09:49:44 -05:00
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
1986-12-26 15:22:05 -05:00
#endif /* !MAXPATHLEN */
1986-03-04 09:49:44 -05:00
1986-11-15 20:20:40 -05:00
extern char * getenv();
1986-12-31 14:23:49 -05:00
exter struct tm * gmtime();
1986-01-15 08:27:06 -05:00
extern char * strcpy();
extern char * strcat();
1986-11-15 20:20:40 -05:00
struct ttinfo { /* time type information */
long tt_gmtoff; /* GMT offset in seconds */
int tt_isdst; /* used to set tm_isdst */
int tt_abbrind; /* abbreviation list index */
};
1986-01-13 17:21:20 -05:00
1986-07-27 12:35:10 -04:00
struct state {
1986-11-15 20:20:40 -05:00
int timecnt;
int typecnt;
int charcnt;
1986-07-27 12:35:10 -04:00
long ats[TZ_MAX_TIMES];
unsigned char types[TZ_MAX_TIMES];
struct ttinfo ttis[TZ_MAX_TYPES];
char chars[TZ_MAX_CHARS + 1];
};
1986-03-04 09:49:44 -05:00
1986-07-27 12:35:10 -04:00
static struct state s;
1986-03-04 09:49:44 -05:00
1986-12-16 15:26:16 -05:00
static int tz_is_set;
1986-01-13 17:21:20 -05:00
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-02 19:31:05 -05:00
long timezone = 0;
int daylight = 0;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-12-26 22:41:00 -05:00
char * tzname[2] = {
"GMT",
"GMT"
};
1986-11-21 12:37:36 -05:00
1986-12-26 15:22:05 -05:00
#ifdef TZA_COMPAT
1986-11-21 12:37:36 -05:00
char * tz_abbr; /* compatibility w/older versions */
1986-12-26 15:22:05 -05:00
#endif /* TZA_COMPAT */
1986-01-16 08:50:51 -05:00
static long
1986-11-15 20:20:40 -05:00
detzcode(codep)
char * codep;
{
1986-11-15 20:20:40 -05:00
register long result;
register int i;
result = 0;
for (i = 0; i < 4; ++i)
result = (result << 8) | (codep[i] & 0xff);
return result;
}
1986-03-04 09:49:44 -05:00
1986-03-01 17:07:13 -05:00
static
1986-12-04 20:32:58 -05:00
tzload(name)
register char * name;
1986-01-13 17:21:20 -05:00
{
1986-03-02 22:03:44 -05:00
register int i;
1986-08-28 10:05:22 -04:00
register int fid;
1986-01-13 17:21:20 -05:00
1986-11-21 12:59:20 -05:00
if (name == 0 && (name = TZDEFAULT) == 0)
1986-03-04 09:49:44 -05:00
return -1;
1986-07-27 12:35:10 -04:00
{
1986-08-28 10:05:22 -04:00
register char * p;
register int doaccess;
char fullname[MAXPATHLEN];
1986-07-27 12:35:10 -04:00
1986-11-21 12:59:20 -05:00
doaccess = name[0] == '/';
1986-07-27 12:35:10 -04:00
if (!doaccess) {
if ((p = TZDIR) == 0)
return -1;
1986-11-21 12:59:20 -05:00
if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
1986-07-27 12:35:10 -04:00
return -1;
(void) strcpy(fullname, p);
(void) strcat(fullname, "/");
1986-11-21 12:59:20 -05:00
(void) strcat(fullname, name);
1986-07-27 12:35:10 -04:00
/*
** Set doaccess if '.' (as in "../") shows up in name.
*/
1986-11-21 12:59:20 -05:00
while (*name != '\0')
if (*name++ == '.')
1986-07-27 12:35:10 -04:00
doaccess = TRUE;
1986-11-21 12:59:20 -05:00
name = fullname;
1986-07-27 12:35:10 -04:00
}
1986-11-21 12:59:20 -05:00
if (doaccess && access(name, 4) != 0)
1986-03-04 09:49:44 -05:00
return -1;
1986-11-21 12:59:20 -05:00
if ((fid = open(name, 0)) == -1)
1986-03-04 09:49:44 -05:00
return -1;
1986-01-13 20:30:59 -05:00
}
1986-07-27 12:35:10 -04:00
{
1986-11-15 20:20:40 -05:00
register char * p;
register struct tzhead * tzhp;
char buf[sizeof s];
1986-07-27 12:35:10 -04:00
1986-11-15 20:20:40 -05:00
i = read(fid, buf, sizeof buf);
if (close(fid) != 0 || i < sizeof *tzhp)
1986-03-04 09:49:44 -05:00
return -1;
1986-11-15 20:20:40 -05:00
tzhp = (struct tzhead *) buf;
1986-12-04 20:32:58 -05:00
s.timecnt = detzcode(tzhp->tzh_timecnt);
s.typecnt = detzcode(tzhp->tzh_typecnt);
s.charcnt = detzcode(tzhp->tzh_charcnt);
if (s.timecnt > TZ_MAX_TIMES ||
s.typecnt == 0 ||
s.typecnt > TZ_MAX_TYPES ||
s.charcnt > TZ_MAX_CHARS)
1986-07-27 12:35:10 -04:00
return -1;
1986-11-15 20:20:40 -05:00
if (i < sizeof *tzhp +
1986-12-04 20:32:58 -05:00
s.timecnt * (4 + sizeof (char)) +
s.typecnt * (4 + 2 * sizeof (char)) +
s.charcnt * sizeof (char))
1986-07-27 12:35:10 -04:00
return -1;
1986-11-15 20:20:40 -05:00
p = buf + sizeof *tzhp;
1986-12-04 20:32:58 -05:00
for (i = 0; i < s.timecnt; ++i) {
s.ats[i] = detzcode(p);
1986-11-15 20:20:40 -05:00
p += 4;
}
1986-12-04 20:32:58 -05:00
for (i = 0; i < s.timecnt; ++i)
s.types[i] = (unsigned char) *p++;
for (i = 0; i < s.typecnt; ++i) {
1986-08-28 10:38:34 -04:00
register struct ttinfo * ttisp;
1986-12-04 20:32:58 -05:00
ttisp = &s.ttis[i];
1986-11-15 20:20:40 -05:00
ttisp->tt_gmtoff = detzcode(p);
p += 4;
ttisp->tt_isdst = (unsigned char) *p++;
ttisp->tt_abbrind = (unsigned char) *p++;
1986-07-27 12:35:10 -04:00
}
1986-12-04 20:32:58 -05:00
for (i = 0; i < s.charcnt; ++i)
s.chars[i] = *p++;
s.chars[i] = '\0'; /* ensure '\0' at end */
1986-03-04 09:49:44 -05:00
}
1986-08-28 10:05:22 -04:00
/*
** Check that all the local time type indices are valid.
*/
1986-12-04 20:32:58 -05:00
for (i = 0; i < s.timecnt; ++i)
if (s.types[i] >= s.typecnt)
1986-03-04 09:49:44 -05:00
return -1;
1986-11-21 13:04:11 -05:00
/*
** Check that all abbreviation indices are valid.
*/
1986-12-04 20:32:58 -05:00
for (i = 0; i < s.typecnt; ++i)
if (s.ttis[i].tt_abbrind >= s.charcnt)
1986-03-04 09:49:44 -05:00
return -1;
1986-11-21 12:59:20 -05:00
/*
** Set tzname elements to initial values.
*/
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-04 20:32:58 -05:00
timezone = s.ttis[0].tt_gmtoff;
1986-12-02 19:31:05 -05:00
daylight = 0;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-12-04 20:32:58 -05:00
tzname[0] = tzname[1] = &s.chars[0];
for (i = 1; i < s.typecnt; ++i) {
1986-11-21 12:59:20 -05:00
register struct ttinfo * ttisp;
1986-12-04 20:32:58 -05:00
ttisp = &s.ttis[i];
1986-12-02 19:31:05 -05:00
if (ttisp->tt_isdst) {
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-02 19:31:05 -05:00
daylight = 1;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-12-04 20:32:58 -05:00
tzname[1] = &s.chars[ttisp->tt_abbrind];
1986-12-02 19:31:05 -05:00
} else {
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-02 19:31:05 -05:00
timezone = ttisp->tt_gmtoff;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-12-04 20:32:58 -05:00
tzname[0] = &s.chars[ttisp->tt_abbrind];
1986-12-02 19:31:05 -05:00
}
1986-11-21 12:59:20 -05:00
}
1986-01-13 17:21:20 -05:00
return 0;
}
1986-12-16 15:26:16 -05:00
static
tzgmt()
1986-01-13 17:21:20 -05:00
{
1986-11-15 20:20:40 -05:00
s.timecnt = 0;
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-02 19:31:05 -05:00
timezone = 0;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-07-27 12:35:10 -04:00
s.ttis[0].tt_gmtoff = 0;
1986-12-26 15:22:05 -05:00
#ifdef USG_COMPAT
1986-12-02 19:31:05 -05:00
daylight = 0;
1986-12-26 15:22:05 -05:00
#endif /* USG_COMPAT */
1986-07-27 12:35:10 -04:00
s.ttis[0].tt_abbrind = 0;
(void) strcpy(s.chars, "GMT");
1986-11-21 12:59:20 -05:00
tzname[0] = tzname[1] = s.chars;
1986-01-13 17:21:20 -05:00
}
1986-01-16 08:50:51 -05:00
1986-12-16 15:26:16 -05:00
void
tzset()
{
register char * name;
tz_is_set = TRUE;
name = getenv("TZ");
if (name != 0 && *name == '\0')
tzgmt(); /* GMT by request */
else if (tzload(name) != 0)
tzgmt();
}
void
tzsetwall()
{
tz_is_set = TRUE;
if (tzload((char *) 0) != 0)
tzgmt();
}
1986-07-27 12:35:10 -04:00
struct tm *
1986-11-15 20:20:40 -05:00
localtime(timep)
1986-07-27 12:35:10 -04:00
long * timep;
{
1986-12-04 20:32:58 -05:00
register struct ttinfo * ttisp;
register struct tm * tmp;
register int i;
long t;
if (!tz_is_set)
1986-12-04 20:32:58 -05:00
(void) tzset();
t = *timep;
if (s.timecnt == 0 || t < s.ats[0]) {
i = 0;
while (s.ttis[i].tt_isdst)
if (++i >= s.timecnt) {
i = 0;
break;
}
} else {
for (i = 1; i < s.timecnt; ++i)
if (t < s.ats[i])
break;
i = s.types[i - 1];
}
ttisp = &s.ttis[i];
t += ttisp->tt_gmtoff;
tmp = gmtime(&t);
tmp->tm_isdst = ttisp->tt_isdst;
1986-12-26 15:22:05 -05:00
#ifdef TZA_COMPAT
1986-12-04 20:32:58 -05:00
tz_abbr =
1986-12-26 15:22:05 -05:00
#endif /* TZA_COMPAT */
1986-12-04 20:32:58 -05:00
tzname[tmp->tm_isdst] = &s.chars[ttisp->tt_abbrind];
return tmp;
1986-07-27 12:35:10 -04:00
}
1986-11-15 20:20:40 -05:00
/*
********************************************************************************
*/
#define SECS_PER_MIN 60
#define MINS_PER_HOUR 60
#define HOURS_PER_DAY 24
#define DAYS_PER_WEEK 7
#define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
#define SECS_PER_DAY ((long) SECS_PER_HOUR * HOURS_PER_DAY)
#define TM_THURSDAY 4
#define TM_YEAR_BASE 1900
#define EPOCH_YEAR 1970
#define EPOCH_WDAY TM_THURSDAY
static int mon_lengths[2][12] = { /* ". . .knuckles are 31. . ." */
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static int year_lengths[2] = {
365, 366
};
/*
** Since the times representable in a 32-bit signed integer
** run from 1901 to 2037, the define below works--2000 IS a leap year.
*/
#define isleap(y) (((y) % 4) == 0)
1986-12-02 19:31:05 -05:00
time_t
mktime(timeptr)
register struct tm * timeptr;
{
register int i, year, month;
register time_t loctimevalue;
time_t gmtimevalue;
register struct ttinfo * ttisp;
if (!tz_is_set)
1986-12-04 20:32:58 -05:00
(void) tzset();
1986-12-02 19:31:05 -05:00
/*
** First, check that the time structure passed to us contains
** a valid time.
** SPECIFICATION ERROR: what if it isn't? Do we bitch, or do
** we correct it somehow - if we correct it, how do we do so?
** The only thing I [guy@sun] can think of is that it calls "gmtime"
** when it's got a time value, and stuffs the returned structure
** back into "*timeptr". Let's try that.
*/
month = timeptr->tm_mon;
if (month < 0 || month > 11)
return -1;
if (timeptr->tm_mday < 1 || timeptr->tm_mday > 31)
return -1;
if (timeptr->tm_min < 0 || timeptr->tm_min > 59)
return -1;
if (timeptr->tm_hour < 0 || timeptr->tm_hour > 23)
return -1;
loctimevalue = 0;
year = timeptr->tm_year + TM_YEAR_BASE;
if (year < EPOCH_YEAR) {
for (i = year; i < EPOCH_YEAR; i++)
loctimevalue -= year_lengths[isleap(i)];
} else {
for (i = EPOCH_YEAR; i < year; i++)
loctimevalue += year_lengths[isleap(i)];
}
while (month--)
loctimevalue += mon_lengths[isleap(year)][month];
loctimevalue += timeptr->tm_mday - 1;
loctimevalue = HOURS_PER_DAY * loctimevalue + timeptr->tm_hour;
loctimevalue = MINS_PER_HOUR * loctimevalue + timeptr->tm_min;
loctimevalue = SECS_PER_MIN * loctimevalue + timeptr->tm_sec;
/*
** "timevalue" is now the number of seconds since January 1, 1970,
** 00:00 local time. Convert it to GMT.
** SPECIFICATION ERROR: should it always use the value of tm_isdst,
** or should it compute it? We could use it only to handle local
** times that can map to two GMT times, and recompute it when the
** smoke clears. The nasty part of that is figuring out what those
** times are.
*/
1986-12-02 19:56:04 -05:00
if (s.timecnt == 0 || loctimevalue < s.ats[0]) {
1986-12-02 19:31:05 -05:00
i = 0;
while (s.ttis[i].tt_isdst)
if (++i >= s.timecnt) {
i = 0;
break;
}
ttisp = &s.ttis[i];
gmtimevalue = loctimevalue - ttisp->tt_gmtoff;
} else {
for (i = 1; i < s.timecnt; ++i) {
ttisp = &s.ttis[s.types[i - 1]];
gmtimevalue = loctimevalue - ttisp->tt_gmtoff;
if (gmtimevalue < s.ats[i])
break;
}
}
/*
** Fill in the rest of the structure.
*/
*timeptr = *localtime(&gmtimevalue);
return gmtimevalue;
}