cancel
Showing results for 
Search instead for 
Did you mean: 

how to get timestamp

SOLVED
Go to solution
Michal Toth
Regular Advisor

how to get timestamp

Hi guys,

I just need to get actual timestamp from the command line (or a shell). I need to get actual count of seconds since 1970. I can do it in C using time() function, but I was about to make a script and amazingly found out that I cannot find anything that would provide this output to me.

thanks for all ideas

Michal
10 REPLIES
H.Merijn Brand (procura
Honored Contributor

Re: how to get timestamp

lt09:/home/merijn 111 > perl -le'print time'
1112974908
lt09:/home/merijn 112 >

sh-2.05b$ STAMP=$(perl -e'print time')
sh-2.05b$ echo $STAMP
1112974980
sh-2.05b$

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: how to get timestamp

#!/usr/bin/sh

EPOCH=$(perl -e 'print time()')
echo "Seconds since 1-Jan-1970 00:00:00 UTC = ${EPOCH}"
If it ain't broke, I can fix that.
Michal Toth
Regular Advisor

Re: how to get timestamp

thanks guys - it really solves the problem, but I was looking rather for a ksh internal or system command. Using perl makes the script less portable.
Patrick Wallek
Honored Contributor

Re: how to get timestamp

I would think perl would actually make the script more portable since you can get Perl for darn near *any* operating system. Heck, you can even get Perl for Windows systems.
H.Merijn Brand (procura
Honored Contributor

Re: how to get timestamp

And if you have a more recent perl, it includes Time::HiRes, which also works on win32 :)

lt09:/home/merijn 116 > perl -MTime::HiRes=time -le'print time'
1112976753.63035
lt09:/home/merijn 117 >

Microseconds? From a shell-builtin? Nahhh

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: how to get timestamp

There's an adb method but that's much less portable. Hang on and I give you an all shell solution.
If it ain't broke, I can fix that.
Michal Toth
Regular Advisor

Re: how to get timestamp

oh :-)

i wasn't questioning availability of perl. I just wanted to stress fact that the perl would have to be installed on the system where I intend to use this script. The thing is that I support roughly 1600 systems and portability really does matter when it comes to these numbers.

cheers

Michal
H.Merijn Brand (procura
Honored Contributor
Solution

Re: how to get timestamp

As from 'man perlhpux'

--8<---
Using perl as shipped with HP-UX

Application release September 2001, HP-UX 11.00 is the
first to ship with Perl. By the time it was perl-5.6.1 in
/opt/perl. The first occurrence is on CD 5012-7954 and can
be installed using
-->8---

And that's just talking about perl5.
perl4 is available on *all* hp-ux systems for a loooooong time:

d3:/u/usr/merijn 101 > uname -a
HP-UX d3 B.10.20 U 9000/800 2015358431 unlimited-user license
d3:/u/usr/merijn 102 > /usr/contrib/bin/perl -v

This is perl, version 4.0

$RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $
Patch level: 36

Copyright (c) 1989, 1990, 1991, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 4.0 source kit.
d3:/u/usr/merijn 103 > /usr/contrib/bin/perl -l -e 'print time'
1112977707
d3:/u/usr/merijn 104 > ll /usr/contrib/bin/perl
2034 -r-xr-xr-x 1 bin bin 544768 Jun 10 1996 /usr/contrib/bin/perl
d3:/u/usr/merijn 105 >

So, unless you still maintain HP-UX 9.00 or any system older than 10.20, you can be safe in that using the bare

perl -e 'print time'

will work. Note that older perl's need a space between the -e and the 'print

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: how to get timestamp

Okay, here's a pure shell solution adapted from another script.

#!/usr/bin/sh

typeset -i EPOCH=$(epoch.sh)
echo "Epoch seconds = ${EPOCH}"


Search the Forums for an obscure script called "caljd.sh" to see where the strange looking function comes from.
If it ain't broke, I can fix that.
Miklos Bacso
Occasional Visitor

Re: how to get timestamp

date +%s