|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Time difference in a file
i have a script that gives me the start time and end time.
how to calculate the time difference b/w the two here is the o/p of a file Start Time: 2008-02-19 00:13:16,338 End Time: 2008-02-19 23:43:17,974 ------------------------------------------------- how to find the difference b/w the two time ? |
|
#2
|
|||
|
|||
|
Quote:
I think it is almost impossible to do it with those timestamps... If the time provided would be in milliseconds (since 1970) you could use the following script: tail +5 yourfile.txt | head -n 1 > endtime tail +2 yourfile.txt | head -n 1 > starttime paste -d"-" endtime starttime > difftime echo quit >> difftime cat << Here Time difference: `bc difftime` Here rm endtime rm starttime rm difftime In "tail +5" and "tail +2" you can replace the number by the line on which the number of milliseconds can be found. And yourfile.txt is the file in which the Start time and End time is stored. Hope this helps, Isuwyn |
|
#3
|
|||
|
|||
|
If you pass that date/time string to an outside script, you can convert it to another format. I dont know of a way to do it natively in the shell, but Ruby and Python come to mind as an easy alternative.
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#4
|
|||
|
|||
|
Quote:
With ksh script: datediffms 2008 02 19 00 13 16 338 2008 02 19 23 43 17 974 23:30:01,636 And the code for datediffms Code:
#!/usr/bin/ksh
dt2ms() { # ccyy mm dd HH MM SS nnn to miliseconds
y=$1 m=${2}-1 d=$3 H=$4 M=$5 S=$6 f=$7 a=$((12*$y+$m-2)) b=$(($a/12))
printf "$(((86400*((367*$a+7)/12-2*$b+$b/4+$d-$b/100+$b/400-719469)\
+$H*3600+$M*60+$S)*1000+$f))"
}
hmsf() { # miliseconds to hh:mm:ss.nnn
s=$(($1/1000)) H=$(($s/3600)) M=$((($s-$H*3600)/60)) S=$(($s-$H*3600-$M*60))
printf "%02s:%02s:%02s,%03s\n" $H $M $S $(($1-$1/1000*1000))
}
printf "$(hmsf $(($(dt2ms $8 $9 ${10} ${11} ${12} ${13} ${14}) \
- $(dt2ms $1 $2 $3 $4 $5 $6 $7))))\n"
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Time difference in a file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|