Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to shell scripting. The heart of my script is to find the difference between two timestamps up to milliseconds. With me I have a file with content of timestamps only as

2012-09-13 15:00:29,290 2012-09-13 15:00:29,297
2012-09-13 15:00:29,428 2012-09-13 15:00:29,447

Like this I have around 30k records, where I should not face any performance issue when I execute script. Many factors like leap year, months with 31 days etc come into picture when I am trying to write script for this.

Can anyone help me on this please?

share|improve this question
Does DST come into the picture? Leap seconds? What are the DST rules? Have they changed over time in your country? Do you have to deal with dates before 1970, or before a switch to the gregorian calendar? – Stéphane Chazelas Nov 20 '12 at 10:07

No need to do complex parsing, will do all the magic for you, with the help of his friend, :

while read d1_1 d1_2 d2_1 d2_2; do
    $(date -d "$d2_1 $d2_2" +%s) - $(date -d "$d1_1 $d1_2" +%s)
    $(date -d "$d2_1 $d2_2" +%N) - $(date -d "$d1_1 $d1_2" +%N)
  printf "%s %s - %s %s = %d milliseconds\n" $d2_1 $d2_2 $d1_1 $d1_2 $((
    (secdiff * 1000) + (nanosecdiff / 1000000)
done < YOUR_FILE.txt


2012-09-13 15:00:29,297 - 2012-09-13 15:00:29,290 = 7 milliseconds
2012-09-13 15:00:29,447 - 2012-09-13 15:00:29,428 = 19 milliseconds

See man date


  • date -d is very useful, it convert timestamps
  • %s is the epoch time (seconds since 01-01-1970)
  • %N is nanoseconds
  • $(( )) and (( )) is for bash arithmetic, see http://mywiki.wooledge.org/ArithmeticExpression
  • $( ) stands for command substitution

Is this fit your needs as well ?

share|improve this answer
Corrected an arithhmetic error (concatenating seconds and nanoseconds). Now I first compute the seconds (OP don't asked that, but for completeness and robustness in re-use) – Gilles Quenot Nov 20 '12 at 8:17

A scripting language such as Perl, Python or Ruby will be fast and require little effort. For example, with Perl and Date::Parse:

perl -MDate::Parse -l -ne 's/,/./g; split; print str2time("$_[2] $_[3]") - str2time("$_[0] $_[1]")'

(For each line, replace , by ., split the line into words $_[0] through $_[3], parse the dates formed by the first two and next two words, and print the difference.)

share|improve this answer
"Little effort" is quite relative. Sure, if you're a X programmer and know about the Y library, it's a cinch. That's how it always is. Actually, I'm surprised a Python programmer hasn't jumped in here yet. I figure it's just a matter of time... :-\ – Mike S Dec 6 '15 at 6:58

Your Answer


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.