#include "V.h"
/*
*   symtime [options] [time]
*
* Produce a symbolic version of a Unix timestamp.  The optional arg is
* the number to be converted; if omitted, time() is called to give the
* current time.
*
* An arg starting with a letter is assumed to be a format, in which the
* following letters are significant:
*
* Options may start with '-' ("disable") or '+' ("enable").
*
* +c    Give the century in dates. (Implies +y)
*
* +d    Give the day only, in the form "YYMMDD".
*
* +d<c> Give the day in the form "CCYY<c>MM<c>DD", where <c> is
*       some separator string.  -d/ and -d- are two common choices.
*
* +t    Give the time only, in the form "hhmmss".
*
* +t<c> Give the time in the form "hh<c>mm<c>ss", where <c> is some
*       separator string.  -c: and -c. are two common choices.
*
* +l    Convert to local time.
*
* +u    Convert to UT.
*
* +y    Give the year in dates.
* -y    Don't give the year in dates. (Implies -c)
*
* +z    Allow zero times.
* -z<c> Show zero times to the char (string) <c>.
*
*/

Flag   showcen  = 1;	/* Show the century */
Flag   showyr   = 1;	/* Show the year */
Flag   showdate = 0;	/* Show the date */
Flag   showtime = 0;	/* Show the time */
Flag   zerotime = 1;	/* Allow zero times */
Flag   formatopt = 0;	/* We got a +d or +t option */
Flag   utimefl = 0;		/* Universal time */
Flag   ltimefl = 0;		/* Local time */
Flag   centflg = 0;		/* A value was given on the command line */
Flag   dateflg = 0;		/* A value was given on the command line */
Flag   tzopt = 0;		/* We got a +l or +u option */

char* datesep = "";
char* timesep = "";
char* zerostr = "";

main(ac,av)
	char** av;
{	int    a, n;
	int    c0, c1;
	U32    ts;
	TIME*  tp;
	char   buf[100];
	char   datebuf[100];
	char   timebuf[100];
	ac = Vinit(ac,av);

	for (a = 1; a < ac; a++) {
		Switch(c0 = av[a][0]) {
		case '-':
		case '+':
			Switch(c1 = av[a][1]) {
			case 'c': case 'C':	/* Give century in dates */
				if (showcen = (c0 == '+')) {
					showyr = 1;
				}
				break;
			case 'd': case 'D':	/* Date only */
				showdate = (c0 == '+');
				datesep = av[a] + 2;
				V3 "Show date as CCYY%sMM%sDD.",datesep,datesep D;
				V3 "Show date." D;
				formatopt = 1;
				break;
			case 'l': case 'L':	/* Local time */
				ltimefl = (c0 == '+');
				V3 "Local time." D;
				tzopt = 1;
				break;
			case 't': case 'T':	/* Time of day only */
				showtime = (c0 == '+');
				timesep = av[a] + 2;
				V3 "Show time as hh%smm%sss.",timesep,timesep D;
				formatopt = 1;
				break;
			case 'u': case 'U':	/* Universal Time */
				utimefl = (c0 == '+');
				V3 "Universal Time." D;
				tzopt = 1;
				break;
			case 'y': case 'Y':	/* Give year in dates */
				if (!(showyr = (c0 == '+'))) {
					showcen = 0;
				}
				break;
			case 'z': case 'Z':	/* Handling of zero times */
				zerotime = (c0 == '+');
				zerostr = av[a] + 2;
				V3 "Show zero as %s",Dsps(zerostr,-1) D;
				break;
			default:
				V2 "Unknown option %s ignored.",Dsps(av[a],-1) D;
				break;
			}
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			sscanf(av[a],"%u",&ts);
			V3 "Timestamp=%08X=%d.",ts,ts D;
			dateflg = 1;
			break;
		default:
			V2 "Unknown arg %d = %s ignored.",a,Dsps(av[a],-1) D;
			break;
		}
	}
	if (!formatopt) {
		showdate = showtime = 1;
		V3 "Show date and time by default." D;
	}
	if (!tzopt) {
		ltimefl = 1;
		V3 "Show local time by default." D;
	}
	if (!dateflg) {
		UnixTime(&ts);
		V3 "Current time=%08X=%d.",ts,ts D;
	}
	if (ltimefl) {
		V3 "Get local time ..." D;
		tp = localtime(&ts);
	} else {
		V3 "Get universal time ..." D;
		tp = gmtime(&ts);
	}
	buf[0] = 0;
	if (ts || zerotime) {
		V5 "Convert ts=%d zerotime=%d.",ts,zerotime D;
		if (showcen) {
			sprintf(datebuf,"%02d", (tp->tm_year+1900) / 100);
			strcat(buf,datebuf);
		}
		if (showyr) {
			sprintf(datebuf,"%02d%s", tp->tm_year,datesep);
			strcat(buf,datebuf);
		}
		if (showdate) {
			sprintf(datebuf,"%02d%s%02d",
				tp->tm_mon+1, datesep,
				tp->tm_mday);
			strcat(buf,datebuf);
		}
		if (showtime) {
			if (buf[0]) strcat(buf," ");
			sprintf(timebuf,"%02d%s%02d%s%02d",
				tp->tm_hour, timesep,
				tp->tm_min, timesep,
				tp->tm_sec);
			strcat(buf,timebuf);
		}
	} else {
		V5 "Zero: ts=%d zerotime=%d.",ts,zerotime D;
		if (showdate) {
			strcat(buf,zerostr);
		}
		if (showtime) {
			if (buf[0]) strcat(buf," ");
			strcat(buf,zerostr);
		}
	}
	printf("%s\n",buf);
	Exit(0);
}
