LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2008, 05:56 AM   #1
prasanta_das
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
Smile how to get difference between two times


STARTING 06:12:23

SUCCESS 06:19:41


how to get diff of two times
 
Old 12-30-2008, 06:05 AM   #2
prasanta_das
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
start
5:34:06 5:34:14 0:00:08
 
Old 12-30-2008, 06:05 AM   #3
prasanta_das
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
can someone do me a favor for the above
 
Old 12-30-2008, 06:32 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
My advice would be to (1) convert your times (as you get them) to epoch time and (2) then take the difference of those two. You can then (3) convert the difference into a more human-friendly time format whenever you need.

Example (using your first request):
Code:
06:12:23 or epoch time: 1230617543

06:19:41 or epoch time: 1230617981

438 seconds difference or 7 minutes and 18 seconds
You will need to write a program to take in the two timestamps and do the computations, if you plan to do this regularly. Here are a few links to sites that can show you what's involved.

http://www.epochconverter.com/
http://www.csgnetwork.com/epochtime.html

The first website has short, but helpful discussions about how to compute epoch time from "human" formats and vice versa in various languages.

Edit: I'm assuming that you want help doing this with a program and not that you just want us to do the math for you.

Last edited by Telemachos; 12-30-2008 at 06:47 AM.
 
Old 12-30-2008, 06:41 AM   #5
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 307

Rep: Reputation: 38
Or use nano seconds -
Code:
date +%N
 
Old 12-30-2008, 07:01 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You have also to check if the END time is smaller or greater than the START time. For example if a job starts at 23:45:09 and terminates at 00:23:18 you have to take in account the change of day in your calculations (to not mention the troubles introduced by the passage from/to the Daylight Saving Time).

Anyway, here is an example of what you can do:
Code:
#!/bin/bash
START=23:58:56
END=00:45:00

if [[ $END < $START ]]
then
  elapsed=$(date -d "00:00:00 $(($(date -d "19700102 $END" +%s) - \
          $(date -d "19700101 $START" +%s))) seconds" +%H:%M:%S)
else
  elapsed=$(date -d "00:00:00 $(($(date -d "19700101 $END" +%s) - \
          $(date -d "19700101 $START" +%s))) seconds" +%H:%M:%S)
fi

echo $elapsed
END and START must be in HH:MM:SS format to let the above code work, since the comparison is done by ASCII order (string comparison).

Last edited by colucix; 12-30-2008 at 07:03 AM.
 
Old 12-30-2008, 09:21 AM   #7
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by prasanta_das View Post
how to get diff of two times
Here's a script that I posted as an answer to a similar question in another thread.

This script will show you how to convert a time to seconds since the epoch. You can then compare the seconds.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
IMAP Times out!!! halo_75 Linux - Networking 3 05-30-2007 07:48 AM
Data transfer online is slow "at times" or "stops at times" Balarabay1 SUSE / openSUSE 14 04-30-2006 10:00 AM
how to use times(2) ... sachitha Programming 6 01-31-2006 08:23 PM
System hangs; Atheros Madwifi-ping times out every 15/16 times james 456 Linux - Networking 0 01-12-2006 06:55 PM


All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration