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 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? –  Stephane Chazelas Nov 20 '12 at 10:07
add comment

2 Answers

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

#!/bin/bash
while read d1_1 d1_2 d2_1 d2_2; do
  secdiff=$((
    $(date -d "$d2_1 $d2_2" +%s) - $(date -d "$d1_1 $d1_2" +%s)
  ))
  nanosecdiff=$((
    $(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 / 1000)
  ))
done < YOUR_FILE.txt

OUTPUT

2012-09-13 15:00:29,297 - 2012-09-13 15:00:29,290 = 7000 milliseconds
2012-09-13 15:00:29,447 - 2012-09-13 15:00:29,428 = 19000 milliseconds

See man date

NOTE

  • 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) –  sputnick Nov 20 '12 at 8:17
add comment

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
add comment

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.