Files
amiga-tz/localtime.c
T

396 lines
7.9 KiB
C
Raw Normal View History

1986-01-13 17:21:20 -05:00
#
/*LINTLIBRARY*/
1986-02-15 15:23:29 -05:00
#include "tzfile.h"
1986-01-13 17:21:20 -05:00
#include "time.h"
#ifdef OBJECTID
static char sccsid[] = "%W%";
#endif
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-03-02 22:03:44 -05:00
#endif
1986-03-04 09:49:44 -05:00
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
1986-11-15 20:20:40 -05:00
extern char * getenv();
1986-01-15 08:27:06 -05:00
extern char * strcpy();
extern char * strcat();
1986-11-15 20:20:40 -05:00
extern char * sprintf();
char * asctime();
struct tm * gmtime();
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-03-02 22:03:44 -05:00
static char isset;
1986-01-13 17:21:20 -05:00
1986-11-21 12:59:20 -05:00
char * tzname[2] = { "GMT", "GMT" };
1986-11-21 12:37:36 -05:00
#ifdef TZ_ABBR
char * tz_abbr; /* compatibility w/older versions */
#endif
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-11-21 12:59:20 -05:00
tzload(name, sp)
register char * name;
1986-07-27 12:35:10 -04:00
register struct state * sp;
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;
sp->timecnt = detzcode(tzhp->tzh_timecnt);
sp->typecnt = detzcode(tzhp->tzh_typecnt);
sp->charcnt = detzcode(tzhp->tzh_charcnt);
if (sp->timecnt > TZ_MAX_TIMES ||
sp->typecnt == 0 ||
sp->typecnt > TZ_MAX_TYPES ||
sp->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 +
sp->timecnt * (4 + sizeof (char)) +
sp->typecnt * (4 + 2 * sizeof (char)) +
sp->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;
for (i = 0; i < sp->timecnt; ++i) {
sp->ats[i] = detzcode(p);
p += 4;
}
for (i = 0; i < sp->timecnt; ++i)
sp->types[i] = (unsigned char) *p++;
for (i = 0; i < sp->typecnt; ++i) {
1986-08-28 10:38:34 -04:00
register struct ttinfo * ttisp;
ttisp = &sp->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-11-15 20:20:40 -05:00
for (i = 0; i < sp->charcnt; ++i)
1986-08-28 10:05:22 -04:00
sp->chars[i] = *p++;
1986-11-15 20:20:40 -05:00
sp->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-11-21 13:04:11 -05:00
for (i = 0; i < sp->timecnt; ++i)
1986-11-15 20:20:40 -05:00
if (sp->types[i] >= sp->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.
*/
for (i = 0; i < sp->typecnt; ++i)
1986-11-15 20:20:40 -05:00
if (sp->ttis[i].tt_abbrind >= sp->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.
*/
tzname[0] = tzname[1] = &sp->chars[0];
for (i = 1; i < sp->typecnt; ++i) {
register struct ttinfo * ttisp;
ttisp = &sp->ttis[i];
tzname[ttisp->tt_isdst != 0] = &sp->chars[ttisp->tt_abbrind];
}
1986-01-13 17:21:20 -05:00
return 0;
}
1986-01-16 08:50:51 -05:00
/*
1986-03-01 17:07:13 -05:00
** settz("") Use built-in GMT.
1986-03-05 18:49:16 -05:00
** settz((char *) 0) Use TZDEFAULT.
1986-03-01 17:07:13 -05:00
** settz(otherwise) Use otherwise.
1986-01-16 08:50:51 -05:00
*/
1986-11-21 12:37:36 -05:00
settz(name)
char * name;
1986-01-13 17:21:20 -05:00
{
1986-11-15 20:20:40 -05:00
register int result;
1986-01-13 17:21:20 -05:00
1986-03-02 22:03:44 -05:00
isset = TRUE;
1986-11-21 12:37:36 -05:00
if (name != 0 && *name == '\0')
1986-11-15 20:20:40 -05:00
result = 0; /* Use built-in GMT */
1986-01-16 08:50:51 -05:00
else {
1986-11-21 12:37:36 -05:00
if (tzload(name, &s) == 0)
1986-01-16 08:50:51 -05:00
return 0;
/*
1986-02-28 21:29:14 -05:00
** If we want to try for local time on errors. . .
1986-07-27 12:35:10 -04:00
if (tzload((char *) 0, &s) == 0)
1986-01-16 08:50:51 -05:00
return -1;
1986-02-28 21:29:14 -05:00
*/
1986-11-15 20:20:40 -05:00
result = -1;
1986-01-16 08:50:51 -05:00
}
1986-11-15 20:20:40 -05:00
s.timecnt = 0;
1986-07-27 12:35:10 -04:00
s.ttis[0].tt_gmtoff = 0;
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-11-15 20:20:40 -05:00
return result;
1986-01-13 17:21:20 -05:00
}
1986-07-27 12:35:10 -04:00
static struct tm *
1986-11-15 20:20:40 -05:00
timesub(timep, sp)
1986-07-27 12:35:10 -04:00
register long * timep;
register struct state * sp;
1986-01-13 17:21:20 -05:00
{
1986-08-28 10:05:22 -04:00
register struct ttinfo * ttisp;
1986-03-05 18:49:16 -05:00
register struct tm * tmp;
1986-01-16 08:50:51 -05:00
register int i;
long t;
1986-01-13 17:21:20 -05:00
1986-01-16 08:50:51 -05:00
t = *timep;
1986-11-24 16:37:20 -05:00
if (sp->timecnt == 0 || t < sp->ats[0]) {
1986-03-02 22:03:44 -05:00
i = 0;
1986-11-24 16:37:20 -05:00
while (sp->ttis[i].tt_isdst)
if (++i >= sp->timecnt) {
i = 0;
break;
}
} else {
1986-11-15 20:20:40 -05:00
for (i = 1; i < sp->timecnt; ++i)
1986-07-27 12:35:10 -04:00
if (t < sp->ats[i])
1986-01-16 08:50:51 -05:00
break;
1986-07-27 12:35:10 -04:00
i = sp->types[i - 1];
1986-01-16 08:50:51 -05:00
}
1986-08-28 10:05:22 -04:00
ttisp = &sp->ttis[i];
t += ttisp->tt_gmtoff;
1986-03-05 18:49:16 -05:00
tmp = gmtime(&t);
1986-08-28 10:05:22 -04:00
tmp->tm_isdst = ttisp->tt_isdst;
1986-11-21 12:37:36 -05:00
#ifdef TZ_ABBR
tz_abbr =
#endif
tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1986-03-05 18:49:16 -05:00
return tmp;
1986-01-13 17:21:20 -05:00
}
1986-01-16 08:50:51 -05:00
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;
{
if (!isset)
(void) settz(getenv("TZ"));
1986-11-15 20:20:40 -05:00
return timesub(timep, &s);
1986-07-27 12:35:10 -04:00
}
struct tm *
zonetime(timep, zone)
long * timep;
char * zone;
{
1986-11-21 12:59:20 -05:00
/*
** This struct needs to be static since tzname[i] may end up pointing
1986-11-24 18:51:12 -05:00
** to it. Alternately, localtime might copy time zone abbreviations
** into small buffers. We'll take the easy way out for now.
1986-11-21 12:59:20 -05:00
*/
static struct state st;
1986-07-27 12:35:10 -04:00
1986-11-15 20:20:40 -05:00
return (tzload(zone, &st) == 0) ? timesub(timep, &st) : 0;
1986-07-27 12:35:10 -04:00
}
1986-01-16 08:50:51 -05:00
char *
1986-11-15 20:20:40 -05:00
ctime(timep)
1986-03-01 17:07:13 -05:00
long * timep;
1986-01-16 08:50:51 -05:00
{
1986-11-15 20:20:40 -05:00
return asctime(localtime(timep));
}
/*
********************************************************************************
*/
#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_SUNDAY 0
#define TM_MONDAY 1
#define TM_TUESDAY 2
#define TM_WEDNESDAY 3
#define TM_THURSDAY 4
#define TM_FRIDAY 5
#define TM_SATURDAY 6
#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)
struct tm *
gmtime(clock)
long * clock;
{
register struct tm * tmp;
register long days;
register long rem;
register int y;
register int yleap;
register int * ip;
static struct tm tm;
tmp = &tm;
y = EPOCH_YEAR;
days = *clock / SECS_PER_DAY;
rem = *clock % SECS_PER_DAY;
if (rem < 0) {
rem = rem + SECS_PER_DAY;
--days;
}
tmp->tm_hour = (int) (rem / SECS_PER_HOUR);
rem = rem % SECS_PER_HOUR;
tmp->tm_min = (int) (rem / SECS_PER_MIN);
tmp->tm_sec = (int) (rem % SECS_PER_MIN);
tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYS_PER_WEEK);
if (tmp->tm_wday < 0)
tmp->tm_wday += DAYS_PER_WEEK;
y = EPOCH_YEAR;
if (days >= 0)
for ( ; ; ) {
yleap = isleap(y);
if (days < (long) year_lengths[yleap])
break;
++y;
days = days - (long) year_lengths[yleap];
}
else do {
--y;
yleap = isleap(y);
days = days + (long) year_lengths[yleap];
} while (days < 0);
tmp->tm_year = y - TM_YEAR_BASE;
tmp->tm_yday = (int) days;
ip = mon_lengths[yleap];
for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
days = days - (long) ip[tmp->tm_mon];
tmp->tm_mday = (int) (days + 1);
tmp->tm_isdst = 0;
return tmp;
}
/*
** A la X3J11
*/
char *
asctime(timeptr)
register struct tm * timeptr;
{
static char wday_name[7][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char mon_name[12][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char result[26];
(void) sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
TM_YEAR_BASE + timeptr->tm_year);
return result;
1986-01-16 08:50:51 -05:00
}
1986-11-21 12:37:36 -05:00
/*
** System V compatability.
*/
void
tzset()
{
(void) settz((char *) 0);
}