Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Subtract Hours

by Anonymous Monk
on Jun 22, 2009 at 13:49 UTC ( [id://773629]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, the below script gets 2 sepereate hour and give me the difference between them. (ex, 08:00 - 17:00 = 9Hrs) the only problem is when i enter somthing like this( ex, (23:00 - 6:00(Am) = 17:00 should be 7) any idea how can i modify this to handle my problem or maybe use other module for me needs
#!/usr/bin/perl use strict; use warnings; use Time::Local; sub str2time{ my $in = shift; my ($hour, $min, $sec) = split (/:/, $in); my ($mday,$mon,$year) = (localtime(time))[3,4,5]; my $retn; eval { $retn = timelocal($sec,$min,$hour,$mday,$mo +n,$year); }; return $retn; } print "Please enter the first time in HH:MM:SS format: "; my $intime = <STDIN>; chomp $intime; my $time1 = str2time($intime); die "Invalid time: $intime\n" if !defined $time1; print "Please enter the 2nd time in HH:MM:SS format: "; $intime = <STDIN>; chomp $intime; my $time2 = str2time($intime); die "Invalid time: $intime\n" if !defined $time1; my $diff; if ( $time1 > $time2 ) { $diff = $time1 - $time2} else { $diff = $time2 - $time1} my $mins = int($diff / 60); my $hours = int($mins / 60); $mins = $mins - ($hours * 60); my $secs = $diff - ($mins * 60); printf "Difference: %02d:%02d:%02d\n", $hours,$mins,$secs;

Replies are listed 'Best First'.
Re: Subtract Hours
by Limbic~Region (Chancellor) on Jun 22, 2009 at 14:21 UTC
    Anonymous Monk,
    It appears that what you want to do is simple. If the LHS < RHS then RHS - LHS. If the LHS > RHS then (24:00 - LHS) + RHS. Obviously if LHS == RHS then 0. Note: LHS = left hand side and RHS = right hand side.

    Cheers - L~R

Re: Subtract Hours
by si_lence (Deacon) on Jun 22, 2009 at 14:00 UTC
    You have to know if the first time is earlier than the second one. You could either ask for the date as well or assume that the first time entered is the earlier one and then handle your second case differently.

    Without the date you will always have to make some assumptions about your input - it could be more than 24 hours between the two times ...

    cheers

    si_lence

      Without the date you will always have to make some assumptions about your input

      Such as the lack to timezone change. For example, there can be 3 4, or 5 hours between 11pm and 3am depending on the date.

        ikegami,
        I really don't think Anonymous Monk is trying to do anything complicated. See Monthly hours report. I think this is just a matter of finding out how many hours between the first time entered and the second assuming you are always moving the clock forward (and not working for > 24 hrs consecutively). DST could still bite you but if you are writing your own code to manage your hours and haven't bothered to use a module - I say you get what you ask for.

        Cheers - L~R

Re: Subtract Hours
by CountZero (Bishop) on Jun 22, 2009 at 14:32 UTC
    Assuming both times are in the same timezone and no change in DST between the two the anser is correct "+ k times 24 hours, for every integer value of k".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Subtract Hours
by ikegami (Patriarch) on Jun 22, 2009 at 15:24 UTC

    Without date info, you can sort the times (if ( $time1 > $time2 )) or you can do what you asked, but you can't do both.

    You problem can be solved by adding 24 hours to the later time, and modding the hours by 24.

    use strict; use warnings; sub hms_to_s { my ($hms) = @_; my ($h,$m,$s) = split /:/, $hms; return $h*60*60 + $m*60 + ($s||0); } for ( [qw( 08:00:00 17:00:00 )], [qw( 23:00:00 06:00:00 )], ) { my ($time1_hms, $time2_hms) = @$_; my $time1 = hms_to_s($time1_hms); my $time2 = hms_to_s($time2_hms); my $diff = ( ( $time2 + 24*60*60 ) - $time1 ) % (24*60*60); my $diff_h = int( $diff / (60*60) ); my $diff_m = int( $diff / 60 ) % 60; my $diff_s = $diff % 60; printf("%s -> %s: %02d:%02d:%02d\n", $time1_hms, $time2_hms, $diff_h, $diff_m, $diff_s, ); }
    08:00:00 -> 17:00:00: 09:00:00 23:00:00 -> 06:00:00: 07:00:00

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://773629]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found