Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to do a bash script that measures the launch time of a browser for that I am using an html which gets the time-stamp on-load in milliseconds using JavaScript.

In the shell script just before i call the browser I get the time-stamp with:

date +%s

The problem is that it gets the time-stamp in seconds, and I need it in milliseconds since sometimes when ran a second time the browser starts in under a second and I need to be able to measure that time precisely using milliseconds instead of seconds.

How can I get the time-stamp in milliseconds from a bash script?

share|improve this question

4 Answers 4

up vote 10 down vote accepted

date +%s.%N will give you, eg., 1364391019.877418748. The %N is the number of nanoseconds elapsed in the current second. Notice it is 9 digits, and by default date will pad this with zeros if it is less than 100000000. This is actually a problem if we want to do math with the number, because bash treats numbers with a leading zero as octal. This padding can be disabled by using a hyphen in the field spec, so:

echo $((`date +%s`*1000+`date +%-N`/1000000))

would naively give you milliseconds since the epoch.

However, as Stephane Chazelas points out in comment below, that's two different date calls which will yield two slightly different times. If the second has rolled over in between them, the calculation will be an entire second off. So:

echo $(($(date +'%s * 1000 + %-N / 1000000')))
share|improve this answer
    
I tried it and it works, thanks +1/accept! –  Eduard Florinescu Mar 27 '13 at 13:38
4  
Note that in between the time you run the first date and the second date, several million nanoseconds may have passed and it might not even be the same second. Best to do $(($(date +'%s * 1000 + %N / 1000000'))). That still doesn't make much sense, but would be more reliable. –  Stéphane Chazelas Mar 27 '13 at 16:24
    
@StephaneChazelas Ouch, yeah that is a pitfall, will edit. Thx. –  goldilocks Mar 27 '13 at 16:41
    
This fails in at least 10% of cases: when first digit of %N is zero, the number is interpreted as octal, which causes bash throw error (unless rest of digits are lower than 7, in which case the failure is hidden!) –  Alois Mahdal Mar 27 '13 at 18:56
1  
Fixed by turning off zero padding as noted in date(1) –  Alois Mahdal Mar 27 '13 at 19:09

I would use:

$ date +%s%3N

which returns milliseconds since Unix epoch.

share|improve this answer
    
Elegant, nice, +1 –  Eduard Florinescu Apr 8 at 16:56

In case anyone is using other shells than bash, ksh93 and zsh have a floating point $SECONDS variable if you do a typeset -F SECONDS which can be handy to measure time with accuracy:

$ typeset -F SECONDS=0
$ do-something
something done
$ echo "$SECONDS seconds have elapsed"
18.3994340000 seconds have elapsed

Recent versions of zsh have a $EPOCHREALTIME special floating point variable in the zsh/datetime module:

$ zmodload zsh/datetime
$ echo $EPOCHREALTIME
1364401642.2725396156
$ printf '%d\n' $((EPOCHREALTIME*1000))
1364401755993
share|improve this answer

To avoid calculations to get the milliseconds, command line JavaScript interpreters may help:

bash-4.2$ node -e 'console.log(new Date().getTime())' # node.js
1364391220596

bash-4.2$ js -e 'print(new Date().getTime())'         # SpiderMonkey
1364391220610

bash-4.2$ jsc-3 -e 'print(new Date().getTime())'      # WebKit
1364391220622

bash-4.2$ seed -e 'new Date().getTime()'              # GNOME object bridge
1364391220669
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.