Operating System - HP-UX
1832592 Members
2820 Online
110043 Solutions
New Discussion

Re: Obtaining UNIX current epoch time in milliseconds

 
Danny Fang
Frequent Advisor

Obtaining UNIX current epoch time in milliseconds

Hi,

Could anyone show me how I could obtain the current UNIX epoch time in milliseconds?

I've tried using the method below, but this seems to return the current epoch time in seconds
bash-3.00$ perl -e 'print time,"\n";'
1144577446
bash-3.00$

Could anyone kindly help me out by showing me how it's obtained or some scripts to perform this task?

Thanks


6 REPLIES 6
melvyn burnard
Honored Contributor

Re: Obtaining UNIX current epoch time in milliseconds

Out of interest, which version of HP-UX are you using to do this?

My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
James R. Ferguson
Acclaimed Contributor

Re: Obtaining UNIX current epoch time in milliseconds

Hi Danny:

You can use Perl's 'Time::HiRes' module.

Calling 'gettimeofday()' in scalar context returns a floating point number of seconds; calling in list context returns time in seconds *and* milliseconds.

cat # ./hitime
#!/usr/bin/perl
use Time::HiRes qw(gettimeofday);
$t = gettimeofday();
print "The time in secs = $t\n";
@t = gettimeofday();
print "The time in secs = $t[0] and msecs = $t[1]\n";

# ./hitime
The time in secs = 1144590040.7041
The time in secs = 1144590040 and msecs = 704642

Regareds!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Obtaining UNIX current epoch time in milliseconds

Of course, the problem with calling this as an external program is that the overhead of fork()'ing and exec()'ing a new process (perl) completely distorts any millisecond resolution and repeatability that you might be looking for. Calling high resolution time functions only makes sense from within a program.
If it ain't broke, I can fix that.
Danny Fang
Frequent Advisor

Re: Obtaining UNIX current epoch time in milliseconds

Hi Melvywn,
I'm using a HPUX 11.0 machine with the following spec:

prod-cingtoman :/users/lows >uname -a
HP-UX toman B.11.11 U 9000/800 1854960616 unlimited-user license

Hi James,
The PERL script that you've provided using the Time::HiRes module is great.

However, I was wondering if there is also an equivalent way of performing the same task as the PERL script provided, using Shell scripts?

Thanks

You can use Perl's 'Time::HiRes' module.

Calling 'gettimeofday()' in scalar context returns a floating point number of seconds; calling in list context returns time in seconds *and* milliseconds.

cat # ./hitime
#!/usr/bin/perl
use Time::HiRes qw(gettimeofday);
$t = gettimeofday();
print "The time in secs = $t\n";
@t = gettimeofday();
print "The time in secs = $t[0] and msecs = $t[1]\n";

# ./hitime
The time in secs = 1144590040.7041
The time in secs = 1144590040 and msecs = 704642

Regareds!



James R. Ferguson
Acclaimed Contributor

Re: Obtaining UNIX current epoch time in milliseconds

Hi (again) Danny:

You asked if 'gettimeofday()' or an equivalent was available directly in a shell. The answer is "no". Your choice would be the Perl snippet I provided or a C program that calls 'gettimeofday(2)'. Either method can be called from a shell script with the understanding that the will be the overhead of process creation that will perturb the time reported slightly.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Obtaining UNIX current epoch time in milliseconds

You can use the perl 1-liner or create a perl script and call that. However, I strongly suggest that you call for the time from a script several times in a row both at an idle time as well as during a busy period. Then compare the results. A perfect time request would return in significantly less time than the resolution (ie, 1 millisecond), so that means your script must start perl, interpret the code, make the call, then return the value and format it so you can read it...all within a few millionths of a second. The reality is that it will take milliseconds to complete each time request even on very fast machines. And the result is non-deterministic, a fancy word that says each time request via perl will take a different amount of time depending on the system load, etc.


Bill Hassell, sysadmin