[comp.unix.questions] Getting UNIX time from the shell

ijk@cbnewsh.ATT.COM (ihor.j.kinal) (06/12/89)

Is there any way that I can use the date command to display the
actual UNIX time???  

I have RTFM, and all the date options seem to display the
date in another format... it seems strange that  I can't get
the actual time [i.e., not have date perform any conversions].

YES, I KNOW I CAN DO THIS WITH A SIMPLE 7 LINE C PROGRAM - BUT
CAN I DO THIS WITH JUST SHELL COMMANDS??  Or possibly awk [old
awk please, since the newer awk hasn't been released yet to our customers]?

I'm using UNIX HP 5.2, on a HP 850. Pretty standard UNIX, with some
additional Berkley enhancements.

Thanks much,

Ihor Kinal
cbnewsh!ijk
{att!violin!ijk}

gph@hpsemc.HP.COM (Paul Houtz) (06/14/89)

I'll just give you a hack I have to put the time and date in a non-24 hour
format into variables.   I do this so I can use them to create dated filenames.


#!/bin/ksh         
#		This is a korn shell script.
#		Define a function "clock" which returns the clock time 
#		in am or pm
clock () {
#		Put the time part of date into $CURTIME.  Could be done with 
#		cut too...
CURTIME=`date | awk '{print $4}'`
#		Use awk again to change 13:00 to 1:00 pm
print -n $CURTIME | awk 'BEGIN {FS = ":" }{if ($1 > 12) print $1-12 ":" $2 " pm" }{if ($1 <= 12) print $1 ":" $2 " am"}'
}
#		Okay, now you want to invoke the function to put readable time 
#		into $time
time=`clock`
#		Then use awk again to get the day month and year into a 
#		separate variable called $day
day=`date | awk '{print $1 " " $2 " " $3 }'`
#		Then print them both
print -n "$day  $time" 
#		end of script

This is an unnecessarily complicated way of doing what you want.  I have
it set up this way because I want those particular variables created.  However,
it should give you the idea!!

Substitue "echo" for "print" everywhere except inside the awk commands, and
change the function into a separate script or here document, and you will
have a bourne shell script that works to.

Hope this helps!
Paul Houtz
HP Technology Access Center
10670 N. Tantau Avenue
Cupertino, Ca 95014
(408) 725-3864
hplabs!hpda!hpsemc!gph 
gph%hpsemc@hplabs.HP.COM

vijay@bradley.UUCP (06/14/89)

Ihor Kinal writes:-->
>/* ---------- "Getting UNIX time from the shell" ---------- */
>Is there any way that I can use the date command to display the
>actual UNIX time???  

   [...]

>YES, I KNOW I CAN DO THIS WITH A SIMPLE 7 LINE C PROGRAM - BUT
>CAN I DO THIS WITH JUST SHELL COMMANDS??  

   [...]

>Ihor Kinal
>cbnewsh!ijk
>{att!violin!ijk}

You do not need to write a C program to do that. A one liner
shell command will do it for you (In C Shell or Bourne Shell).
All you need is :
   
                  date | cut -f4,5 -d" "

That will give you the time. If it's a hassle having to
type that command whenever you want the time, add this to your
.profile :

           time=`date | cut -f4,5 -d" "`
           export time

or if you are using C shell, add this to your .login :

           setenv time `date | cut -f4,5 -d" "`  

Or alternatively you can create a shell file called time
with the command 

               date | cut -f4,5 -d" "

Then make that file executable and you will be set.
 
Take your pick......


=============================================================I 
  /    /                 E-mail: vijay@bradley.edu           I
 /    / *   *  ___               ...!uiucdcs!bradley!vijay   I
 /  /  /   /  /  /  /  / I===================================I
 //___/___/__/_\/__/__/  I  Exxon - Our gasoline contains no 
         /           /   I          sea water. Guaranteed!!             
        /           /    I                       
     __/         __/     I                                
===============================================================

jpr@dasys1.UUCP (Jean-Pierre Radley) (06/24/89)

In article <10800028@bradley> vijay@bradley.UUCP writes:
>You do not need to write a C program to do that. A one liner
>shell command will do it for you (In C Shell or Bourne Shell).
>All you need is :
>   
>                  date | cut -f4,5 -d" "
>

I never use two processes where one will do.

	date +%T

gives me the time. (Oh well, so it doesn't give me my timezone, but
that's really an invariant, no?)
-- 
Jean-Pierre Radley		CIS: 72160,1341		jpr@jpradley.UUCP