Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org how to get difference between two times
 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

 12-30-2008, 05:56 AM #1 prasanta_das LQ Newbie   Registered: Dec 2008 Posts: 3 Rep: how to get difference between two times STARTING 06:12:23 SUCCESS 06:19:41 how to get diff of two times
 12-30-2008, 06:05 AM #2 prasanta_das LQ Newbie   Registered: Dec 2008 Posts: 3 Original Poster Rep: start 5:34:06 5:34:14 0:00:08
 12-30-2008, 06:05 AM #3 prasanta_das LQ Newbie   Registered: Dec 2008 Posts: 3 Original Poster Rep: can someone do me a favor for the above
 12-30-2008, 06:32 AM #4 Telemachos Member   Registered: May 2007 Distribution: Debian Posts: 754 Rep: 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.
 12-30-2008, 06:41 AM #5 arckane Member   Registered: Sep 2005 Location: UK Distribution: Gentoo/Debian/Ubuntu Posts: 307 Rep: Or use nano seconds - Code: `date +%N`
 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: 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.
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:
Quote:
 Originally Posted by prasanta_das 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.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kittunot4u Linux - General 3 07-19-2010 04:26 AM halo_75 Linux - Networking 3 05-30-2007 07:48 AM Balarabay1 SUSE / openSUSE 14 04-30-2006 10:00 AM sachitha Programming 6 01-31-2006 08:23 PM james 456 Linux - Networking 0 01-12-2006 06:55 PM

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

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -