amiga-tz/library/time_zoneinit.c

57 lines
1.4 KiB
C

#include "time_header.h"
static void scrub_abbrs(struct state *sp)
{
int i;
// Replace bogus characters.
for (i = 0; i < sp->charcnt; ++i)
if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
sp->chars[i] = TZ_ABBR_ERR_CHAR;
// Truncate long abbreviations.
for (i = 0; i < sp->typecnt; ++i) {
const struct ttinfo * const ttisp = &sp->ttis[i];
char *cp = &sp->chars[ttisp->tt_abbrind];
if (strlen(cp) > TZ_ABBR_MAX_LEN && strcmp(cp, GRANDPARENTED) != 0) {
*(cp + TZ_ABBR_MAX_LEN) = '\0';
}
}
}
/**
* @brief
* Initialize *SP to a value appropriate for the TZ setting NAME.
* Return 0 on success, an errno value on failure.
*/
int zoneinit(struct state *sp, char const *name)
{
if (name && ! name[0]) {
// User wants it fast rather than right.
sp->leapcnt = 0; // so, we're off a little
sp->timecnt = 0;
sp->typecnt = 0;
sp->charcnt = 0;
sp->goback = sp->goahead = false;
init_ttinfo(&sp->ttis[0], 0, false, 0);
strcpy(sp->chars, gmt);
sp->defaulttype = 0;
return 0;
} else {
int err = tzload(name, sp, true);
if (err != 0 && name && name[0] != '\0' && tzparse(name, sp, false)) {
err = 0;
}
if (err == 0) {
scrub_abbrs(sp);
}
return err;
}
}