Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

convert timestamp hhmmss to seconds?

  • 30-01-2007 09:59PM
    #1
    Registered Users, Registered Users 2 Posts: 4,349 ✭✭✭


    Hi,

    i want to subtract two timestamps in unix, does anyone have a quick script for this? be it perl, shell whatever? the timestamp is of format hhmmss.


Comments

  • Registered Users, Registered Users 2 Posts: 1,421 ✭✭✭Steveire


    Here's some python:
    #!/usr/bin/env python
    import datetime
    def getDifference(timestamp1, timestamp2):
      hours1, minutes1, seconds1 = int(timestamp1[0:2]), int(timestamp1[2:4]), int(timestamp1[4:6])
      hours2, minutes2, seconds2 = int(timestamp2[0:2]), int(timestamp2[2:4]), int(timestamp2[4:6])
      t1 = datetime.datetime(1, 1, 1, hours1, minutes1, seconds1)
      t2 = datetime.datetime(1, 1, 1, hours2, minutes2, seconds2)
      difference = t1 - t2
      return difference.seconds
    
    timestamp1 = "112233"
    timestamp2 = "111010"
    print getDifference(timestamp1, timestamp2)
    # Prints 743
    


  • Registered Users, Registered Users 2 Posts: 4,349 ✭✭✭jon1981


    #!/usr/bin/perl
    
    
    sub convert_time{
    
    $time_in="$_[0]";
    $time_in=~/(\d{2})(\d{2})(\d{2})/;
    
    $hour = $1;
    $min = $2;
    $sec = $3;
    
    $time_sec = $sec + ($min*60) + ($hour*3600);
    
    }
    
    $time_end = $ARGV[0];
    $time_start = $ARGV[1];
    $duration = convert_time($time_end) - convert_time($time_start);
    print "$duration\n";
    
    cheers but i got off my lazy ass, this is crude but works


  • Registered Users, Registered Users 2 Posts: 2,780 ✭✭✭niallb


    You want crude? I'll give you crude :-)
    Bash style Shell - all on one line.
    for ts in 165959 090000 ; \
    do echo 60*$(echo $ts | cut -c 1-2,3-4,5-6 --output-delimiter="*60+")   ;\
     done | paste -d- - - | bc
    

    Ugly as sin, but when I press Return - I want answers :-)

    NiallB


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Do your own homework tbh. (A perl version of this thread was posted in the programming forum).


  • Registered Users, Registered Users 2 Posts: 4,349 ✭✭✭jon1981


    niallb wrote:
    You want crude? I'll give you crude :-)
    Bash style Shell - all on one line.
    for ts in 165959 090000 ; \
    do echo 60*$(echo $ts | cut -c 1-2,3-4,5-6 --output-delimiter="*60+")   ;\
     done | paste -d- - - | bc
    

    Ugly as sin, but when I press Return - I want answers :-)

    NiallB

    damn!!!


  • Advertisement
Advertisement