Difference Time Unix Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference Time Unix Shell
# 1  
Old 01-02-2012
Difference Time Unix Shell

I have 2 variables
Code:
MTIME="Jan_2_2012_23:55:49"
SCH_TIME="Jan_03_2012_00:32:28"

I want to find the time taken (in seconds) between MTIME and SCH_TIME.

Is there any way by which this can be done in Unix Shell Script?

Last edited by vbe; 01-02-2012 at 11:18 AM.. Reason: code tags
# 2  
Old 01-02-2012
If your date supports -d option, try this...
Code:
#!/bin/bash

MTIME="Jan_2_2012_23:55:49"
SCH_TIME="Jan_03_2012_00:32:28"

((DIFF=$(date -d "${SCH_TIME//_/ }" +%s ) - $(date -d "${MTIME//_/ }" +%s )))
echo "$DIFF Seconds"

--ahamed
# 3  
Old 01-03-2012
Thank you Ahamed.
But this works only in Linux.

Is there a way of using it in Solaris (ksh)
# 4  
Old 01-03-2012
A perl:
Code:
#!/usr/bin/perl

use strict;
use Time::Piece;
use POSIX qw(strftime);

# Time::Piece to create two time objects using the timestamps  

my $MTIME="Jan_2_2012_23:55:49";
my $SCH_TIME="Jan_03_2012_00:32:28";

my $before = Time::Piece->strptime($MTIME   , "%b_%d_%Y_%H:%M:%S");
my $after  = Time::Piece->strptime($SCH_TIME, "%b_%d_%Y_%H:%M:%S");


# Gap in seconds
my $secdiff = int ($after - $before);

print "Seconds:$secdiff\n";


# Format the result .
print  strftime( '%H:%M:%S', gmtime($secdiff))."\n";

# 5  
Old 01-03-2012
A solution in nawk

Code:
nawk 'BEGIN{
  MTIME="Jan_2_2012_23:55:49"; SCH_TIME="Jan_03_2012_00:32:28"

  month="JanFebMarAprMayJunJulAugSepOctNovDec"
  split(MTIME,a,"[_:]"); split(SCH_TIME,b,"[_:]")
  
  t1=mktime(a[3]" "int((index(month,a[1])/3))+1" "a[2]" "a[4]" "a[5]" "a[6])
  t2=mktime(b[3]" "int((index(month,b[1])/3))+1" "b[2]" "b[4]" "b[5]" "b[6])

  print t2-t1" Seconds"
}'

--ahamed

Last edited by ahamed101; 01-03-2012 at 01:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

2. Shell Programming and Scripting

Date / Time difference in shell script

Request ID GMDCOMTM GMDRRSTIME GMDRESTIME <36812986> : : :I want to display the date -time difference in other fields. Above I have given for only 1 record. I want to calculate for all the records. (GMCOMTM - GMDRRSTM) ,(GMDRRSTM-GMDRESTM) and... (5 Replies)
Discussion started by: ghosh_tanmoy
5 Replies

3. Shell Programming and Scripting

Date / Time difference in shell script

================================================================================ Request ID GMDCOM TIME GMDRRS TIME COM-RRS ================================================================================ <36812974> Tue Oct 1 13:32:40 2013 Tue Oct 1 20:36:42 2013... (1 Reply)
Discussion started by: ghosh_tanmoy
1 Replies

4. UNIX for Dummies Questions & Answers

Shell script - getting Time difference using awk

Hi..I have the data in a file like in this format, and I need the output time difference in seconds by using awk command. Start date/time and end date/time given in column 2,3 & 4,5. Please assist how to write shell script. File1.txt JOB1 10/09/2013 17:42:16 10/09/2013 17:43:46 SU 6202685/1... (4 Replies)
Discussion started by: mprithvi
4 Replies

5. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

6. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

7. Shell Programming and Scripting

Find Time difference in Unix

Hi, START_TIME :- "10-NOV-2009 00:00:04" STOP_TIME :- "10-NOV-2009 00:05:47" Please help to find difference between these two. Searched for the same topic but did not find an answer for the same time format :( Regards, Robin (3 Replies)
Discussion started by: robinbannis
3 Replies

8. Shell Programming and Scripting

Time difference in Minute in UNIX

is there any ways to get the time difference between 2 dates in UNIX? for example, For below date the outut should come 22 minutes startdate enddate ========= ======= 06/17/2008 13:25 06/17/2008 13:47 For, below date, the output should come 1462 minutes ... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

9. Shell Programming and Scripting

Unix function to calcuate the difference in time

HI , I need to get the timedifference between two values... which funcation will help eg: difference betweem 19:22 and 19:43 should give 21 mins (2 Replies)
Discussion started by: savitha
2 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question