Pgindent timezone file, per request from Tom.

This commit is contained in:
Bruce Momjian 2004-05-21 12:30:25 +00:00
parent 63bd0db121
commit 0a19fb42c2
9 changed files with 2226 additions and 1754 deletions

View File

@ -5,54 +5,63 @@
#define nonzero(n) (((n) == 0) ? 1 : (n)) #define nonzero(n) (((n) == 0) ? 1 : (n))
char *imalloc(const int n) char *
imalloc(const int n)
{ {
return malloc((size_t) nonzero(n)); return malloc((size_t) nonzero(n));
} }
char *icalloc(int nelem, int elsize) char *
icalloc(int nelem, int elsize)
{ {
if (nelem == 0 || elsize == 0) if (nelem == 0 || elsize == 0)
nelem = elsize = 1; nelem = elsize = 1;
return calloc((size_t) nelem, (size_t) elsize); return calloc((size_t) nelem, (size_t) elsize);
} }
void *irealloc(void *pointer, const int size) void *
irealloc(void *pointer, const int size)
{ {
if (pointer == NULL) if (pointer == NULL)
return imalloc(size); return imalloc(size);
return realloc((void *) pointer, (size_t) nonzero(size)); return realloc((void *) pointer, (size_t) nonzero(size));
} }
char *icatalloc(char *old, const char *new) char *
icatalloc(char *old, const char *new)
{ {
register char * result; register char *result;
register int oldsize, newsize; register int oldsize,
newsize;
newsize = (new == NULL) ? 0 : strlen(new); newsize = (new == NULL) ? 0 : strlen(new);
if (old == NULL) if (old == NULL)
oldsize = 0; oldsize = 0;
else if (newsize == 0) else if (newsize == 0)
return old; return old;
else oldsize = strlen(old); else
oldsize = strlen(old);
if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
if (new != NULL) if (new != NULL)
(void) strcpy(result + oldsize, new); (void) strcpy(result + oldsize, new);
return result; return result;
} }
char *icpyalloc(const char *string) char *
icpyalloc(const char *string)
{ {
return icatalloc((char *) NULL, string); return icatalloc((char *) NULL, string);
} }
void ifree(char *p) void
ifree(char *p)
{ {
if (p != NULL) if (p != NULL)
(void) free(p); (void) free(p);
} }
void icfree(char *p) void
icfree(char *p)
{ {
if (p != NULL) if (p != NULL)
(void) free(p); (void) free(p);

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/timezone/pgtz.c,v 1.10 2004/05/21 05:08:06 tgl Exp $ * $PostgreSQL: pgsql/src/timezone/pgtz.c,v 1.11 2004/05/21 12:30:25 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -45,35 +45,45 @@ pg_TZDIR(void)
* set in our own library). * set in our own library).
*/ */
#define T_YEAR (60*60*24*365) #define T_YEAR (60*60*24*365)
#define T_MONTH (60*60*24*30) #define T_MONTH (60*60*24*30)
struct tztry { struct tztry
time_t std_t,dst_t; {
char std_time[TZ_STRLEN_MAX+1],dst_time[TZ_STRLEN_MAX+1]; time_t std_t,
int std_ofs,dst_ofs; dst_t;
struct tm std_tm, dst_tm; char std_time[TZ_STRLEN_MAX + 1],
dst_time[TZ_STRLEN_MAX + 1];
int std_ofs,
dst_ofs;
struct tm std_tm,
dst_tm;
}; };
static bool compare_tm(struct tm *s, struct pg_tm *p) { static bool
if (s->tm_sec != p->tm_sec || compare_tm(struct tm * s, struct pg_tm * p)
s->tm_min != p->tm_min || {
s->tm_hour != p->tm_hour || if (s->tm_sec != p->tm_sec ||
s->tm_mday != p->tm_mday || s->tm_min != p->tm_min ||
s->tm_mon != p->tm_mon || s->tm_hour != p->tm_hour ||
s->tm_year != p->tm_year || s->tm_mday != p->tm_mday ||
s->tm_wday != p->tm_wday || s->tm_mon != p->tm_mon ||
s->tm_yday != p->tm_yday || s->tm_year != p->tm_year ||
s->tm_wday != p->tm_wday ||
s->tm_yday != p->tm_yday ||
s->tm_isdst != p->tm_isdst) s->tm_isdst != p->tm_isdst)
return false; return false;
return true; return true;
} }
static bool try_timezone(char *tzname, struct tztry *tt, bool checkdst) { static bool
try_timezone(char *tzname, struct tztry * tt, bool checkdst)
{
struct pg_tm *pgtm; struct pg_tm *pgtm;
if (!pg_tzset(tzname)) if (!pg_tzset(tzname))
return false; /* If this timezone couldn't be picked at all */ return false; /* If this timezone couldn't be picked at
* all */
/* Verify standard time */ /* Verify standard time */
pgtm = pg_localtime(&(tt->std_t)); pgtm = pg_localtime(&(tt->std_t));
@ -95,7 +105,9 @@ static bool try_timezone(char *tzname, struct tztry *tt, bool checkdst) {
return true; return true;
} }
static int get_timezone_offset(struct tm *tm) { static int
get_timezone_offset(struct tm * tm)
{
#if defined(HAVE_STRUCT_TM_TM_ZONE) #if defined(HAVE_STRUCT_TM_TM_ZONE)
return tm->tm_gmtoff; return tm->tm_gmtoff;
#elif defined(HAVE_INT_TIMEZONE) #elif defined(HAVE_INT_TIMEZONE)
@ -113,12 +125,15 @@ static int get_timezone_offset(struct tm *tm) {
#ifdef WIN32 #ifdef WIN32
#define TZABBREV(tz) win32_get_timezone_abbrev(tz) #define TZABBREV(tz) win32_get_timezone_abbrev(tz)
static char *win32_get_timezone_abbrev(char *tz) { static char *
static char w32tzabbr[TZ_STRLEN_MAX+1]; win32_get_timezone_abbrev(char *tz)
int l = 0; {
char *c; static char w32tzabbr[TZ_STRLEN_MAX + 1];
int l = 0;
char *c;
for (c = tz; *c; c++) { for (c = tz; *c; c++)
{
if (isupper(*c)) if (isupper(*c))
w32tzabbr[l++] = *c; w32tzabbr[l++] = *c;
} }
@ -140,44 +155,47 @@ static char *win32_get_timezone_abbrev(char *tz) {
static char * static char *
identify_system_timezone(void) identify_system_timezone(void)
{ {
static char __tzbuf[TZ_STRLEN_MAX+1]; static char __tzbuf[TZ_STRLEN_MAX + 1];
bool std_found=false, bool std_found = false,
dst_found=false; dst_found = false;
time_t tnow = time(NULL); time_t tnow = time(NULL);
time_t t; time_t t;
struct tztry tt; struct tztry tt;
char cbuf[TZ_STRLEN_MAX+1]; char cbuf[TZ_STRLEN_MAX + 1];
/* Initialize OS timezone library */ /* Initialize OS timezone library */
tzset(); tzset();
memset(&tt, 0, sizeof(tt)); memset(&tt, 0, sizeof(tt));
for (t = tnow; t < tnow+T_YEAR; t += T_MONTH) { for (t = tnow; t < tnow + T_YEAR; t += T_MONTH)
struct tm *tm = localtime(&t); {
struct tm *tm = localtime(&t);
if (tm->tm_isdst == 0 && !std_found) { if (tm->tm_isdst == 0 && !std_found)
{
/* Standard time */ /* Standard time */
memcpy(&tt.std_tm, tm, sizeof(struct tm)); memcpy(&tt.std_tm, tm, sizeof(struct tm));
memset(cbuf,0,sizeof(cbuf)); memset(cbuf, 0, sizeof(cbuf));
strftime(cbuf, sizeof(cbuf)-1, "%Z", tm); /* zone abbr */ strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
strcpy(tt.std_time, TZABBREV(cbuf)); strcpy(tt.std_time, TZABBREV(cbuf));
tt.std_ofs = get_timezone_offset(tm); tt.std_ofs = get_timezone_offset(tm);
tt.std_t = t; tt.std_t = t;
std_found = true; std_found = true;
} }
else if (tm->tm_isdst == 1 && !dst_found) { else if (tm->tm_isdst == 1 && !dst_found)
{
/* Daylight time */ /* Daylight time */
memcpy(&tt.dst_tm, tm, sizeof(struct tm)); memcpy(&tt.dst_tm, tm, sizeof(struct tm));
memset(cbuf,0,sizeof(cbuf)); memset(cbuf, 0, sizeof(cbuf));
strftime(cbuf, sizeof(cbuf)-1, "%Z", tm); /* zone abbr */ strftime(cbuf, sizeof(cbuf) - 1, "%Z", tm); /* zone abbr */
strcpy(tt.dst_time, TZABBREV(cbuf)); strcpy(tt.dst_time, TZABBREV(cbuf));
tt.dst_ofs = get_timezone_offset(tm); tt.dst_ofs = get_timezone_offset(tm);
tt.dst_t = t; tt.dst_t = t;
dst_found = true; dst_found = true;
} }
if (std_found && dst_found) if (std_found && dst_found)
break; /* Got both standard and daylight */ break; /* Got both standard and daylight */
} }
if (!std_found) if (!std_found)
@ -189,24 +207,25 @@ identify_system_timezone(void)
return NULL; /* go to GMT */ return NULL; /* go to GMT */
} }
if (dst_found) { if (dst_found)
{
/* Try STD<ofs>DST */ /* Try STD<ofs>DST */
sprintf(__tzbuf,"%s%d%s",tt.std_time,-tt.std_ofs/3600,tt.dst_time); sprintf(__tzbuf, "%s%d%s", tt.std_time, -tt.std_ofs / 3600, tt.dst_time);
if (try_timezone(__tzbuf, &tt, dst_found)) if (try_timezone(__tzbuf, &tt, dst_found))
return __tzbuf; return __tzbuf;
} }
/* Try just the STD timezone */ /* Try just the STD timezone */
strcpy(__tzbuf,tt.std_time); strcpy(__tzbuf, tt.std_time);
if (try_timezone(__tzbuf, &tt, dst_found)) if (try_timezone(__tzbuf, &tt, dst_found))
return __tzbuf; return __tzbuf;
/* Did not find the timezone. Fallback to try a GMT zone. */ /* Did not find the timezone. Fallback to try a GMT zone. */
sprintf(__tzbuf,"Etc/GMT%s%d", sprintf(__tzbuf, "Etc/GMT%s%d",
(-tt.std_ofs<0)?"+":"",tt.std_ofs/3600); (-tt.std_ofs < 0) ? "+" : "", tt.std_ofs / 3600);
ereport(LOG, ereport(LOG,
(errmsg("could not recognize system timezone, defaulting to \"%s\"", (errmsg("could not recognize system timezone, defaulting to \"%s\"",
__tzbuf), __tzbuf),
errhint("You can specify the correct timezone in postgresql.conf."))); errhint("You can specify the correct timezone in postgresql.conf.")));
return __tzbuf; return __tzbuf;
} }
@ -223,7 +242,7 @@ identify_system_timezone(void)
bool bool
tz_acceptable(void) tz_acceptable(void)
{ {
struct pg_tm tt; struct pg_tm tt;
time_t time2000; time_t time2000;
/* /*
@ -255,7 +274,7 @@ tz_acceptable(void)
const char * const char *
select_default_timezone(void) select_default_timezone(void)
{ {
char *def_tz; char *def_tz;
def_tz = getenv("TZ"); def_tz = getenv("TZ");
if (def_tz && pg_tzset(def_tz) && tz_acceptable()) if (def_tz && pg_tzset(def_tz) && tz_acceptable())
@ -280,9 +299,12 @@ select_default_timezone(void)
* This is called after initial loading of postgresql.conf. If no TimeZone * This is called after initial loading of postgresql.conf. If no TimeZone
* setting was found therein, we try to derive one from the environment. * setting was found therein, we try to derive one from the environment.
*/ */
void pg_timezone_initialize(void) { void
pg_timezone_initialize(void)
{
/* Do we need to try to figure the timezone? */ /* Do we need to try to figure the timezone? */
if (strcmp(GetConfigOption("timezone"), "UNKNOWN") == 0) { if (strcmp(GetConfigOption("timezone"), "UNKNOWN") == 0)
{
const char *def_tz; const char *def_tz;
/* Select setting */ /* Select setting */

View File

@ -9,7 +9,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/timezone/pgtz.h,v 1.7 2004/05/21 05:08:06 tgl Exp $ * $PostgreSQL: pgsql/src/timezone/pgtz.h,v 1.8 2004/05/21 12:30:25 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -18,6 +18,6 @@
#define TZ_STRLEN_MAX 255 #define TZ_STRLEN_MAX 255
extern char *pg_TZDIR(void); extern char *pg_TZDIR(void);
#endif /* _PGTZ_H */ #endif /* _PGTZ_H */

View File

@ -14,19 +14,19 @@
** Thank you! ** Thank you!
*/ */
#include <limits.h> /* for CHAR_BIT */ #include <limits.h> /* for CHAR_BIT */
#include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */ #include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
#include <unistd.h> /* for F_OK and R_OK */ #include <unistd.h> /* for F_OK and R_OK */
#include "pgtime.h" #include "pgtime.h"
#ifndef WIFEXITED #ifndef WIFEXITED
#define WIFEXITED(status) (((status) & 0xff) == 0) #define WIFEXITED(status) (((status) & 0xff) == 0)
#endif /* !defined WIFEXITED */ #endif /* !defined WIFEXITED */
#ifndef WEXITSTATUS #ifndef WEXITSTATUS
#define WEXITSTATUS(status) (((status) >> 8) & 0xff) #define WEXITSTATUS(status) (((status) >> 8) & 0xff)
#endif /* !defined WEXITSTATUS */ #endif /* !defined WEXITSTATUS */
/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */ /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
#define is_digit(c) ((unsigned)(c) - '0' <= 9) #define is_digit(c) ((unsigned)(c) - '0' <= 9)
@ -37,7 +37,7 @@
#ifndef EXIT_SUCCESS #ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0 #define EXIT_SUCCESS 0
#endif /* !defined EXIT_SUCCESS */ #endif /* !defined EXIT_SUCCESS */
/* /*
** SunOS 4.1.1 headers lack EXIT_FAILURE. ** SunOS 4.1.1 headers lack EXIT_FAILURE.
@ -45,28 +45,29 @@
#ifndef EXIT_FAILURE #ifndef EXIT_FAILURE
#define EXIT_FAILURE 1 #define EXIT_FAILURE 1
#endif /* !defined EXIT_FAILURE */ #endif /* !defined EXIT_FAILURE */
/* /*
** SunOS 4.1.1 libraries lack remove. ** SunOS 4.1.1 libraries lack remove.
*/ */
#ifndef remove #ifndef remove
extern int unlink (const char * filename); extern int unlink(const char *filename);
#define remove unlink #define remove unlink
#endif /* !defined remove */ #endif /* !defined remove */
/* /*
* Private function declarations. * Private function declarations.
*/ */
extern char *icalloc (int nelem, int elsize); extern char *icalloc(int nelem, int elsize);
extern char *icatalloc (char *old, const char *new); extern char *icatalloc(char *old, const char *new);
extern char *icpyalloc (const char *string); extern char *icpyalloc(const char *string);
extern char *imalloc (int n); extern char *imalloc(int n);
extern void *irealloc (void *pointer, int size); extern void *irealloc(void *pointer, int size);
extern void icfree (char *pointer); extern void icfree(char *pointer);
extern void ifree (char *pointer); extern void ifree(char *pointer);
extern char *scheck (const char *string, const char *format); extern char *scheck(const char *string, const char *format);
/* /*
@ -75,19 +76,19 @@ extern char *scheck (const char *string, const char *format);
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif /* !defined TRUE */ #endif /* !defined TRUE */
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif /* !defined FALSE */ #endif /* !defined FALSE */
#ifndef TYPE_BIT #ifndef TYPE_BIT
#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT) #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
#endif /* !defined TYPE_BIT */ #endif /* !defined TYPE_BIT */
#ifndef TYPE_SIGNED #ifndef TYPE_SIGNED
#define TYPE_SIGNED(type) (((type) -1) < 0) #define TYPE_SIGNED(type) (((type) -1) < 0)
#endif /* !defined TYPE_SIGNED */ #endif /* !defined TYPE_SIGNED */
#ifndef INT_STRLEN_MAXIMUM #ifndef INT_STRLEN_MAXIMUM
/* /*
@ -97,8 +98,8 @@ extern char *scheck (const char *string, const char *format);
** add one more for a minus sign if the type is signed. ** add one more for a minus sign if the type is signed.
*/ */
#define INT_STRLEN_MAXIMUM(type) \ #define INT_STRLEN_MAXIMUM(type) \
((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type)) ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
#endif /* !defined INT_STRLEN_MAXIMUM */ #endif /* !defined INT_STRLEN_MAXIMUM */
#define _(msgid) (msgid) #define _(msgid) (msgid)
@ -106,4 +107,4 @@ extern char *scheck (const char *string, const char *format);
** UNIX was a registered trademark of The Open Group in 2003. ** UNIX was a registered trademark of The Open Group in 2003.
*/ */
#endif /* !defined PRIVATE_H */ #endif /* !defined PRIVATE_H */

View File

@ -3,15 +3,16 @@
#include "private.h" #include "private.h"
char *scheck(const char *string, const char *format) char *
scheck(const char *string, const char *format)
{ {
register char * fbuf; register char *fbuf;
register const char * fp; register const char *fp;
register char * tp; register char *tp;
register int c; register int c;
register char * result; register char *result;
char dummy; char dummy;
static char nada; static char nada;
result = &nada; result = &nada;
if (string == NULL || format == NULL) if (string == NULL || format == NULL)
@ -21,10 +22,12 @@ char *scheck(const char *string, const char *format)
return result; return result;
fp = format; fp = format;
tp = fbuf; tp = fbuf;
while ((*tp++ = c = *fp++) != '\0') { while ((*tp++ = c = *fp++) != '\0')
{
if (c != '%') if (c != '%')
continue; continue;
if (*fp == '%') { if (*fp == '%')
{
*tp++ = *fp++; *tp++ = *fp++;
continue; continue;
} }
@ -36,8 +39,9 @@ char *scheck(const char *string, const char *format)
if (*fp == 'l' || *fp == 'h') if (*fp == 'l' || *fp == 'h')
*tp++ = *fp++; *tp++ = *fp++;
else if (*fp == '[') else if (*fp == '[')
do *tp++ = *fp++; do
while (*fp != '\0' && *fp != ']'); *tp++ = *fp++;
while (*fp != '\0' && *fp != ']');
if ((*tp++ = *fp++) == '\0') if ((*tp++ = *fp++) == '\0')
break; break;
} }

View File

@ -25,22 +25,23 @@
#include "tzfile.h" #include "tzfile.h"
struct lc_time_T { struct lc_time_T
const char * mon[MONSPERYEAR]; {
const char * month[MONSPERYEAR]; const char *mon[MONSPERYEAR];
const char * wday[DAYSPERWEEK]; const char *month[MONSPERYEAR];
const char * weekday[DAYSPERWEEK]; const char *wday[DAYSPERWEEK];
const char * X_fmt; const char *weekday[DAYSPERWEEK];
const char * x_fmt; const char *X_fmt;
const char * c_fmt; const char *x_fmt;
const char * am; const char *c_fmt;
const char * pm; const char *am;
const char * date_fmt; const char *pm;
const char *date_fmt;
}; };
#define Locale (&C_time_locale) #define Locale (&C_time_locale)
static const struct lc_time_T C_time_locale = { static const struct lc_time_T C_time_locale = {
{ {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
@ -59,21 +60,16 @@ static const struct lc_time_T C_time_locale = {
"%H:%M:%S", "%H:%M:%S",
/* /*
** x_fmt * * x_fmt * C99 requires this format. * Using just numbers (as here)
** C99 requires this format. * makes Quakers happier; * it's also compatible with SVR4.
** Using just numbers (as here) makes Quakers happier; */
** it's also compatible with SVR4.
*/
"%m/%d/%y", "%m/%d/%y",
/* /*
** c_fmt * * c_fmt * C99 requires this format. * Previously this code used "%D
** C99 requires this format. * %X", but we now conform to C99. * Note that * "%a %b %d
** Previously this code used "%D %X", but we now conform to C99. * %H:%M:%S %Y" * is used by Solaris 2.3.
** Note that */
** "%a %b %d %H:%M:%S %Y"
** is used by Solaris 2.3.
*/
"%a %b %e %T %Y", "%a %b %e %T %Y",
/* am */ /* am */
@ -86,20 +82,21 @@ static const struct lc_time_T C_time_locale = {
"%a %b %e %H:%M:%S %Z %Y" "%a %b %e %H:%M:%S %Z %Y"
}; };
static char * _add (const char *, char *, const char *); static char *_add(const char *, char *, const char *);
static char * _conv (int, const char *, char *, const char *); static char *_conv(int, const char *, char *, const char *);
static char * _fmt (const char *, const struct pg_tm *, char *, const char *, int *); static char *_fmt(const char *, const struct pg_tm *, char *, const char *, int *);
#define IN_NONE 0 #define IN_NONE 0
#define IN_SOME 1 #define IN_SOME 1
#define IN_THIS 2 #define IN_THIS 2
#define IN_ALL 3 #define IN_ALL 3
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t) size_t
pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm * t)
{ {
char * p; char *p;
int warn; int warn;
warn = IN_NONE; warn = IN_NONE;
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn); p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
@ -109,196 +106,196 @@ size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_
return p - s; return p - s;
} }
static char * _fmt(const char *format, const struct pg_tm *t, char *pt, const char *ptlim, int *warnp) static char *
_fmt(const char *format, const struct pg_tm * t, char *pt, const char *ptlim, int *warnp)
{ {
for ( ; *format; ++format) { for (; *format; ++format)
if (*format == '%') { {
label: if (*format == '%')
switch (*++format) { {
case '\0': label:
--format; switch (*++format)
break; {
case 'A': case '\0':
pt = _add((t->tm_wday < 0 || --format;
t->tm_wday >= DAYSPERWEEK) ? break;
"?" : Locale->weekday[t->tm_wday], case 'A':
pt, ptlim); pt = _add((t->tm_wday < 0 ||
continue; t->tm_wday >= DAYSPERWEEK) ?
case 'a': "?" : Locale->weekday[t->tm_wday],
pt = _add((t->tm_wday < 0 || pt, ptlim);
t->tm_wday >= DAYSPERWEEK) ? continue;
"?" : Locale->wday[t->tm_wday], case 'a':
pt, ptlim); pt = _add((t->tm_wday < 0 ||
continue; t->tm_wday >= DAYSPERWEEK) ?
case 'B': "?" : Locale->wday[t->tm_wday],
pt = _add((t->tm_mon < 0 || pt, ptlim);
t->tm_mon >= MONSPERYEAR) ? continue;
"?" : Locale->month[t->tm_mon], case 'B':
pt, ptlim); pt = _add((t->tm_mon < 0 ||
continue; t->tm_mon >= MONSPERYEAR) ?
case 'b': "?" : Locale->month[t->tm_mon],
case 'h': pt, ptlim);
pt = _add((t->tm_mon < 0 || continue;
t->tm_mon >= MONSPERYEAR) ? case 'b':
"?" : Locale->mon[t->tm_mon], case 'h':
pt, ptlim); pt = _add((t->tm_mon < 0 ||
continue; t->tm_mon >= MONSPERYEAR) ?
case 'C': "?" : Locale->mon[t->tm_mon],
/* pt, ptlim);
** %C used to do a... continue;
** _fmt("%a %b %e %X %Y", t); case 'C':
** ...whereas now POSIX 1003.2 calls for
** something completely different.
** (ado, 1993-05-24)
*/
pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
"%02d", pt, ptlim);
continue;
case 'c':
{
int warn2 = IN_SOME;
pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp); /*
if (warn2 == IN_ALL) * * %C used to do a... * _fmt("%a %b %e %X %Y", t); *
warn2 = IN_THIS; * ...whereas now POSIX 1003.2 calls for * something
if (warn2 > *warnp) * completely different. * (ado, 1993-05-24)
*warnp = warn2; */
} pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
continue; "%02d", pt, ptlim);
case 'D': continue;
pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp); case 'c':
continue; {
case 'd': int warn2 = IN_SOME;
pt = _conv(t->tm_mday, "%02d", pt, ptlim);
continue; pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
case 'E': if (warn2 == IN_ALL)
case 'O': warn2 = IN_THIS;
/* if (warn2 > *warnp)
** C99 locale modifiers. *warnp = warn2;
** The sequences }
** %Ec %EC %Ex %EX %Ey %EY continue;
** %Od %oe %OH %OI %Om %OM case 'D':
** %OS %Ou %OU %OV %Ow %OW %Oy pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
** are supposed to provide alternate continue;
** representations. case 'd':
*/ pt = _conv(t->tm_mday, "%02d", pt, ptlim);
goto label; continue;
case 'e': case 'E':
pt = _conv(t->tm_mday, "%2d", pt, ptlim); case 'O':
continue;
case 'F': /*
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp); * * C99 locale modifiers. * The sequences * %Ec %EC
continue; * %Ex %EX %Ey %EY * %Od %oe %OH %OI %Om %OM * %OS
case 'H': * %Ou %OU %OV %Ow %OW %Oy * are supposed to provide
pt = _conv(t->tm_hour, "%02d", pt, ptlim); * alternate * representations.
continue; */
case 'I': goto label;
pt = _conv((t->tm_hour % 12) ? case 'e':
(t->tm_hour % 12) : 12, pt = _conv(t->tm_mday, "%2d", pt, ptlim);
"%02d", pt, ptlim); continue;
continue; case 'F':
case 'j': pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim); continue;
continue; case 'H':
case 'k': pt = _conv(t->tm_hour, "%02d", pt, ptlim);
/* continue;
** This used to be... case 'I':
** _conv(t->tm_hour % 12 ? pt = _conv((t->tm_hour % 12) ?
** t->tm_hour % 12 : 12, 2, ' '); (t->tm_hour % 12) : 12,
** ...and has been changed to the below to "%02d", pt, ptlim);
** match SunOS 4.1.1 and Arnold Robbins' continue;
** strftime version 3.0. That is, "%k" and case 'j':
** "%l" have been swapped. pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
** (ado, 1993-05-24) continue;
*/ case 'k':
pt = _conv(t->tm_hour, "%2d", pt, ptlim);
continue; /*
* * This used to be... * _conv(t->tm_hour % 12 ? *
* t->tm_hour % 12 : 12, 2, ' '); * ...and has been
* changed to the below to * match SunOS 4.1.1 and
* Arnold Robbins' * strftime version 3.0. That is,
* "%k" and * "%l" have been swapped. * (ado,
* 1993-05-24)
*/
pt = _conv(t->tm_hour, "%2d", pt, ptlim);
continue;
#ifdef KITCHEN_SINK #ifdef KITCHEN_SINK
case 'K': case 'K':
/*
** After all this time, still unclaimed!
*/
pt = _add("kitchen sink", pt, ptlim);
continue;
#endif /* defined KITCHEN_SINK */
case 'l':
/*
** This used to be...
** _conv(t->tm_hour, 2, ' ');
** ...and has been changed to the below to
** match SunOS 4.1.1 and Arnold Robbin's
** strftime version 3.0. That is, "%k" and
** "%l" have been swapped.
** (ado, 1993-05-24)
*/
pt = _conv((t->tm_hour % 12) ?
(t->tm_hour % 12) : 12,
"%2d", pt, ptlim);
continue;
case 'M':
pt = _conv(t->tm_min, "%02d", pt, ptlim);
continue;
case 'm':
pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
continue;
case 'n':
pt = _add("\n", pt, ptlim);
continue;
case 'p':
pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Locale->pm :
Locale->am,
pt, ptlim);
continue;
case 'R':
pt = _fmt("%H:%M", t, pt, ptlim, warnp);
continue;
case 'r':
pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
continue;
case 'S':
pt = _conv(t->tm_sec, "%02d", pt, ptlim);
continue;
case 's':
{
struct pg_tm tm;
char buf[INT_STRLEN_MAXIMUM(time_t) + 1];
time_t mkt;
tm = *t; /*
mkt = pg_mktime(&tm); * * After all this time, still unclaimed!
if (TYPE_SIGNED(time_t)) */
(void) sprintf(buf, "%ld", (long) mkt); pt = _add("kitchen sink", pt, ptlim);
else continue;
(void) sprintf(buf, "%lu", (unsigned long) mkt); #endif /* defined KITCHEN_SINK */
pt = _add(buf, pt, ptlim); case 'l':
}
continue; /*
case 'T': * * This used to be... * _conv(t->tm_hour, 2, ' '); *
pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp); * ...and has been changed to the below to * match
continue; * SunOS 4.1.1 and Arnold Robbin's * strftime version
case 't': * 3.0. That is, "%k" and * "%l" have been swapped. *
pt = _add("\t", pt, ptlim); * (ado, 1993-05-24)
continue; */
case 'U': pt = _conv((t->tm_hour % 12) ?
pt = _conv((t->tm_yday + DAYSPERWEEK - (t->tm_hour % 12) : 12,
t->tm_wday) / DAYSPERWEEK, "%2d", pt, ptlim);
"%02d", pt, ptlim); continue;
continue; case 'M':
case 'u': pt = _conv(t->tm_min, "%02d", pt, ptlim);
/* continue;
** From Arnold Robbins' strftime version 3.0: case 'm':
** "ISO 8601: Weekday as a decimal number pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
** [1 (Monday) - 7]" continue;
** (ado, 1993-05-24) case 'n':
*/ pt = _add("\n", pt, ptlim);
pt = _conv((t->tm_wday == 0) ? continue;
DAYSPERWEEK : t->tm_wday, case 'p':
"%d", pt, ptlim); pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
continue; Locale->pm :
case 'V': /* ISO 8601 week number */ Locale->am,
case 'G': /* ISO 8601 year (four digits) */ pt, ptlim);
case 'g': /* ISO 8601 year (two digits) */ continue;
case 'R':
pt = _fmt("%H:%M", t, pt, ptlim, warnp);
continue;
case 'r':
pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
continue;
case 'S':
pt = _conv(t->tm_sec, "%02d", pt, ptlim);
continue;
case 's':
{
struct pg_tm tm;
char buf[INT_STRLEN_MAXIMUM(time_t) +1];
time_t mkt;
tm = *t;
mkt = pg_mktime(&tm);
if (TYPE_SIGNED(time_t))
(void) sprintf(buf, "%ld", (long) mkt);
else
(void) sprintf(buf, "%lu", (unsigned long) mkt);
pt = _add(buf, pt, ptlim);
}
continue;
case 'T':
pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
continue;
case 't':
pt = _add("\t", pt, ptlim);
continue;
case 'U':
pt = _conv((t->tm_yday + DAYSPERWEEK -
t->tm_wday) / DAYSPERWEEK,
"%02d", pt, ptlim);
continue;
case 'u':
/*
* * From Arnold Robbins' strftime version 3.0: * "ISO
* 8601: Weekday as a decimal number * [1 (Monday) -
* 7]" * (ado, 1993-05-24)
*/
pt = _conv((t->tm_wday == 0) ?
DAYSPERWEEK : t->tm_wday,
"%d", pt, ptlim);
continue;
case 'V': /* ISO 8601 week number */
case 'G': /* ISO 8601 year (four digits) */
case 'g': /* ISO 8601 year (two digits) */
/* /*
** From Arnold Robbins' strftime version 3.0: "the week number of the ** From Arnold Robbins' strftime version 3.0: "the week number of the
** year (the first Monday as the first day of week 1) as a decimal number ** year (the first Monday as the first day of week 1) as a decimal number
@ -317,144 +314,156 @@ label:
** 1997 lasts from 1996-12-30 to 1997-01-05..." ** 1997 lasts from 1996-12-30 to 1997-01-05..."
** (ado, 1996-01-02) ** (ado, 1996-01-02)
*/ */
{ {
int year; int year;
int yday; int yday;
int wday; int wday;
int w; int w;
year = t->tm_year + TM_YEAR_BASE; year = t->tm_year + TM_YEAR_BASE;
yday = t->tm_yday; yday = t->tm_yday;
wday = t->tm_wday; wday = t->tm_wday;
for ( ; ; ) { for (;;)
int len; {
int bot; int len;
int top; int bot;
int top;
len = isleap(year) ? len = isleap(year) ?
DAYSPERLYEAR : DAYSPERLYEAR :
DAYSPERNYEAR; DAYSPERNYEAR;
/*
** What yday (-3 ... 3) does /*
** the ISO year begin on? * * What yday (-3 ... 3) does * the ISO year
*/ * begin on?
bot = ((yday + 11 - wday) % */
DAYSPERWEEK) - 3; bot = ((yday + 11 - wday) %
/* DAYSPERWEEK) - 3;
** What yday does the NEXT
** ISO year begin on? /*
*/ * * What yday does the NEXT * ISO year begin
top = bot - * on?
(len % DAYSPERWEEK); */
if (top < -3) top = bot -
top += DAYSPERWEEK; (len % DAYSPERWEEK);
top += len; if (top < -3)
if (yday >= top) { top += DAYSPERWEEK;
++year; top += len;
w = 1; if (yday >= top)
break; {
++year;
w = 1;
break;
}
if (yday >= bot)
{
w = 1 + ((yday - bot) /
DAYSPERWEEK);
break;
}
--year;
yday += isleap(year) ?
DAYSPERLYEAR :
DAYSPERNYEAR;
} }
if (yday >= bot) { if (*format == 'V')
w = 1 + ((yday - bot) / pt = _conv(w, "%02d",
DAYSPERWEEK); pt, ptlim);
break; else if (*format == 'g')
{
*warnp = IN_ALL;
pt = _conv(year % 100, "%02d",
pt, ptlim);
} }
--year; else
yday += isleap(year) ? pt = _conv(year, "%04d",
DAYSPERLYEAR : pt, ptlim);
DAYSPERNYEAR;
} }
if (*format == 'V')
pt = _conv(w, "%02d",
pt, ptlim);
else if (*format == 'g') {
*warnp = IN_ALL;
pt = _conv(year % 100, "%02d",
pt, ptlim);
} else pt = _conv(year, "%04d",
pt, ptlim);
}
continue;
case 'v':
/*
** From Arnold Robbins' strftime version 3.0:
** "date as dd-bbb-YYYY"
** (ado, 1993-05-24)
*/
pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
continue;
case 'W':
pt = _conv((t->tm_yday + DAYSPERWEEK -
(t->tm_wday ?
(t->tm_wday - 1) :
(DAYSPERWEEK - 1))) / DAYSPERWEEK,
"%02d", pt, ptlim);
continue;
case 'w':
pt = _conv(t->tm_wday, "%d", pt, ptlim);
continue;
case 'X':
pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
continue;
case 'x':
{
int warn2 = IN_SOME;
pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
*warnp = warn2;
}
continue;
case 'y':
*warnp = IN_ALL;
pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
"%02d", pt, ptlim);
continue;
case 'Y':
pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
pt, ptlim);
continue;
case 'Z':
if (t->tm_zone != NULL)
pt = _add(t->tm_zone, pt, ptlim);
/*
** C99 says that %Z must be replaced by the
** empty string if the time zone is not
** determinable.
*/
continue;
case 'z':
{
int diff;
char const * sign;
if (t->tm_isdst < 0)
continue; continue;
diff = t->tm_gmtoff; case 'v':
if (diff < 0) {
sign = "-"; /*
diff = -diff; * * From Arnold Robbins' strftime version 3.0: *
} else sign = "+"; * "date as dd-bbb-YYYY" * (ado, 1993-05-24)
pt = _add(sign, pt, ptlim); */
diff /= 60; pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
pt = _conv((diff/60)*100 + diff%60, continue;
"%04d", pt, ptlim); case 'W':
} pt = _conv((t->tm_yday + DAYSPERWEEK -
continue; (t->tm_wday ?
case '+': (t->tm_wday - 1) :
pt = _fmt(Locale->date_fmt, t, pt, ptlim, (DAYSPERWEEK - 1))) / DAYSPERWEEK,
warnp); "%02d", pt, ptlim);
continue; continue;
case '%': case 'w':
/* pt = _conv(t->tm_wday, "%d", pt, ptlim);
** X311J/88-090 (4.12.3.5): if conversion char is continue;
** undefined, behavior is undefined. Print out the case 'X':
** character itself as printf(3) also does. pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
*/ continue;
default: case 'x':
break; {
int warn2 = IN_SOME;
pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
*warnp = warn2;
}
continue;
case 'y':
*warnp = IN_ALL;
pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
"%02d", pt, ptlim);
continue;
case 'Y':
pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
pt, ptlim);
continue;
case 'Z':
if (t->tm_zone != NULL)
pt = _add(t->tm_zone, pt, ptlim);
/*
* * C99 says that %Z must be replaced by the * empty
* string if the time zone is not * determinable.
*/
continue;
case 'z':
{
int diff;
char const *sign;
if (t->tm_isdst < 0)
continue;
diff = t->tm_gmtoff;
if (diff < 0)
{
sign = "-";
diff = -diff;
}
else
sign = "+";
pt = _add(sign, pt, ptlim);
diff /= 60;
pt = _conv((diff / 60) * 100 + diff % 60,
"%04d", pt, ptlim);
}
continue;
case '+':
pt = _fmt(Locale->date_fmt, t, pt, ptlim,
warnp);
continue;
case '%':
/*
* * X311J/88-090 (4.12.3.5): if conversion char is *
* undefined, behavior is undefined. Print out the *
* character itself as printf(3) also does.
*/
default:
break;
} }
} }
if (pt == ptlim) if (pt == ptlim)
@ -464,15 +473,17 @@ label:
return pt; return pt;
} }
static char * _conv(const int n, const char *format, char *pt, const char *ptlim) static char *
_conv(const int n, const char *format, char *pt, const char *ptlim)
{ {
char buf[INT_STRLEN_MAXIMUM(int) + 1]; char buf[INT_STRLEN_MAXIMUM(int) +1];
(void) sprintf(buf, format, n); (void) sprintf(buf, format, n);
return _add(buf, pt, ptlim); return _add(buf, pt, ptlim);
} }
static char *_add(const char *str, char *pt, const char *ptlim) static char *
_add(const char *str, char *pt, const char *ptlim)
{ {
while (pt < ptlim && (*pt = *str++) != '\0') while (pt < ptlim && (*pt = *str++) != '\0')
++pt; ++pt;

View File

@ -26,17 +26,20 @@
** Each file begins with. . . ** Each file begins with. . .
*/ */
#define TZ_MAGIC "TZif" #define TZ_MAGIC "TZif"
struct tzhead { struct tzhead
char tzh_magic[4]; /* TZ_MAGIC */ {
char tzh_reserved[16]; /* reserved for future use */ char tzh_magic[4]; /* TZ_MAGIC */
char tzh_ttisgmtcnt[4]; /* coded number of trans. time flags */ char tzh_reserved[16]; /* reserved for future use */
char tzh_ttisstdcnt[4]; /* coded number of trans. time flags */ char tzh_ttisgmtcnt[4]; /* coded number of trans. time
char tzh_leapcnt[4]; /* coded number of leap seconds */ * flags */
char tzh_timecnt[4]; /* coded number of transition times */ char tzh_ttisstdcnt[4]; /* coded number of trans. time
char tzh_typecnt[4]; /* coded number of local time types */ * flags */
char tzh_charcnt[4]; /* coded number of abbr. chars */ char tzh_leapcnt[4]; /* coded number of leap seconds */
char tzh_timecnt[4]; /* coded number of transition times */
char tzh_typecnt[4]; /* coded number of local time types */
char tzh_charcnt[4]; /* coded number of abbr. chars */
}; };
/* /*
@ -46,8 +49,8 @@ struct tzhead {
** tzh_timecnt (unsigned char)s types of local time starting at above ** tzh_timecnt (unsigned char)s types of local time starting at above
** tzh_typecnt repetitions of ** tzh_typecnt repetitions of
** one (char [4]) coded UTC offset in seconds ** one (char [4]) coded UTC offset in seconds
** one (unsigned char) used to set tm_isdst ** one (unsigned char) used to set tm_isdst
** one (unsigned char) that's an abbreviation list index ** one (unsigned char) that's an abbreviation list index
** tzh_charcnt (char)s '\0'-terminated zone abbreviations ** tzh_charcnt (char)s '\0'-terminated zone abbreviations
** tzh_leapcnt repetitions of ** tzh_leapcnt repetitions of
** one (char [4]) coded leap second transition times ** one (char [4]) coded leap second transition times
@ -77,33 +80,36 @@ struct tzhead {
*/ */
#define TZ_MAX_TIMES 370 #define TZ_MAX_TIMES 370
#define TZ_MAX_TYPES 256 /* Limited by what (unsigned char)'s can hold */ #define TZ_MAX_TYPES 256 /* Limited by what (unsigned char)'s can
* hold */
#define TZ_MAX_CHARS 50 /* Maximum number of abbreviation characters */ #define TZ_MAX_CHARS 50 /* Maximum number of abbreviation
/* (limited by what unsigned chars can hold) */ * characters */
/* (limited by what unsigned chars can hold) */
#define TZ_MAX_LEAPS 50 /* Maximum number of leap second corrections */ #define TZ_MAX_LEAPS 50 /* Maximum number of leap second
* corrections */
#define SECSPERMIN 60 #define SECSPERMIN 60
#define MINSPERHOUR 60 #define MINSPERHOUR 60
#define HOURSPERDAY 24 #define HOURSPERDAY 24
#define DAYSPERWEEK 7 #define DAYSPERWEEK 7
#define DAYSPERNYEAR 365 #define DAYSPERNYEAR 365
#define DAYSPERLYEAR 366 #define DAYSPERLYEAR 366
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY) #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
#define MONSPERYEAR 12 #define MONSPERYEAR 12
#define TM_SUNDAY 0 #define TM_SUNDAY 0
#define TM_MONDAY 1 #define TM_MONDAY 1
#define TM_TUESDAY 2 #define TM_TUESDAY 2
#define TM_WEDNESDAY 3 #define TM_WEDNESDAY 3
#define TM_THURSDAY 4 #define TM_THURSDAY 4
#define TM_FRIDAY 5 #define TM_FRIDAY 5
#define TM_SATURDAY 6 #define TM_SATURDAY 6
#define TM_JANUARY 0 #define TM_JANUARY 0
#define TM_FEBRUARY 1 #define TM_FEBRUARY 1
#define TM_MARCH 2 #define TM_MARCH 2
#define TM_APRIL 3 #define TM_APRIL 3
#define TM_MAY 4 #define TM_MAY 4
@ -112,8 +118,8 @@ struct tzhead {
#define TM_AUGUST 7 #define TM_AUGUST 7
#define TM_SEPTEMBER 8 #define TM_SEPTEMBER 8
#define TM_OCTOBER 9 #define TM_OCTOBER 9
#define TM_NOVEMBER 10 #define TM_NOVEMBER 10
#define TM_DECEMBER 11 #define TM_DECEMBER 11
#define TM_YEAR_BASE 1900 #define TM_YEAR_BASE 1900
@ -127,4 +133,4 @@ struct tzhead {
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
#endif /* !defined TZFILE_H */ #endif /* !defined TZFILE_H */

File diff suppressed because it is too large Load Diff