/* sdate.c
*
* Gets the current date and time from the system, and emits the short form
* mm/dd/yy as a string.
*
* -x: emit extended terse form mm/dd/yy hh:mm:ss
* -t: emit time in the form hh:mm
*
* -w: emit week day as 0-6 for sun-sat
* simultaneously -C: Fri e.g.
* -V: FRI
* -v: fri
*
* -d: emit zero padded day of the month as 01-31
* -h: emit zero padded hour of the day 00-23
* -M: emit zero padded minute of the hour 00-59
*
* With option -l, emits current date suitable for a letter
*/
#include <stdio.hgt;
#include <ascii.hgt;
#include <stdfun.hgt;
#include <string.hgt;
#include <sys/types.hgt;
#include <time.hgt;
#define OPTSTRING "lxCMymnstdhvVwST:"
#define EMIT_DATE 1
#define EMIT_TIME 2
#define EMIT_DATE_TIME 3
#define EMIT_WKDAY 4
#define FALSE 0
#define TRUE 1
#define SHORTDATESZ 3
#define DATESZ 10
char *Month[] = {
"January\0",
"February\0",
"March\0",
"April\0",
"May\0",
"June\0",
"July\0",
"August\0",
"September\0",
"October\0",
"November\0",
"December\0"
};
char *day[] = {
"sun\0",
"mon\0",
"tue\0",
"wed\0",
"thu\0",
"fri\0",
"sat\0"
};
struct tm tm0, *ptm0;
long sysclocktime;
int verbose, startime, shorterdate, emityear, emitmonth;
int emitmonthday, wkday, emithour, emitminute;
int letterdate;
static char _sccsid[] = { " sdate.c 1.5 10/4/97 " };
int main( argc, argv )
int argc;
char *argv[];
{
extern int toupper();
extern int getopt();
extern long time();
char *sputdate(), *addmins(), *stoupper();
extern int optind;
extern char *optarg; /* Captures arguments to options */
int c_date[ SHORTDATESZ ];
char c_datestr[ DATESZ ];
int adv_mins;
int emit;
char end;
int c_opt;
/* Defaults */
emit = EMIT_DATE;
startime = FALSE;
letterdate = FALSE;
shorterdate = FALSE;
emityear = FALSE;
emitmonth = FALSE;
emitmonthday = FALSE;
emithour = FALSE;
emitminute = FALSE;
verbose = FALSE;
end = NUL;
while( ( c_opt = getopt( argc, argv, OPTSTRING ) ) != EOF ) {
switch( c_opt ) {
case 'l':
/* -l */
letterdate = TRUE;
break;
case 'm':
/* -m */
emitmonth = TRUE;
break;
case 'd':
/* -d */
emitmonthday = TRUE;
break;
case 'y':
/* -y */
emityear = TRUE;
break;
case 'n':
/* -n */
end = LF;
break;
case 'S':
/* -S: Use "star time" */
startime = TRUE;
break;
case 's':
/* -s: Use "shorter date mm[/]dd" */
shorterdate = TRUE;
break;
case 'M':
/* -M */
emit = EMIT_TIME;
emitminute = TRUE;
break;
case 'x':
/* -x */
emit = EMIT_DATE_TIME;
break;
case 'h':
/* -h */
emit = EMIT_TIME;
emithour = TRUE;
break;
case 't':
/* -t */
emit = EMIT_TIME;
adv_mins = 0;
break;
case 'w':
/* -w */
emit = EMIT_WKDAY;
break;
case 'v':
/* -v */
verbose = 1;
break;
case 'V':
/* -V */
verbose = 2;
break;
case 'C':
/* -C */
verbose = 3;
break;
case 'T':
/* -T arg */
emit = EMIT_TIME;
sscanf( optarg, "%d", &adv_mins );
break;
default:
fprintf( stderr, "%s: Bad Option -%c\n", argv[0], c_opt );
fprintf( stderr, "Usage: %s -[%s]\n", argv[0], OPTSTRING );
exit( 1 );
}
}
ptm0 = &tm0;
/* One should always give the explicit null argument, since the old
* syntax time( &clock ) is still operative
*/
sysclocktime = time( (long *) 0 );
ptm0 = localtime( &sysclocktime );
if( emit == EMIT_DATE ) {
/* Globals */
c_date[0] = ptm0-gt;tm_mon;
c_date[1] = ptm0-gt;tm_mday;
c_date[2] = ptm0-gt;tm_year;
/* sputdate() corrects System c_date[] so worldly date
* is printed into character array c_datestr.
*/
if( emitmonthday ) {
printf( "%02d", c_date[ 1 ] );
if( end == LF )
putchar( LF );
exit( 0 );
}
else if( letterdate ) {
printf( "%s %d, 19%d\n", Month[ c_date[ 0 ] ], c_date[ 1 ], c_date[ 2 ] );
exit( 0 );
}
else {
sputdate( c_date, c_datestr, end );
}
}
else if( emit == EMIT_TIME ) {
/* Globals */
c_date[0] = ptm0-gt;tm_hour;
c_date[1] = ptm0-gt;tm_min;
if( emithour ) {
printf( "%02d", c_date[0] );
if( end == LF )
putchar( LF );
exit( 0 );
}
else if( emitminute ) {
printf( "%02d", c_date[1] );
if( end == LF )
putchar( LF );
exit( 0 );
}
else {
addmins( c_date, c_datestr, adv_mins, end );
}
}
else if( emit == EMIT_DATE_TIME ) {
printf( "%02d/%02d/%02d %02d:%02d:%02d",
ptm0-gt;tm_mon + 1,
ptm0-gt;tm_mday,
ptm0-gt;tm_year,
ptm0-gt;tm_hour,
ptm0-gt;tm_min,
ptm0-gt;tm_sec );
if( end == LF )
putchar( LF );
exit( 0 );
}
else {
/* emit WEEK_DAY */
wkday = ptm0-gt;tm_wday;
if( verbose == 0 ) {
sprintf( c_datestr, "%d%c", wkday, end );
}
else if( verbose == 1 ) {
sprintf( c_datestr, "%s%c", day[ wkday ], end );
}
else if( verbose == 2 ) {
sprintf( c_datestr, "%s%c", stoupper( day[ wkday ], DATESZ ), end );
}
else if( verbose == 3 ) {
sprintf( c_datestr, "%s%c", day[ wkday ], end );
c_datestr[0] = (char)toupper( c_datestr[0] );
}
}
printf( "%s", c_datestr );
exit( 0 );
return( 0 );
} /* end main() */
/* Put a zero padded date into string.
* Assumed that string is at least of size DATELEN.
* Since this is meant for printing to the world
* we adjust the month number to the worldly number if the address of
* the integer array is the address of the global c_date[] which contains
* the date in system form.
* 'end' is the prescribed char preceding the terminal NUL.
* Called by gtsysdate() to write global c_datestr[].
*/
char *sputdate( d, l, end ) /* put 0-padded short date to string */
int *d;
char *l;
char end;
{
if( emityear ) {
sprintf( l, "%02d%c", d[2], end );
return( l );
}
if( emitmonth ) {
sprintf( l, "%02d%c", d[0] + 1, end );
return( l );
}
if( startime ) {
if( shorterdate ) {
sprintf( l, "%02d%02d%c", d[0] + 1, d[1], end );
}
else {
sprintf( l, "%02d%02d%02d%c", d[0] + 1, d[1], d[2], end );
}
}
else {
if( shorterdate ) {
sprintf( l, "%02d/%02d%c", d[0] + 1, d[1], end );
}
else {
sprintf( l, "%02d/%02d/%02d%c", d[0] + 1, d[1], d[2], end );
}
}
return( l );
}
char *addmins( t, tstr, advm, end )
int *t;
char *tstr;
int advm;
char end;
{
int hrs, mins;
hrs = t[ 0 ];
mins = t[ 1 ];
while( (mins + advm) gt;= 60 ) {
++hrs;
advm -= 60;
}
mins +=advm;
if( startime ) {
sprintf( tstr, "%02d%02d%c", hrs, mins, end );
}
else {
sprintf( tstr, "%02d:%02d%c", hrs, mins, end );
}
return( tstr );
}
char *stoupper( str, sz ) /* String to upper */
char *str;
int sz;
{
extern int toupper();
--sz;
while( sz-- )
str[ sz ] = (char)toupper( str[ sz ] );
return( str );
}
Return to Home Page
Return to Metayoga Page
Return to C Language Page
The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/sdate.html
Created: 1997
Last Updated: May 28, 2000
Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL