Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get current timestamp in microseconds or milliseconds

1,258 views
Skip to first unread message

laredotornado

unread,
Oct 20, 2009, 5:23:53 PM10/20/09
to
Hi,

In a shell script (sh), how do I store the time in milliseconds (or
microseconds, don't care) to a variable?

Thanks, - Dave

Janis Papanagnou

unread,
Oct 20, 2009, 5:48:38 PM10/20/09
to
laredotornado wrote:
> Hi,
>
> In a shell script (sh), how do I store the time in milliseconds (or
> microseconds, don't care) to a variable?

Where shall the milli-(micro-)seconds come from? (Actual time?)

Depending on what shell the 'sh' actually is, in a modern ksh93 you can do

ms=${SECONDS#*.}

(For plain sh Bourne shells you'd have to rely on external programs.)

Janis

>
> Thanks, - Dave

laredotornado

unread,
Oct 20, 2009, 5:58:43 PM10/20/09
to
On Oct 20, 3:48 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:

I meant milliseconds/microseconds from the actual time, yes. I wrote
this, but sadly it always produces zero:

#!/bin/sh
ms=${SECONDS#*.}
echo $ms

I'm on a Mac OS 10.5.6. Here is what I see in response to uname -a:

Darwin ocho.local 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24
17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i38

Keith Keller

unread,
Oct 20, 2009, 6:14:24 PM10/20/09
to
On 2009-10-20, laredotornado <laredo...@zipmail.com> wrote:
> On Oct 20, 3:48�pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> wrote:
>>
>> Depending on what shell the 'sh' actually is, in a modern ksh93 you can do
>>
>> � �ms=${SECONDS#*.}
>>
>> (For plain sh Bourne shells you'd have to rely on external programs.)
>
> I meant milliseconds/microseconds from the actual time, yes. I wrote
> this, but sadly it always produces zero:
>
> #!/bin/sh
> ms=${SECONDS#*.}
> echo $ms

Well, as Janis wrote, for plain Bourne shells this won't work. It's
likely that /bin/sh is a Bourne shell (or a link to bash).

> I'm on a Mac OS 10.5.6. Here is what I see in response to uname -a:

uname won't really help. You need to know what shell /bin/sh is. But
on OS X it's likely bash.

I don't think that ksh code will give you what you want anyway; if my
reading of the man page is right, it gives you the number of seconds
since shell invocation, not absolute milliseconds. (You may need to
install a newer version of ksh on your OS X box.) bash has a similar
variable (though AFAICT it doesn't do any unit smaller than seconds).

What exactly are you trying to accomplish? What is it you're measuring
that you think you need milliseconds?

--keith

--
kkeller...@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

Glenn Jackman

unread,
Oct 20, 2009, 6:54:40 PM10/20/09
to
At 2009-10-20 05:23PM, "laredotornado" wrote:
> Hi,
>
> In a shell script (sh), how do I store the time in milliseconds (or
> microseconds, don't care) to a variable?

Not in the shell, but with per:

perl -MTime::HiRes -e 'print int(1000 * Time::HiRes::gettimeofday),"\n"'

The overhead of invoking perl will certainly add a few milleseconds...

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Lao Ming

unread,
Oct 20, 2009, 8:55:03 PM10/20/09
to

About 22-23 milliseconds for the overhead of each iteration below:

#! /bin/bash
i=0
while [ $i -le 60 ]
do
ms=$( perl -MTime::HiRes -e 'print int(1000 *
Time::HiRes::gettimeofday),"\n"' )
sec=$( date '+%S' )
printf '%2d %d\n' "$sec" "$ms"
sleep 1
i=$(( $i + 1 ))
done

I'm sure there are better ways to do this so feel free.

mop2

unread,
Oct 20, 2009, 10:10:53 PM10/20/09
to
On Tue, 20 Oct 2009 19:23:53 -0200, laredotornado <laredo...@zipmail.com> wrote:

> In a shell script (sh), how do I store the time in milliseconds (or
> microseconds, don't care) to a variable?


nano seconds since 1970-01-01 00:00:00 UTC:

$ date --version|head -n1
date (GNU coreutils) 7.6

$ NS=`date +%s%N`


$ echo $NS
1256090733975302128

John DuBois

unread,
Oct 25, 2009, 2:20:10 PM10/25/09
to
In article <0bp1r6x...@goaway.wombat.san-francisco.ca.us>,

Keith Keller <kkeller...@wombat.san-francisco.ca.us> wrote:
>On 2009-10-20, laredotornado <laredo...@zipmail.com> wrote:
>> On Oct 20, 3:48�pm, Janis Papanagnou <janis_papanag...@hotmail.com>
>> wrote:
>>>
>>> Depending on what shell the 'sh' actually is, in a modern ksh93 you can do
>>>
>>> � �ms=${SECONDS#*.}
>>>
>>> (For plain sh Bourne shells you'd have to rely on external programs.)
>>
>> I meant milliseconds/microseconds from the actual time, yes.
>> I'm on a Mac OS 10.5.6. Here is what I see in response to uname -a:
>
>I don't think that ksh code will give you what you want anyway; if my
>reading of the man page is right, it gives you the number of seconds
>since shell invocation, not absolute milliseconds.

Yes, that's correct.

This will give current nanoseconds, on new-enough ksh93:

ns=$(printf "%(%N)T")

John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

0 new messages