-1

I don't seem to find this anywhere... I have two date/time using YYYYmmddHHMMSS, like this:

D1=20140603132050
D2=20140604114020

I need to find the difference between them in hours.

  • I'm using korn shell;
  • I have GNU date;

Any clue?

jww
  • 97,681
  • 90
  • 411
  • 885
AndiFaria
  • 27
  • 4
  • What you want to see? – Srini V Jun 04 '14 at 14:46
  • @realspirituals: I want to see "The difference between D1 and D2 is X hours." – AndiFaria Jun 04 '14 at 14:51
  • @fedorqui: For some strange reason, GNU date doesn't recognize the "-d" parameter... – AndiFaria Jun 04 '14 at 14:52
  • 1
    Oh in Solaris there is no `-d` in date. http://stackoverflow.com/search?q=%5Bsolaris%5D+date shows some approaches, for example http://stackoverflow.com/questions/12471585/how-to-calculate-time-difference-in-solaris looks helpful. – fedorqui Jun 04 '14 at 14:54
  • This would be easier in a language like Perl or Tcl. what's `perl --version` and/or `echo 'puts [info patchlevel]' | tclsh` – glenn jackman Jun 04 '14 at 15:24

1 Answers1

0

If you have a recent-ish Perl, you can do:

diff_hrs=$(
    printf "%s\n" "$D1" "$D2" | 
    perl -MTime::Piece -e '
        ($t1, $t2) = map {chomp; Time::Piece->strptime($_, "%Y%m%d%H%M%S")} <>; 
        print +($t2-$t1)/3600, "\n"
    '
)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352