Sponsored Content
Top Forums Shell Programming and Scripting Script to display time difference.. Post 302219428 by chihung on Tuesday 29th of July 2008 04:27:33 AM
Old 07-29-2008
It is extremely hard to do it in pure unix shell script.
I came up with my version in perl, python, tcl. I am sure one of these commands is available in your os. IMHO, Tcl is a lot easier to extract timestamp string.

Perl:
Code:
#! /usr/bin/perl

use Time::Local;

while (<>) {
        if ( /(\d\d)\/(\d\d)\/(\d\d) (\d\d):(\d\d):(\d\d)$/ ) {
                $yr=$3+2000; $mth=$1; $day=$2;
                $hh=$4; $mm=$5; $ss=$6;

                if (/Begin Hot BACKUP/) {
                        $t0=timelocal($ss,$mm,$hh,$day,$mth,$year);
                }
                if (/End Hot BACKUP/) {
                        $t1=timelocal($ss,$mm,$hh,$day,$mth,$year);
                }
        }
}
print $t1-$t0,"\n";


Python:
Code:
#! /usr/bin/python

import sys
import re
import datetime
import time

for newline in sys.stdin.readlines():
        line=newline.rstrip()
        t=re.findall('(\d\d)/(\d\d)/(\d\d) (\d\d):(\d\d):(\d\d)$',line)
        ts=t[0]
        if t:
                if 'Begin Hot BACKUP' in line:
                        _t=datetime.datetime(int(ts[2])+2000, int(ts[0]), int(ts[1]), int(ts[3]), int(ts[4]), int(ts[5]))
                        t0=int(time.mktime(_t.timetuple()))
                if 'End Hot BACKUP' in line:
                        _t=datetime.datetime(int(ts[2])+2000, int(ts[0]), int(ts[1]), int(ts[3]), int(ts[4]), int(ts[5]))
                        t1=int(time.mktime(_t.timetuple()))

print t1-t0


Tcl:
Code:
#! /usr/bin/tclsh

while { [gets stdin line] >= 0 } {
        if { [regexp {(\d\d/\d\d/\d\d \d\d:\d\d:\d\d)$} $line x ts] } {
                if { [string match {*Begin Hot BACKUP*} $line] } {
                        set t0 [clock scan $ts]
                }
                if { [string match {*End Hot BACKUP*} $line] } {
                        set t1 [clock scan $ts]
                }
        }
}
puts [expr $t1-$t0]


It would be better if you can 'plant' this in your script
Code:
....
start=`perl -e 'print time()'`
....
# do your stuff here
end=`perl -e 'print time()`
echo "Total elapsed time in seconds: `expr $end - $start`"


Last edited by chihung; 07-29-2008 at 05:33 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to display last login date/time?

I was wondering if anyone had a script that would display the last time a user logged into a particular machine. I know about the "last" command, but it gives too much info.... I just wanted to know the last time a user used his/her id. ANy help would be greatly appreciated. Ryan (3 Replies)
Discussion started by: ryaneverett5
3 Replies

2. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies

3. Shell Programming and Scripting

display time required to complete running script

hi is there any way i can display a countdown time needed to run a script? like load a counter at the beginning of the script with the estimated time and display the counter decrementing till it finishes running the script? (3 Replies)
Discussion started by: npatwardhan
3 Replies

4. Shell Programming and Scripting

compare the log and display the time difference..

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. #!/bin/ksh export ORACLE_SID=$1 export primaryhost=$2 export sid=`echo $ORACLE_SID| tr ` RESULTFILE=/oracle/PC9/backupstatus_prod.log LOGP=`ssh -o StrictHostKeyChecking=no -l... (1 Reply)
Discussion started by: suri.tyson
1 Replies

5. Shell Programming and Scripting

script to display time

hey folks, i am stuc in this problem. You all might help me out. I want to write a BASH script to display time every 15 seconds using %r field descriptor. And want to clear the window each time before displaying time using clear command. Please help me out (3 Replies)
Discussion started by: manojrsb
3 Replies

6. 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

7. 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

8. 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

9. 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

10. 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
DateTime::Format::Epoch::ActiveDirectory(3pm)		User Contributed Perl Documentation	     DateTime::Format::Epoch::ActiveDirectory(3pm)

NAME
DateTime::Format::Epoch::ActiveDirectory - Active Directory epoch seconds SYNOPSIS
use DateTime::Format::Epoch::ActiveDirectory; my $dt = DateTime::Format::Epoch::ActiveDirectory ->parse_datetime( 1051488000 ); DateTime::Format::Epoch::ActiveDirectory->format_datetime($dt); # 1051488000 my $formatter = DateTime::Format::Epoch::ActiveDirectory->new(); my $dt2 = $formatter->parse_datetime( 1051488000 ); $formatter->format_datetime($dt2); DESCRIPTION
This module can convert a DateTime object (or any object that can be converted to a DateTime object) to the number of seconds since the epoch used in Microsoft Active Directory. Note that this epoch is defined in the local time zone. This means that these two pieces of code will print the same number of seconds, even though they represent two datetimes 6 hours apart: $dt = DateTime->new( year => 2003, month => 5, day => 2, time_zone => 'Europe/Amsterdam' ); print $formatter->format_datetime($dt); $dt = DateTime->new( year => 2003, month => 5, day => 2, time_zone => 'America/Chicago' ); print $formatter->format_datetime($dt); METHODS
Most of the methods are the same as those in DateTime::Format::Epoch. The only difference is the constructor. o new() Constructor of the formatter/parser object. It has no parameters. SUPPORT
Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details. AUTHOR
Eugene van der Pijll <pijll@gmx.net> COPYRIGHT
Copyright (c) 2004 Eugene van der Pijll. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
DateTime datetime@perl.org mailing list perl v5.10.1 2007-12-03 DateTime::Format::Epoch::ActiveDirectory(3pm)
All times are GMT -4. The time now is 06:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy