Difference between two date values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference between two date values
# 1  
Old 10-13-2015
Difference between two date values

Code:
bash-3.00$ date

Tue Oct 13 15:03:54 CEST 2015
Code:
start=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $start
08 17
end=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $end
08 37

how can I get the difference between above both timestamps, Im trying but getting errors: any one please help
Code:
bash-3.00$ echo `expr $end - $start`
expr: syntax error

bash-3.00$ echo  $(($end - $start))
bash: 08: value too great for base (error token is "08")

expected output for this case should be something like 00 20

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Don Cragun; 10-22-2015 at 03:10 AM.. Reason: Put sample output in bold CODE tags.
# 2  
Old 10-13-2015
man bash:
Quote:
Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, ...
So, leadin the numbers with 10#. Try
Code:
end=( $(date +"%T") )
echo ${end[@]}
15 50 54
start='15 08 01'
echo $(( (10#${end[0]} - 10#${start[0]}) * 3600 + (10#${end[1]} - 10#${start[1]}) * 60 + 10#${end[2]} - 10#${start[2]} ))
2573

# 3  
Old 10-13-2015
Quote:
Originally Posted by sam@sam
Code:
bash-3.00$ date

Tue Oct 13 15:03:54 CEST 2015
Code:
start=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $start

08 17
Code:
end=`date +"%T" | awk -F":" '{print $2,$3}'`
echo $end

08 37
how can I difference between above both times, Im trying but getting errors:
Code:
bash-3.00$ echo `expr $end - $start`

expr: syntax error
Code:
bash-3.00$ echo  $(($end - $start))

bash: 08: value too great for base (error token is "08")
expected output for this case should be something like 00 20
Hello sam@sam,

Could you please try following and let us know if this helps.
Code:
START=`date +"%T" | awk -F":" '{print $2 FS $3}'`
##### I ran following command after few mins
END=`date +"%T" | awk -F":" '{print $2 FS $3}'`
##### Then use following awk command to calculate difference.
awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'

You could use this in script too as per your convenience too.
NOTE: It is only looking for taking difference of minutes and seconds not dates.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-13-2015 at 11:45 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-13-2015
No controls on 'sleep job' whatsoever :
Code:
#!/usr/bin/ksh
START="$(date +%s";"%T)"
sleep 2 # this is your job, my job is sleeping.
END="$(date +%s";"%T)"
printf "From=%s To=%s Dur=%s sec\n" "${START#*;}" "${END#*;}"  "$((${END%%;*}-${START%%;*}))"

Since we are using posix time duration will be correct for any job duration.
You can change the "%T" in START and END to reflect time more precise, and also do additional calculation to convert to minutes and hours on those variables.
This User Gave Thanks to Peasant For This Post:
# 5  
Old 10-14-2015
You are not conclusive about your ultimate goal, but if you just want to measure the time a certain job needs to run you can just use the time command. The output will be similar to this:

Code:
# time <command>
real    0m3.01s
user    0m0.00s
sys     0m0.00s

What you are interested in most is perhaps the "real" part of the output. This is what the job took to run on the system. "user" and "sys" is the time spent in user mode and kernel mode respectively. This specific job (i used "sleep 3") took a tad over 3 seconds to run, needing (nearly) no time in user mode and no time in kernel mode.

I hope this helps.

bakunin
# 6  
Old 10-14-2015
bash has the SECONDS variable:
Code:
START=$SECONDS 
... commands ... 
echo $(( SECONDS - START ))
31

# 7  
Old 10-14-2015
Hi R.Singh your code match my requirement but at when use awk Im not getting expected results, can you please help me here
Code:
 START=`date +"%T" | awk -F":" '{print $2 FS $3}'`
 echo $START
31:36
 END=`date +"%T" | awk -F":" '{print $2 FS $3}'`
 echo $END
31:59
 awk -vstart=$START -vend=$END 'BEGIN{split(start, A,":");split(end, B,":");sec_start=A[1] * 60 + A[2];sec_end=B[1] * 60 + B[2];diff=sec_end - sec_start;print "Minutes \t Seconds" ORS int(diff/60) "\t\t " diff%60}'
Minutes          Seconds
0                0

When I enter awk comd its not existing from cmd prompt Where I need to exit forcefully
my OS details
Code:
uname -a
SunOS 5.10 Generic_148888-05 sun4u sparc SUNW,SPARC-Enterprise


Last edited by Don Cragun; 10-22-2015 at 03:13 AM.. Reason: Put sample output in bold CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. AIX

Time Difference between date and date -u

Hi Everyone, We are having an issue with date and date -u in our AIX Systems. We have checked environment variable TZ and /etc/environment and however, we could not rectify the difference. >date Thu Mar 19 22:31:40 IST 2015 >date -u Thu Mar 19 17:01:44 GMT 2015 Any clue... (5 Replies)
Discussion started by: madhav.kunapa
5 Replies

3. Shell Programming and Scripting

Date difference

HI All , i need a bash script to find the number of days between two dates . Format YYYY-MM-DD THanks, Neil (1 Reply)
Discussion started by: nevil
1 Replies

4. Programming

Date difference

I tried the below code to find difference between two dates. It works fine if the day of the month is 2-digit number. But it fails when we have a single-digit day of month(ex:1-9). my code is as below. please help me soon. #!/usr/bin/perl -w use strict; use Time::Local; ... (2 Replies)
Discussion started by: anandrec
2 Replies

5. Homework & Coursework Questions

help with the date difference

1. The problem statement, all variables and given/known data: The problem i have is that i probably make a few mistake here in the code but don't know what it is and i try to get the date difference but don't know where to add the days_in_month function 2. Relevant commands, code,... (1 Reply)
Discussion started by: mgyeah
1 Replies

6. Shell Programming and Scripting

Difference in date

Dear all, I fancy that I'm pretty competent in ksh, but I have someone on HP-UX wanting me to script up a simple interface to handle user alterations rather than giving them high privileges to run up SAM. This is all fairly straightforward, but I'm stuck on an epoch date issue. When we have... (6 Replies)
Discussion started by: rbatte1
6 Replies

7. Shell Programming and Scripting

Calculate difference between two successive values

Hi, I have a file containing timestamps (at micro-seconds granularity). It looks like the following: 06:49:42.383818 06:49:42.390190 06:49:42.392308 06:49:42.392712 06:49:42.393437 06:49:42.393960 06:49:42.402115 Now I need a sed/awk script to take the difference of two successive... (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

8. Shell Programming and Scripting

Korn Shell Variable values difference

I am using two shell scripts a.ksh and b.ksh a.ksh 1. Sets the value +++++++++++++++++ export USER1=abcd1 export PASSWORD=xyz +++++++++++++++++ b.ksh 2. Second scripts calls sctipt a.ksh and uses the values set in a.ksh and pass to an executable demo... (2 Replies)
Discussion started by: kunalseth
2 Replies

9. Shell Programming and Scripting

Bash script getting difference values from files

Hi I want to write a BASH script. I have files updated every hour from two platforms.The file names are : - Falcon_smsCounters_1200020708.log - Canari_smsCounters_1200020708.log 1200 refers to hour twelve and 020708 to 02 july 2008. Inside every file i have a list of parameters... (2 Replies)
Discussion started by: salimayoub
2 Replies

10. Linux

date difference

hi, i have 2 dates in the form: '20080315120030' and '20080310140030'. i.e. YYYYMMDDHHMMSS. i need a way of getting the difference between them using shell script. any thoughts? (14 Replies)
Discussion started by: muay_tb
14 Replies
Login or Register to Ask a Question