tmxfmt.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* AT&T Research
*
* Time_t conversion support
*/
#include <tmx.h>
#include <ctype.h>
/*
* format n with padding p into s
* return end of s
*
* p: <0 blank padding
* 0 no padding
* >0 0 padding
*/
static char*
{
char* b;
if (w)
{
while (w > p)
{
p++;
n *= 10;
}
else if (w > p)
p = w;
}
switch (pad)
{
case '-':
p = 0;
break;
case '_':
if (p > 0)
p = -p;
break;
case '0':
if (p < 0)
p = -p;
break;
}
b = s;
if (p > 0)
s += sfsprintf(s, e - s, "%0*lu", p, n);
else if (p < 0)
s += sfsprintf(s, e - s, "%*lu", -p, n);
else
s += sfsprintf(s, e - s, "%lu", n);
if (w && (s - b) > w)
*(s = b + w) = 0;
return s;
}
typedef struct Stack_s
{
char* format;
int delimiter;
} Stack_t;
/*
* format t into buf of length len
* end of buf is returned
*/
char*
{
register char* cp;
register char* ep;
register char* p;
register int n;
int c;
int i;
int flags;
int alt;
int pad;
int delimiter;
int width;
int prec;
int parts;
char* arg;
char* f;
const char* oformat;
char argbuf[256];
char fmt[32];
tmlocale();
delimiter = 0;
for (;;)
{
{
delimiter = 0;
break;
sp--;
continue;
}
if (c != '%')
{
*cp++ = c;
continue;
}
alt = 0;
arg = 0;
pad = 0;
width = 0;
prec = 0;
parts = 0;
for (;;)
{
switch (c = *format++)
{
case '_':
case '-':
pad = c;
continue;
case 'E':
case 'O':
break;
alt = c;
continue;
case '0':
if (!parts)
{
pad = c;
continue;
}
/*FALLTHROUGH*/
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
switch (parts)
{
case 0:
parts++;
/*FALLTHROUGH*/
case 1:
break;
case 2:
break;
}
continue;
case '.':
if (!parts++)
parts++;
continue;
case '(':
i = 1;
for (;;)
{
if (!(c = *format++))
{
format--;
break;
}
else if (c == '(')
i++;
else if (c == ')' && !--i)
break;
*arg++ = c;
}
*arg = 0;
continue;
default:
break;
}
break;
}
switch (c)
{
case 0:
format--;
continue;
case '%':
*cp++ = '%';
continue;
case '?':
else if (!*format)
continue;
case 'a': /* abbreviated day of week name */
goto index;
case 'A': /* day of week name */
goto index;
case 'b': /* abbreviated month name */
case 'h':
goto index;
case 'B': /* month name */
goto index;
case 'c': /* `ctime(3)' date sans newline */
goto push;
case 'C': /* 2 digit century */
continue;
case 'd': /* day of month */
continue;
case 'D': /* date */
goto push;
case 'e': /* blank padded day of month */
continue;
case 'f': /* (AST) OBSOLETE use %Qf */
p = "%Qf";
goto push;
case 'F': /* ISO 8601:2000 standard date format */
p = "%Y-%m-%d";
goto push;
case 'g': /* %V 2 digit year */
case 'G': /* %V 4 digit year */
{
n--;
}
{
n++;
}
if (c == 'g')
{
n %= 100;
c = 2;
}
else
c = 4;
continue;
case 'H': /* hour (0 - 23) */
continue;
case 'i': /* (AST) OBSOLETE use %QI */
p = "%QI";
goto push;
case 'I': /* hour (0 - 12) */
else if (n == 0) n = 12;
continue;
case 'j': /* Julian date (1 offset) */
continue;
case 'J': /* Julian date (0 offset) */
continue;
case 'k': /* (AST) OBSOLETE use %QD */
p = "%QD";
goto push;
case 'K': /* (AST) largest to smallest */
switch (alt)
{
case 'E':
break;
case 'O':
break;
default:
break;
}
goto push;
case 'l': /* (AST) OBSOLETE use %QL */
p = "%QL";
goto push;
case 'L': /* (AST) OBSOLETE use %Ql */
p = "%Ql";
goto push;
case 'm': /* month number */
continue;
case 'M': /* minutes */
continue;
case 'n':
*cp++ = '\n';
continue;
case 'N': /* (AST|GNU) nanosecond part */
continue;
#if 0
case 'o': /* (UNUSED) */
continue;
#endif
case 'p': /* meridian */
goto index;
case 'P': /* (AST|GNU) lower case meridian */
continue;
case 'q': /* (AST) OBSOLETE use %Qz */
p = "%Qz";
goto push;
case 'Q': /* (AST) %Q<alpha> or %Q<delim>recent<delim>distant<delim> */
if (c = *format)
{
format++;
if (isalpha(c))
{
switch (c)
{
case 'd': /* `ls -l' distant date */
goto push;
case 'D': /* `date(1)' date */
goto push;
case 'f': /* TM_DEFAULT override */
goto push;
case 'I': /* international `date(1)' date */
goto push;
case 'l': /* TM_DEFAULT */
goto push;
case 'L': /* `ls -l' date */
if (t)
{
now = tmxgettime();
{
goto push;
}
}
goto push;
case 'o': /* set options ( %([+-]flag...)o ) */
if (arg)
{
c = '+';
i = 0;
for (;;)
{
switch (*arg++)
{
case 0:
n = 0;
break;
case '=':
i = !i;
continue;
case '+':
case '-':
case '!':
c = *(arg - 1);
continue;
case 'l':
n = TM_LEAP;
break;
case 'n':
case 's':
n = TM_SUBSECOND;
break;
case 'u':
n = TM_UTC;
break;
default:
continue;
}
if (!n)
break;
/*
* right, the global state stinks
* but we respect its locale-like status
*/
if (c == '+')
{
if (!(flags & n))
{
flags |= n;
if (!i)
}
}
else if (flags & n)
{
flags &= ~n;
if (!i)
}
}
}
break;
case 'r': /* `ls -l' recent date */
goto push;
case 'z': /* time zone nation code */
{
goto string;
goto string;
}
break;
default:
format--;
break;
}
}
else
{
if (t)
{
now = tmxgettime();
}
else
p = (char*)format;
i = 0;
while (n = *format)
{
format++;
if (n == c)
{
if (!p)
p = (char*)format;
if (++i == 2)
goto push_delimiter;
}
}
}
}
continue;
case 'r':
goto push;
case 'R':
p = "%H:%M";
goto push;
case 's': /* (DEFACTO) seconds[.nanoseconds] since the epoch */
case '#':
now = t;
f = fmt;
*f++ = '%';
if (pad == '0')
*f++ = pad;
if (width)
if (parts > 1)
{
n = prec + 1;
cp += n;
}
continue;
case 'S': /* seconds */
{
p = ".%N";
goto push;
}
continue;
case 't':
*cp++ = '\t';
continue;
case 'T':
goto push;
case 'u': /* weekday number [1(Monday)-7] */
i = 7;
continue;
case 'U': /* week number, Sunday as first day */
continue;
#if 0
case 'v': /* (UNUSED) */
continue;
#endif
case 'V': /* ISO week number */
continue;
case 'W': /* week number, Monday as first day */
continue;
case 'w': /* weekday number [0(Sunday)-6] */
continue;
case 'x':
goto push;
case 'X':
goto push;
case 'y': /* year in the form yy */
continue;
case 'Y': /* year in the form ccyy */
continue;
case 'z': /* time zone west offset */
if (arg)
{
continue;
}
cp = tmpoff(cp, ep - cp, "", (flags & TM_UTC) ? 0 : tm->tm_zone->west + (tm->tm_isdst ? tm->tm_zone->dst : 0), pad == '_' ? -24 * 60 : 24 * 60);
continue;
case 'Z': /* time zone */
if (arg)
{
continue;
}
p = (flags & TM_UTC) ? tm_info.format[TM_UT] : tm->tm_isdst && tm->tm_zone->daylight ? tm->tm_zone->daylight : tm->tm_zone->standard;
goto string;
case '=': /* (AST) OBSOLETE use %([+-]flag...)Qo (old %=[=][+-]flag) */
format++;
*arg = 0;
goto options;
default:
*cp++ = '%';
*cp++ = c;
continue;
}
cp++;
continue;
c = '+';
i = 0;
for (;;)
{
switch (*arg++)
{
case 0:
n = 0;
break;
case '=':
i = !i;
continue;
case '+':
case '-':
case '!':
c = *(arg - 1);
continue;
case 'l':
n = TM_LEAP;
break;
case 'n':
case 's':
n = TM_SUBSECOND;
break;
case 'u':
n = TM_UTC;
break;
default:
continue;
}
if (!n)
break;
/*
* right, the global state stinks
* but we respect its locale-like status
*/
if (c == '+')
{
if (!(flags & n))
{
flags |= n;
if (!i)
}
}
else if (flags & n)
{
flags &= ~n;
if (!i)
}
}
continue;
push:
c = 0;
{
format = p;
delimiter = c;
sp++;
}
continue;
}
*cp = 0;
return cp;
}