Re^2: Getting UNIX time from the shell

Maarten Litmaath maart at cs.vu.nl
Thu Jun 15 06:11:40 AEST 1989


gph at hpsemc.HP.COM (Paul Houtz) writes:
\...
[comments deleted]

\#!/bin/ksh         
\
\clock () {
\CURTIME=`date | awk '{print $4}'`
\print -n $CURTIME |
\	awk 'BEGIN {FS = ":" }{if ($1 > 12) print $1-12 ":" $2 " pm" }
\		{if ($1 <= 12) print $1 ":" $2 " am"}'
\}
\
\time=`clock`
\
\day=`date | awk '{print $1 " " $2 " " $3 }'`
\
\print -n "$day  $time" 

1)	I reformatted the `print ... | awk ...' line: it was longer than 80
	characters, totally unnecessary :-(
2)	The following clock() avoids invoking awk twice, furthermore the
	script is enhanced:

	clock()
	{
		# in ksh the LOCAL parameters are set; in sh the GLOBAL ones
		set `date`
		echo $4 | awk '{
			FS = ":"
			if ($1 > 12)
				print $1 - 12 ":" $2 " pm"
			else
				print $1 ":" $2 " am"
		}'
	}

3)	I know Paul probably needs the clock() function elsewhere, but
	compare anyway:

	eval `
		IFS=' :'
		set \`date\`
		echo day="'$1 $2 $3'"
		h=\`echo $4 12 - p | dc\`
		case $h in
		-* | 0)
			echo time="'$4:$5 am'"
			;;
		*)
			echo time="'$h:$5 pm'"
		esac
	`

	echo "$day  $time"
-- 
"I HATE arbitrary limits, especially when |Maarten Litmaath @ VU Amsterdam:
   they're small."  (Stephen Savitzky)    |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list