Forum Moderators: coopster & phranque

Message Too Old, No Replies

math and perl

math and perl

         

oilman

5:09 pm on Nov 21, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need a script that will take a start time (ie 7:30) and an end time (ie 4:30) and a lunch time (ie 1 hour) and calculate the total hours and minutes worked. Anybody ever come across something like this? I have tried to write it myself but I haven't got to that lesson yet ;)

sugarkane

11:38 pm on Nov 24, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dunno if this is too late for you, but this might help:
----

#!/usr/bin/perl

####
$start="7:30";
$finish="4:30";
$lunch="1:00";
####

($start_hr, $start_min)=split(/:/, $start);
($finish_hr, $finish_min)=split(/:/, $finish);
($lunch_hr, $lunch_min)=split(/:/, $lunch);

if ($finish_hr < $start_hr) {$finish_hr=$finish_hr+12;}

$start_foo=($start_hr*60)+$start_min;
$finish_foo=($finish_hr*60)+$finish_min;
$lunch_foo=($lunch_hr*60)+$lunch_min;

$total_foo=$finish_foo-$lunch_foo-$start_foo;

$hours=int($total_foo/60);
$mins=$total_foo-($hours*60);

print "$hours:$mins";

----

I've only given it a cursory test, but seems to work...

Hope it helps :)

sugarkane

11:46 pm on Nov 24, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW I've just noticed that the above barfs if the working day is longer than 12 hours, but then no one here would ever dream of working that much surely ;)

Solved easily by using a 24hr time format and substituting:
if ($finish_hr < $start_hr) {$finish_hr=$finish_hr+12;}
with:
if ($finish_hr < $start_hr) {$finish_hr=$finish_hr+24;}

(which would then barf at a working day of more than 24hrs, but what the hey...)

oilman

2:32 am on Nov 25, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks sugarkane - I will use that for sure. ;)

sugarkane

3:26 am on Nov 25, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember to send the royalty cheques to the usual address ;)

Air

3:57 am on Nov 25, 2000 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



good one SK ...