[mod.sources] date - formatted date program

sources-request@panda.UUCP (02/12/86)

Mod.sources:  Volume 3, Issue 122
Submitted by: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>


---8<-------CUT--HERE------8<-------CUT--HERE-------8<-------CUT--HERE------8<-
#!/bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #!/bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with sh (not csh) to create the files:
#          date.1
#          date.c
#
# Created: Tue Feb 11 14:13:38 MET 1986
# Creator: DIDELOT Andre (University of Geneva)
 
export PATH
PATH=/bin:$PATH
 
if [ -f 'date.1' ]
then
    echo ***WARNING "'date.1'" exists, not remplaced
else
    echo extracting "'date.1'" 1>&2
    sed -e 's/^X//' > 'date.1' <<'---EOF(date.1)---'
X.TH DATE l "6 August 1984"
X.UC 4
X.SH NAME
Xdate \- formatted output of current date
X.SH SYNOPSIS
X.B date
X[ 
X.B \-lf
X]
X[ [\-]
X.B format variables
X]
X.SH DESCRIPTION
XWithout any argument,
X.I date
Xoutput use ASCII format.
XThe
X.B \-f
Xoption stand for french output.
XThe
X.B \-l
Xoption stand for long output, either in english or in french.
X.PP
XThe string
X.I format
Xcontains a format like that used by the C subroutine
X.I printf
X; all special char conventions are recognized.
XThe string would have better to be quoted to prevent any further shell
Xinterpretation.
XAn optionnal single \- allow use of formats beginning with a \-.
X.PP
X.I Variables
Xis a list of single letters, either comma or blank separated, or even
Xconcatened alltogether; valid names and their meaning are :
X.TP 10
Xd, j
Xday of the week.
X.TP 10
XM
Xmonth.
X.TP 10
XD, J
Xday of the month.
X.TP 10
Xh
Xhours.
X.TP 10
Xm
Xminutes.
X.TP 10
Xs
Xseconds.
X.TP 10
XY, A
Xyear.
X.SH SEE ALSO
Xctime, localtime, gmtime, asctime, timezone (3)
X.SH AUTHOR
XA. Didelot
X.SH BUGS
XNo way to specify letter's case using format.
---EOF(date.1)---
    size=`wc -c date.1 | awk '{print \$1}'`
    if [ 1050 -ne $size ]
    then
        echo ***WARNING "'date.1'" bad checksum
    fi
fi
 
if [ -f 'date.c' ]
then
    echo ***WARNING "'date.c'" exists, not remplaced
else
    echo extracting "'date.c'" 1>&2
    sed -e 's/^X//' > 'date.c' <<'---EOF(date.c)---'
X#include <stdio.h>
X#include <sys/time.h>
X
Xchar *dayshort[] = {
X	"Mon",
X	"Tue",
X	"Wed",
X	"Thu",
X	"Fri",
X	"Sat",
X	"Sun"
X};
X
Xchar *monthshort[] = {
X	"Jan",
X	"Feb",
X	"Mar",
X	"Apr",
X	"May",
X	"Jun",
X	"Jul",
X	"Aug",
X	"Sep",
X	"Oct",
X	"Nov",
X	"Dec"
X};
X
Xchar *days[] = {
X	"monday",
X	"tuesday",
X	"wednesday",
X	"thursday",
X	"friday",
X	"saturday",
X	"sunday"
X};
X
Xchar *months[] = {
X	"january",
X	"february",
X	"march",
X	"april",
X	"may",
X	"june",
X	"july",
X	"august",
X	"september",
X	"october",
X	"november",
X	"december"
X};
X
Xchar *jrs[] = {
X	"lun",
X	"mar",
X	"mer",
X	"jeu",
X	"ven",
X	"sam",
X	"dim"
X};
X
Xchar *ms[] = {
X	"jan",
X	"fev",
X	"mar",
X	"avr",
X	"mai",
X	"juin",
X	"juil",
X	"aou",
X	"sep",
X	"oct",
X	"nov",
X	"dec"
X};
X
Xchar *jours[] = {
X	"lundi",
X	"mardi",
X	"mercredi",
X	"jeudi",
X	"vendredi",
X	"samedi",
X	"dimanche"
X};
X
Xchar *mois[] = {
X	"janvier",
X	"fevrier",
X	"mars",
X	"avril",
X	"mai",
X	"juin",
X	"juillet",
X	"aout",
X	"septembre",
X	"octobre",
X	"novembre",
X	"decembre"
X};
X
Xmain( argc, argv)
X
Xint	argc;
Xchar	**argv;
X
X{	long	l;
X
X	int 	i, j,
X		fflg, lflg,
X		iday, imonth;
X
X	char	*word[7],
X		format[80],
X		*command,
X		*date,
X		day[15],
X		month[15],
X		daymonth[3],
X		hour[3],
X		minute[3],
X		second[3],
X		year[5];
X
X/* get date				*/
X
X	time(&l);
X	date = ctime(&l);
X
X/* print date and exit, if no argument	*/
X
X	if (argc == 1) {
X		printf( "%s", date);
X		exit(0);
X	}
X
X/* save name of command			*/
X
X	command = argv[0];
X
X/* get options if any			*/
X
X	argc--;	argv++;
X	lflg = fflg = 0;
X	while(argv[0][0] == '-' && argv[0][1]) {
X	    for( i=1; argv[0][i]; i++)
X		switch(argv[0][i]) {
X
X		case 'f':
X			fflg++;
X			break;
X
X		case 'l':
X			lflg++;
X			break;
X
X		default:
X			fprintf( stderr,
X			"%s : bad option %c\n", command, argv[0][i]);
X		}
X	    argc--; argv++;
X	}
X	if (argv[0][0] == '-') {
X	    argc--; argv++;
X	}
X
X/* initialize day, month, ...		*/
X
X	substrcpy( date,  0,  2, day);
X	substrcpy( date,  4,  6, month);
X	if (date[8] == ' ') {
X	    daymonth[0] = date[9];
X	    daymonth[1] = '\0';
X	    }
X	else
X	    substrcpy( date,  8,  9, daymonth);
X	substrcpy( date, 11, 12, hour);
X	substrcpy( date, 14, 15, minute);
X	substrcpy( date, 17, 18, second);
X	substrcpy( date, 20, 23, year);
X
X
X/* compute iday, imonth			*/
X
X	for( iday=0; iday<7; iday++) 
X		if (!strncmp( dayshort[iday], day, 3)) break;
X	for( imonth=0; imonth<12; imonth++) 
X		if (!strncmp( monthshort[imonth], month, 3)) break;
X
X/* look for french and/or long output	*/
X
X	if (fflg)
X	    if (lflg) {
X		strcpy( day,   jours[iday]);
X		strcpy( month, mois[imonth]);
X		}
X	    else {
X		strcpy( day,   jrs[iday]);
X		strcpy( month, ms[imonth]);
X	    }
X	else
X	    if (lflg) {
X		strcpy( day,   days[iday]);
X		strcpy( month, months[imonth]);
X	    }
X
X/* check number of arguments		*/
X
X	if (argc == 1 || argc > 8) {
X		fprintf( stderr,
X		"%s : wrong number of arguments\n", command);
X		exit(1);
X	}
X
X	if (argc) {
X
X/* translate C convention like \n, \t, ...
X   into single char.			*/
X
X	    for ( i=0, j=0; argv[0][i]; i++, j++) {
X		format[j] = argv[0][i];
X		if (argv[0][i] == '\\') 
X			switch (argv[0][++i]) {
X
X			case 'n':
X				format[j] = '\n';
X				break;
X
X			case 't':
X				format[j] = '\t';
X				break;
X
X			case 'b':
X				format[j] = '\b';
X				break;
X
X			case 'r':
X				format[j] = '\r';
X				break;
X
X			case 'f':
X				format[j] = '\f';
X				break;
X
X			case '\\':
X				break;
X
X			default:
X				format[++j] = argv[0][i];
X			}
X	    }
X	    format[j] = '\0';
X	    argc--; argv++;
X
X/* order arguments			*/
X
X	    for( i=0; i<7; i++) word[i] = '\0';
X	    j = 0;
X	    while (argc) {
X		for( i=0; argv[0][i]; i++)
X			switch(argv[0][i]) {
X
X			case 'd':
X			case 'j':
X				word[j++] = day;
X				break;
X
X			case 'M':
X				word[j++] = month;
X				break;
X
X			case 'D':
X			case 'J':
X				word[j++] = daymonth;
X				break;
X
X			case 'h':
X				word[j++] = hour;
X				break;
X
X			case 'm':
X				word[j++] = minute;
X				break;
X
X			case 's':
X				word[j++] = second;
X				break;
X
X			case 'A':	
X			case 'Y':	
X				word[j++] = year;
X				break;
X
X			case ',':
X				break;
X
X			default:	
X				fprintf( stderr,
X				"%s : bad argument %c\n", command, argv[0][i]);
X			}
X		argc--;	argv++;
X	    }
X
X/* output date according to format	*/
X
X	    printf( format, word[0], word[1], word[2],
X		    word[3], word[4], word[5], word[6]);
X	    }
X
X	else {
X
X	    strcpy( format, "%s %s %s %s   %s:%s:%s\n");
X	    if (fflg)
X		printf( format, day, daymonth, month, year, hour, minute, second);
X	    else
X		printf( format, day, month, daymonth, year, hour, minute, second);
X	}
X}
X
X
X/* copy string str1[n..m] into str2	*/
X
Xsubstrcpy( str1, n, m, str2)
X
Xint	n, m;
Xchar	*str1, *str2;
X
X{
X	m++;
X	if ( n >= 0 && m >= n ) {
X		while( n-- && *str1++ && m--);
X		while( m-- && ( *str2++ = *str1++ ));
X	}
X	*str2='\0';
X}
---EOF(date.c)---
    size=`wc -c date.c | awk '{print \$1}'`
    if [ 4636 -ne $size ]
    then
        echo ***WARNING "'date.c'" bad checksum
    fi
fi
---8<-------CUT--HERE------8<-------CUT--HERE-------8<-------CUT--HERE------8<-
	Andre DIDELOT				CHUNET: andre@cui.unige.chunet

MAIL:	Centre Universitaire d'Informatique	UUCP:	mcvax!cernvax!cui!andre
	Universite de Geneve				mcvax!cernvax!cui!root
	Rue du General Dufour 24
	CH - 1211 GENEVE 4			BITNET: DIDELOT@CGEUGE51
	SWITZERLAND					SYSTEM@CGEUGE51