Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

perl logic to calculate total time taken excluding non working hours

by sachinhere (Novice)
on Jan 29, 2018 at 06:06 UTC ( [id://1208026]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I am looking for Perl code logic for report of time taken on task excluding non-working hours. working time is 8.30 am to 5 PM EST.Suppose task start at 4:30 PM and end on 9:00 AM of next day so it means time taken on task is 1 hour excluding non working hours. Please help I am using Mysql database. I tried timediff function of mysql but it return total time considering non working hours.

Replies are listed 'Best First'.
Re: perl logic to calculate total time taken excluding non working hours
by soonix (Canon) on Jan 29, 2018 at 06:49 UTC
Re: perl logic to calculate total time taken excluding non working hours
by ablanke (Prior) on Jan 29, 2018 at 10:46 UTC
    Hi sachinhere,

    Date::Manip offers a lot of date/time operation.

    use strict; use warnings; use Date::Manip; my $task_start = new Date::Manip::Date; my $task_end = new Date::Manip::Date; my $err = $task_start->parse('Jan 30 2018 4:30 PM EST'); $err = $task_end->parse('Jan 31 2018 9:00 AM EST'); my $delta = $task_start->calc($task_end); print $delta->value()."\n"; #0:0:0:0:16:30:0 $task_start->config('WorkDayBeg','08:30:00'); $task_start->config('WorkDayEnd','17:00:00'); $task_end->config('WorkDayBeg','08:30:00'); $task_end->config('WorkDayEnd','17:00:00'); $delta = $task_start->calc($task_end,"business"); print $delta->value()."\n"; #0:0:0:0:1:0:0
Re: perl logic to calculate total time taken excluding non working hours
by tinita (Parson) on Jan 29, 2018 at 10:24 UTC
Re: perl logic to calculate total time taken excluding non working hours
by thanos1983 (Parson) on Jan 29, 2018 at 11:50 UTC

    Hello sachinhere,

    I put together an example using Date::Manip module as a possible complete solution to your problem.

    Sample of solution bellow:

    What you need to notice here is, for the module the default working (business) hours are between 08:00 am - 5:00 pm (1 hour lunch in between). If these times do not fit your criteria (e.g. you prefer start at 08:30 am and end 5:30 pm) you can modify them, read more on how to do it here BUSINESS CONFIGURATION VARIABLES.

    Regarding the Adding the Mode options e.g. "business" explanation. This parameter that you can add on the DateCalc function is $mode. This parameter when you do date calculations can take 6 different options. Sample from the documentation:

    Each $mode has a different ability and output. For example $mode exact will return only hours and seconds and not days/weeks/months/years. If you are wondering why you can read here EXACT, SEMI-EXACT, AND APPROXIMATE DATE/DELTA CALCULATIONS.

    The calculations that you can do through this module are almost infinite, I tried to add the minimum possible modifications to resolve your problem. I would encourage you to read as much as possible the documentation to understand the output and how you can adjust it on your requirements (timezone, working hours, holidays etc...etc...).

    Update: Maybe I miss understood your initial question. I was under the impression that you want to calculate the time between non working hours. If this is what you want the code above should work, in case you do not want to do that and you are only interested in calculating the time between working hours see bellow:

    Update 2: Another "similar question" was raised some time ago see Calculating Number Of Pay Periods. On this thread apart from calculating the time between two days during business hours you can also calculate between time-zones see Re: Calculating Number Of Pay Periods (UPDATED).

    I hope this helps, BR

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: perl logic to calculate total time taken excluding non working hours
by karlgoethebier (Abbot) on Jan 29, 2018 at 11:40 UTC

    See also

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: perl logic to calculate total time taken excluding non working hours
by Anonymous Monk on Jan 29, 2018 at 12:15 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found