Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi , I have two times as
$t1 = "1245"; $t2 = "0150";
How do I get their time difference as 65 minutes. When I try its coming as 1095,,,please let me know what to do. Thank you Andy

Replies are listed 'Best First'.
Re: time diff in minutes
by ssandv (Hermit) on Aug 11, 2009 at 17:30 UTC

    There's two answers to this, and it depends on what you're really trying to accomplish. If this is homework/you learning about Perl and computer programming, then you'd be best served to spend the time figuring out how to solve problems like this. The first, and hardest, part of solving problems with a computer is to explain clearly what you need to do. In this case, it's pretty clear that there's a series of steps a human goes through to determine that 12:45 is 65 minutes before 01:50--if you can figure out what those steps are, then you can then convert those steps into code that the computer understands.

    If, on the other hand, this is something for work, and you just need it done *now*, you should check CPAN and or what you have locally available for modules that handle time conversion. I'm pretty sure Time::Local is good enough, but there are probably better ones available.

    If you gave more context for your problem, it would be easier to give you a more specific answer. Also, please tell us (actually, *show* us) what you've already tried.

Re: time diff in minutes
by SuicideJunkie (Vicar) on Aug 11, 2009 at 17:33 UTC

    12:45 is not 1245 minutes, so you clearly can't just use subtraction. You need to get the actual number of minutes first. (12*60+45 = 765)

    Then you have to figure out whether 1:50 means 1:50 am or pm (aka 13:50). Also, what day is it?

    The trick to these homework problems is to break it down into all the little steps that you don't realize you're doing when you do it manually. Once you've written down the steps (don't skip anything!), you can then convert that into code that the computer can run.

Re: time diff in minutes
by ikegami (Patriarch) on Aug 11, 2009 at 17:42 UTC
    Note that depending on the date and time zone, the difference could be 13 hours, 5 minutes or 14 hours, 5 minutes.
Re: time diff in minutes
by NetWallah (Canon) on Aug 11, 2009 at 17:33 UTC
    At most places on this planet,
    • An hour is made of 60 minutes
    • For P.M. time, add 12 to the hour, to get military time
    • Convert the time into Minutes
    Please use these pieces of information to solve your problem.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

      An hour is made of 60 minutes

      Not if you cross a leap second (made even more fun by their non-representation in POSIX time).

      (ed: Well, of course, it depends on what you actually mean by 'minutes', whether an hour with a positive leap second has 60 minutes or 60.166... minutes. But that just leads into the need to figure out exactly what question you're actually asking at any given moment. Stupid inconsistent world :)

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: time diff in minutes
by mikelieman (Friar) on Aug 11, 2009 at 18:12 UTC
    Clock starts at 00:00:00.

    The difference between 12:45 and 1:50 *is* 13 hours and 5 minutes.

    The difference between 12:45 and 13:50 is 65 minutes.

    It appears your data representation needs some consideration.

Re: time diff in minutes
by Marshall (Canon) on Aug 11, 2009 at 17:52 UTC
    The basic idea for this is to convert both times into "epoch" seconds. Do some subtraction in seconds. Then convert back to "human hours/min/secs time. Or in your case, you may want just 65 minutes.

    Epoch time is an signed integer value in number of seconds that usually starts at Jan 1, 1970, midnight for historical reasons.

    Look at Perl built in functions: gmtime, localtime, time. Then look at the module: Time::Local. That module give the inverse functions.

    For benchmarking, forget about the year, month, date. Normally just the time expressed in hours , min and seconds is all that is needed. In your case just minutes which is of course is seconds / 60.

    updated : modules and functions to look for.

Re: time diff in minutes
by perlofwisdom (Pilgrim) on Aug 11, 2009 at 20:48 UTC

    As SuicideJunkie pointed out "0150" is not the same as "1350". But here is some pared down code to convert "HH:MI" timestamps to "epoch":

    use Time::Local; my $t1 = "1245"; my $t2 = "1350"; my $beg = ConvHHMI($t1); my $end = ConvHHMI($t2); my $diff = $end - $beg; my $t3 = $diff / 60; print "Begin=$t1 ($beg), End=$t2 ($end), Diff=$t3 ($diff)\n"; sub ConvHHMI { my $w_date = shift @_; my ($sec,$min,$hour,$mday,$mon,$year,$misc) = (0,0,0,1,0,0,0); ($hour, $min) = $w_date =~ /^(\d\d)(\d\d)$/; return timelocal($sec,$min,$hour,$mday,$mon,$year); }

    Results:

    Begin=1245 (946759500), End=1350 (946763400), Diff=65 (3900)

Re: time diff in minutes
by bichonfrise74 (Vicar) on Aug 11, 2009 at 22:15 UTC
    Try this... Although this code can break easily.
    #!/usr/bin/perl use strict; my $t1 = "1245"; my $t2 = "0150"; Get_Time_Diff( $t1, $t2 ); sub Get_Time_Diff { my @times = @_; for my $i (0 .. $#times) { $times[$i] += 1200 if ( $times[$i] < '1200' ); $times[$i] = substr( $times[$i], 0, 2 ) * 60 + substr( $times[$i], 2, 4 ); } my $diff = $times[1] - $times[0]; print "Time difference is: $diff \n"; }
      Here's why I don't like the code in this answer:
      1. It gets the length of @times with $#times, but then later assume it has only two elements
      2. It prints, but the sub is called 'Get'
      3. It assumes times < 1200 are all in the afternoon!
      4. It doesn't use warnings
      5. It uses quotes to define constant integers

      Please consider DateTime


      - Boldra