/* Copyright (C) 1991-2013 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <ctype.h>
#include <errno.h>
#include <bits/libc-lock.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NOID
#include <timezone/tzfile.h>
char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
int __daylight = 0;
long int __timezone = 0L;
weak_alias (__tzname, tzname)
weak_alias (__daylight, daylight)
weak_alias (__timezone, timezone)
/* This locks all the state variables in tzfile.c and this file. */
__libc_lock_define_initialized (static, tzset_lock)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define sign(x) ((x) < 0 ? -1 : 1)
/* This structure contains all the information about a
timezone given in the POSIX standard TZ envariable. */
typedef struct
{
const char *name;
/* When to change. */
enum { J0, J1, M } type; /* Interpretation of: */
unsigned short int m, n, d; /* Month, week, day. */
unsigned int secs; /* Time of day. */
long int offset; /* Seconds east of GMT (west if < 0). */
/* We cache the computed time of change for a
given year so we don't have to recompute it. */
time_t change; /* When to change to this zone. */
int computed_for; /* Year above is computed for. */
} tz_rule;
/* tz_rules[0] is standard, tz_rules[1] is daylight. */
static tz_rule tz_rules[2];
static void compute_change (tz_rule *rule, int year) __THROW internal_function;
static void tzset_internal (int always, int explicit)
__THROW internal_function;
/* List of buffers containing time zone strings. */
struct tzstring_l
{
struct tzstring_l *next;
size_t len; /* strlen(data) - doesn't count terminating NUL! */
char data[0];
};
static struct tzstring_l *tzstring_list;
/* Allocate a permanent home for S. It will never be moved or deallocated,
but may share space with other strings.
Don't modify the returned string. */
char *
__tzstring (const char *s)
{
char *p;
struct tzstring_l *t, *u, *new;
size_t len = strlen (s);
/* Walk the list and look for a match. If this string is the same
as the end of an already-allocated string, it can share space. */
for (u = t = tzstring_list; t; u = t, t = t->next)
if (len <= t->len)
{
p = &t->data[t->len - len];
if (strcmp (s, p) == 0)
return p;
}
/* Not found; allocate a new buffer. */
new = malloc (sizeof (struct tzstring_l) + len + 1);
if (!new)
return