Find the answer to your Linux question:
Results 1 to 10 of 10
Can somebody help me with a bash script to calculate difference between two times. I have to append this to a script and report if the time difference between last ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4

    Difference between two times


    Can somebody help me with a bash script to calculate difference between two times. I have to append this to a script and report if the time difference between last log entry and current time is greater than 5 mins

    First time is from an entry of a log file (which i can not modify) and its format is 2012-06-22 14:47:44,905 (can ignore milliseconds) and the second is current system time.

    I tried searching different forums but couldn't find anything that would be helpful to me.

    A quick reply would be really helpful.

    regards,

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    Maybe this is a start.
    Needs more errorhandling, better logging in general (via a syslog facility) and adjusting to your usecase of course.

    The basic idea is to convert the dates to seconds since epoch and then do simple arithmetic on them.

    Code:
    #!/usr/bin/env bash
    
    logtime=$(date --date='2012-06-22 14:47:44' +%s)
    currenttime=$(date +%s)
    threshold=300
    
    timediff=$(( $currenttime - $logtime ))
    
    if [ $timediff -gt 0 ]; then
      if [ $timediff -gt $threshold ]; then
        echo "Warning: Time difference is $timediff seconds. (Threshold: $threshold)"
      fi
    else
      echo "Error: OMG, time is reverted. Duck and cover."
    fi
    You must always face the curtain with a bow.

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    P.S.:
    You also need to define the timezones of both calculations: $logtime and $currenttime.

    See here:
    Code:
    TZ="Europe/Berlin"  date --date='2012-06-22 14:47:44' +%s
    1340369264
    TZ="America/Los_Angeles"  date --date='2012-06-22 14:47:44' +%s
    1340401664
    You must always face the curtain with a bow.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4
    Thanks for the help, but it doesn't work as I am using Freebsd 5.4 and date function doesn't has --date switch

    I thought of doing that initially but it failed. I should have mentioned that in the initial post

    $ date --date='2012-06-22 14:47:44' +%s
    date: illegal option -- -
    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
    [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]

    Can you suggest any other way of doing that.

  6. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    It seems, that date is so old that it doesnt support long options.
    Use -d then or consult the man page.
    Code:
    TZ="America/Los_Angeles"  date -d '2012-06-22 14:47:44' +%s
    1340401664
    Other than that, freebsd 5.4 is from early 2005.
    Time to update, imho.
    You must always face the curtain with a bow.

  7. #6
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4
    I agree that it needs to upgrade, but that is out of my control.

    Unfortunately there is also no -d switch available

    Any other method of doing this.

  8. #7
    Just Joined!
    Join Date
    Jun 2012
    Posts
    4
    I think this would give me the desired thing

    date -j -f "%Y-%m-%d %H:%M:%S" "2012-06-22 14:48:36" "+%s"

  9. #8
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    I would be surprised, if the date of freebsd 5.4 cannot do this operation.
    However, I do not have such a system installed for testing.

    So my suggestion for you is to study the man page for the correct syntax.
    You must always face the curtain with a bow.

  10. #9
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    195
    If you have a reasonable awk version you could try something like this. Caveats about logging, error handling, etc... as per Irithori
    's post also apply here, meaning you should do some.

    Code:
    #!/bin/bash
    
    diff="$(awk -v logtime="2012-06-22 14:47:44,905" \
            'BEGIN \
            {
                    sub( ",.*", "", logtime);
                    gsub( "[-:]", " ", logtime);
                    prev = mktime( logtime);
                    print systime() - prev;
            }' )"
    
    echo $diff
    It's the same idea, convert to seconds since epoch, subtract and then compare to your limit. But if your "date" is hosed, your "awk" might be in better shape. If you have "gawk" it will definitely work. But to tell you the truth I found date math so frustrating in shell scripts that I gave up and wrote a C program to do it for me eons ago.

  11. #10
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    Hi.

    See 250+ line ksh script at days elapsed between 2 dates - The UNIX and Linux Forums

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •