aboutsummaryrefslogtreecommitdiff
path: root/challenge-178/athanasius/perl/ch-2.pl
blob: accd13bc9a0f04585c853434da2a7c6262d51c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!perl

###############################################################################
=comment

Perl Weekly Challenge 178
=========================

TASK #2
-------
*Business Date*

Submitted by: Mohammad S Anwar

You are given $timestamp (date with time) and $duration in hours.

Write a script to find the time that occurs $duration business hours after
$timestamp. For the sake of this task, let us assume the working hours is 9am
to 6pm, Monday to Friday. Please ignore timezone too.

For example,

  Suppose the given timestamp is 2022-08-01 10:30 and the duration is 4 hours.
  Then the next business date would be 2022-08-01 14:30.

  Similar if the given timestamp is 2022-08-01 17:00 and the duration is 3.5
  hours.
  Then the next business date would be 2022-08-02 11:30.

=cut
###############################################################################

#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#

#==============================================================================
=comment

Assumption
----------
All calculations are to the nearest whole minute.

Implementation
--------------
Calculations are performed by the CPAN module DateTime.

=cut
#==============================================================================

use strict;
use warnings;
use Const::Fast;
use DateTime;
use Regexp::Common qw( number );
use Try::Tiny;

use enum qw( Monday=1 Tuesday Wednesday Thursday Friday Saturday Sunday );

const my $MINUTE  =>  1 / 60;
const my $START   =>  9;
const my $END     => 18;
const my $BUS_HRS => $END - $START;
const my $USAGE   =>
qq[Usage:
  perl $0 <timestamp> <duration>

    <timestamp>    Timestamp, e.g., "2022-08-01 10:30"
    <duration>     Duration in hours\n];

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 178, Task #2: Business Date (Perl)\n\n";
}

#==============================================================================
MAIN:
#==============================================================================
{
    my ($dt0, $duration) = parse_command_line();

    printf "Input\n  Timestamp: %s %02d:%02d\n  Duration:  %s hours\n\n",
        $dt0->ymd, $dt0->hour, $dt0->minute, $duration;

    my $dow   = $dt0->day_of_week;
    my $hours = $dt0->hour + ($dt0->minute / 60);
    my $dt1   = ((Monday <= $dow <= Friday) &&
                 ($END - $hours) >= ($duration - $MINUTE))
                ? same_day( $dt0, $duration )
                : advance ( $dt0, $duration, $dow );

    printf "Output\n  Timestamp: %s %02d:%02d\n",
        $dt1->ymd, $dt1->hour, $dt1->minute;
}

#------------------------------------------------------------------------------
sub same_day
#------------------------------------------------------------------------------
{
    my ($dt, $duration) =  @_;

    my  $start      =  $dt->hour + ($dt->minute / 60);
        $start      =  $START if $start < $START;
    my  $new_time   =  $start + $duration;
    my  $hours      =  int( $new_time );
    my  $remainder  = ($new_time - $hours) * 60;
    my  $minutes    =  int( $remainder );
        $remainder -=  $minutes;
    my  $seconds    =  int( $remainder * 60 );

    return DateTime->new(
                            year   => $dt->year,
                            month  => $dt->month,
                            day    => $dt->day,
                            hour   => $hours,
                            minute => $minutes,
                            second => $seconds,
                        );
}

#------------------------------------------------------------------------------
sub advance
#------------------------------------------------------------------------------
{
    my ($dt, $duration, $dow) = @_;
    my  $dur_remaining        = $duration;

    if (1 <= $dow <= 5 && $dt->hour < $END)
    {
        my $start =  $dt->hour + ($dt->minute / 60);
           $start =  $START if $start < $START;

        $dur_remaining -= $END - $start;
    }

    $dt = get_next_day( $dt );

    while (($dur_remaining - $BUS_HRS) > $MINUTE)
    {
        $dt             = get_next_day( $dt );
        $dur_remaining -= $BUS_HRS;
    }

    $dt->set( hour => 0, minute => 0, second => 0 );

    return same_day( $dt, $dur_remaining );
}

#------------------------------------------------------------------------------
sub get_next_day
#------------------------------------------------------------------------------
{
    my ($dt)   =  @_;
    my  $dow   =  $dt->day_of_week;
    my  $delta = ($dow == Friday  ) ? 3 :
                 ($dow == Saturday) ? 2 : 1;

    return $dt->add( days => $delta );
}

#------------------------------------------------------------------------------
sub parse_command_line
#------------------------------------------------------------------------------
{
    my $args = scalar @ARGV;
       $args == 2 or error( "Expected 2 command line arguments, found $args" );

    my ($timestamp, $duration) = @ARGV;

    my ($year, $month, $day, $hour, $minute) =
        $timestamp =~ / (\d{4}) - (\d{2}) - (\d{2}) \s+ (\d{1,2}) : (\d{2}) /x
                       or error( qq[Timestamp "$timestamp" is invalid] );

    1 <= $month  <= 12 or error( qq["$month" is not a valid month] );
    1 <= $day    <= 31 or error( qq["day" is not a valid day of the month] );
    0 <= $hour   <= 23 or error( qq["$hour" is not a valid hour] );
    0 <= $minute <= 59 or error( qq["$minute" is not a valid minute] );

    my $dt0;

    try
    {
        $dt0 = DateTime->new(
                                year   => $year,
                                month  => $month,
                                day    => $day,
                                hour   => $hour,
                                minute => $minute,
                            );
    }
    catch
    {
        error( 'Invalid date' );
    };

    $duration =~ / ^ $RE{num}{real} $ /x
                   or error( qq["$duration" is not a valid real number] );
    $duration >= 0 or error( 'Negative duration is not supported' );

    return ($dt0, $duration);
}

#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

###############################################################################