2

I want to add to a date now, x hours and processing this dependant of businness calendar. I don't know how to explain otherwise so an example will be more practical.

My sample business calendar is:

  • Start time 9 a.m.
  • End time 6 p.m.
  • as well as various public holidays and weekends

I wish that :

  • date now : 18-07-2022 4:30 p.m.
  • to which I add 5 hours
  • I have the following result : 19-07-2022 12:30 p.m.

Of course in the case where a public holiday would be in the middle, we would skip it (weekend included). If I take my previous example but for the weekend it would be:

  • start date: 15-07-2022 4:30 p.m.
  • add 5 hours
  • end date : 18-07-2022 12:30 p.m.

I've already use a param business calandars as following :

my %sansSLA = (
          1 => { Name  => 'Monday',
                 Start => '8:00',
                 End   => '18:00'},
          2 => { Name  => 'Tuesday',
                 Start => '8:00',
                 End   => '18:00'},
          3 => { Name  => 'Wednesday',
                 Start => '8:00',
                 End   => '18:00'},
          4 => { Name  => 'Thursday',
                 Start => '8:00',
                 End   => '18:00'},
          5 => { Name  => 'Friday',
                 Start => '8:00',
                 End   => '18:00'},
          6 => { Name  => 'Saturday',
                 Start => undef,
                 End   => undef},
          0 => { Name  => 'Sunday',
                 Start => undef,
                 End   => undef},
          holidays => [qw(01-01 05-01 05-08 07-14 08-15 11-11 12-25 2022-05-18 2022-05-26 2023-0410 2023-04-10 2023-05-18 2024-04-01 2024-05-09 2025-04-21 2025-05-29 2026-04-06 2026-05-14 2027-03-29 2027-05-06 2028-04-17 2028-05-25 2029-04-02 2029-05-10 2030-04-22 2030-05-30)],
   );

or :

my %sla8_18 = (
          1 => { Name  => 'Monday',
                 Start => '8:00',
                 End   => '18:00'},
          2 => { Name  => 'Tuesday',
                 Start => '8:00',
                 End   => '18:00'},
          3 => { Name  => 'Wednesday',
                 Start => '8:00',
                 End   => '18:00'},
          4 => { Name  => 'Thursday',
                 Start => '8:00',
                 End   => '18:00'},
          5 => { Name  => 'Friday',
                 Start => '8:00',
                 End   => '18:00'},
          6 => { Name  => 'Saturday',
                 Start => '8:00',
                 End   => '18:00'},
          0 => { Name  => 'Sunday',
                 Start => undef,
                 End   => undef},
          holidays => [qw(01-01 05-01 05-08 07-14 08-15 11-11 12-25 2022-05-18 2022-05-26 2023-0410 2023-04-10 2023-05-18 2024-04-01 2024-05-09 2025-04-21 2025-05-29 2026-04-06 2026-05-14 2027-03-29 2027-05-06 2028-04-17 2028-05-25 2029-04-02 2029-05-10 2030-04-22 2030-05-30)],
   );

and

my %slaMax = (
          1 => { Name  => 'Monday',
                 Start => '00:00',
                 End   => '23:59'},
          2 => { Name  => 'Tuesday',
                 Start => '00:00',
                 End   => '23:59'},
          3 => { Name  => 'Wednesday',
                 Start => '00:00',
                 End   => '23:59'},
          4 => { Name  => 'Thursday',
                 Start => '00:00',
                 End   => '23:59'},
          5 => { Name  => 'Friday',
                 Start => '00:00',
                 End   => '23:59'},
          6 => { Name  => 'Saturday',
                 Start => '00:00',
                 End   => '23:59'},
          0 => { Name  => 'Sunday',
                 Start => '00:00',
                 End   => '23:59'},
          #holidays => [qw(01-01 05-01 05-08 07-14 08-15 11-11 12-25 2022-05-18 2022-05-26 2023-0410 2023-04-10 2023-05-18 2024-04-01 2024-05-09 2025-04-21 2025-05-29 2026-04-06 2026-05-14 2027-03-29 2027-05-06 2028-04-17 2028-05-25 2029-04-02 2029-05-10 2030-04-22 2030-05-30)],
   );

thank you in advance for your help. :)

6
  • I would probably use metacpan.org/pod/DateTime and wrap it somehow. You will have to implement some of the business logic yourself, such as checking that your times are within the boundaries, an calculating offsets to apply to arguments when you add time.
    – simbabque
    Jul 18, 2022 at 8:09
  • Their is no fattest solution than wrap DateTime ? I wasn't think that what i want to do is as much specific and that nobody have already develop or integrated a solution in a library. Thanks for your help. I'll wait for other respons just in case. Have a good day.
    – Jimmy
    Jul 18, 2022 at 8:21
  • There are various date and time libraries. DateTime is not that heavy, but it depends. If you are using it in CGI it's borderline. If it's in a script that you use as an end-user, it doesn't really matter. For a PSGI based application, it's totally fine. Date::Manip has more special functionality. I haven't used it in a while, but i have used it to do next business day and things like that. Just not as specific as you need it. It is a very heavy module, that I really wouldn't want to load unless there is no other way. Much bigger than DateTime. Time::Piece is simple, and comes in core Perl.
    – simbabque
    Jul 18, 2022 at 8:24
  • What would be really cool is a generic wrapper around DateTime, some kind of subclass that you can tell what the constraints of your day are, and it just works, so you could do something like (and that is just a hypothetical interface I made up) my $tomorrow = DateTime::CustomWorkdays->now->add( hours => 5 )->iso8601 and it would produce your 2022-07-18T12:30:00 when now is 4:30pm the day before.
    – simbabque
    Jul 18, 2022 at 8:27
  • 1
    Well if it's for RT, you already have DateTime anyway (github.com/bestpractical/rt/blob/stable/etc/cpanfile#L21). I'm not familiar with their code base though.
    – simbabque
    Jul 18, 2022 at 10:17

1 Answer 1

0

Let us code this through.

sub add_time {
    my $time = shift; #Assuming timestamp in seconds
    my $add = shift;  #assuming seconds

    while ( $add > 0 ) {
        my $day = get_day_record($time);
        my $time_in_day_available = time_in_day ($time, $day);
        if ($add > $time_in_day_available) {
            $add = $add - $time_in_day_available;
            $time = get_timestamp_of_next_day($time);
        } else {
            $time = start_time_of_day ($time, $day) + $add;
            $add = 0;
        }
    }
    return $time;
}

The rest is also easy. (Check-for-holidays needs to be done)

sub get_day_record {
    my $time = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                localtime($time);

    if (check_for_holidays($time)) {
          return { Name  => 'holiday',
                 Start => undef,
                 End   => undef}
    } else {
        return $sansSLA{$wday};
    }
}

sub check_for_holidays {
    return ;
}


sub time_in_day {
    my $time = shift ;
    my $day = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                localtime($time);

    return 0
        unless $day->{Start};

    my ($end_hour, $end_min) = split(':', $day->{End});
    my $end_work_time = timelocal(0,$end_min,$end_hour,$mday,$mon,$year);

    return 0
        if $end_work_time < $time;

    my $start_work_time = start_time_of_day ($time, $day);

    return $end_work_time - $start_work_time;
}

sub start_time_of_day {
    my $time = shift ;
    my $day = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                localtime($time);


    my ($start_hour, $start_min) = split(':', $day->{Start});
    my $start_work_time = timelocal(0,$start_min,$start_hour,$mday,$mon,$year);

    return $time 
        if $start_work_time < $time;

    return $start_work_time;
}

sub get_timestamp_of_next_day {
    my $time = shift;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                localtime($time);
    return timelocal_nocheck(0,0,0,$mday+1,$mon,$year);
}   

And the whole thing in a single script to test this (including the use of the necessary Time::Local)

use strict;
use warnings;
use Time::Local qw(timelocal timelocal_nocheck);
my %sansSLA = (
    1 => { Name  => 'Monday',
            Start => '8:00',
            End   => '18:00'},
    2 => { Name  => 'Tuesday',
            Start => '8:00',
            End   => '18:00'},
    3 => { Name  => 'Wednesday',
            Start => '8:00',
            End   => '18:00'},
    4 => { Name  => 'Thursday',
            Start => '8:00',
            End   => '18:00'},
    5 => { Name  => 'Friday',
            Start => '8:00',
            End   => '18:00'},
    6 => { Name  => 'Saturday',
            Start => undef,
            End   => undef},
    0 => { Name  => 'Sunday',
            Start => undef,
            End   => undef},
    holidays => [qw(01-01 05-01 05-08 07-14 08-15 11-11 12-25 2022-05-18 2022-05-26 2023-0410 2023-04-10 2023-05-18 2024-04-01 2024-05-09 2025-04-21 2025-05-29 2026-04-06 2026-05-14 2027-03-29 2027-05-06 2028-04-17 2028-05-25 2029-04-02 2029-05-10 2030-04-22 2030-05-30)],
);

my $start = timelocal (0,0,0,18,06,122); # this is the 18 07 2022

print scalar localtime(add_time($start, 52*60*60));
7
  • Passing illegal values to timelocal_nocheck (as this code can do) sometimes leads to weird results.
    – ikegami
    Jul 18, 2022 at 20:15
  • @ikegami I am not convinced this issue can cause any problems here. Anyway, not using the timelocal_nocheck is easy. In my eyes though it's better to use a library function than write the code processing DST, Leap Seconds and so on by yourself. Jul 19, 2022 at 8:03
  • Re "In my eyes though it's better to use a library function than write the code processing DST, Leap Seconds and so on by yourself.", Why do you think I said otherwise!?!
    – ikegami
    Jul 19, 2022 at 13:18
  • (I'm not the one that downvoted. I too think this will work even with the illegal values. But I did encounter a case where it didn't, and the author refused to apply a trivial fix.)
    – ikegami
    Jul 19, 2022 at 13:32
  • (Linux doesn't have have leap seconds in its epoch time. As such, the same is true for Windows, and probably MacOS.)
    – ikegami
    Jul 19, 2022 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.