-
Notifications
You must be signed in to change notification settings - Fork 4
/
WorkdayCalendar.pm
454 lines (350 loc) · 15.6 KB
/
WorkdayCalendar.pm
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use v6;
class WorkdayCalendar {
has Str $.file is rw;
has Date @.holidays is rw;
has Str @.workdays is rw = <Mon Tue Wed Thu Fri>;
method clear {
@.workdays = <Mon Tue Wed Thu Fri>;
@.holidays = ();
}
multi method new(Str :$filename!) {
my $c = WorkdayCalendar.new;
$c.read($filename);
return $c;
}
multi method new(Str $filename) {
return WorkdayCalendar.new(filename=>$filename);
}
method perl {
return "WorkdayCalendar.new(" ~ @.workdays.perl ~ ", " ~ @.holidays.perl ~ ", " ~ $.file.perl ~ ")";
}
method read(Str $calendar_filename) {
my $CAL = open $calendar_filename or die "Can't open $calendar_filename : $!";
$.file = $calendar_filename;
self.clear;
for $CAL.lines -> $line {
next if $line ~~ /^'#'/; #--- Comments are skipped
my ($type, $data) = split /':'/, $line;
given $type {
when 'H' {
my ($year, $month, $day) = split /'/'|'-'/, $data; #--- Only Holidays '
try {
push @.holidays, Date.new("$year-$month-$day");;
CATCH {
note "ERROR: Specifying holiday date. Date $year-$month-$day skipped";
next;
}
}
}
when 'W' {
try {
my @workweek_spec = split /','/, $data;
my $workweek_spec_failed = False;
for @workweek_spec -> $weekday {
if ($weekday ne any(<Mon Tue Wed Thu Fri Sat Sun>)) {
warn "Workday '$weekday' not recognized";
$workweek_spec_failed = True;
last;
}
}
if ($workweek_spec_failed) {
note "ERROR: Workweek specification not valid. Assuming Mon,Tue,Wed,Thu,Fri";
@.workdays = <Mon Tue Wed Thu Fri>;
next;
} else {
@.workdays = @workweek_spec;
}
}
}
}
}
@.holidays .= sort;
close $CAL;
}
method is-workday(Date $day){
return !(self.is-weekend($day) || self.is-holiday($day));
}
method is-weekend(Date $day) {
my $weekday_name = <Mon Tue Wed Thu Fri Sat Sun>[$day.day-of-week - 1];
return ($weekday_name ne any(@.workdays));
}
method is-holiday(Date $day) {
my @daycounts;
push @daycounts, $_.daycount for @.holidays;
return ?($day.daycount == any(@daycounts));
}
method workdays-away(Date $start, Int $days) {
return $start if $days == 0;
my Date $current_day = $start;
for (1 .. $days.abs) {
repeat {
$current_day = ($days > 0) ?? $current_day.Date::succ
!! $current_day.Date::pred;
} until self.is-workday($current_day);
}
return $current_day;
}
method workdays-to(Date $start is copy, Date $target is copy) {
return 0 if $start.daycount == $target.daycount;
my $sign = ($start.daycount < $target.daycount) ?? +1
!! -1;
my Date $current_day = $start;
my Int $count = 0;
repeat {
$current_day = ($sign == +1) ?? $current_day.Date::succ
!! $current_day.Date::pred;
$count++ if self.is-workday($current_day);
} until ($current_day.daycount == $target.daycount);
return ($count * $sign);
}
method networkdays(Date $start is copy, Date $target is copy) {
if $start.daycount == $target.daycount {
return (self.is-workday($start)) ?? 1
!! 0;
}
my $sign = +1;
if $start.daycount > $target.daycount {
$sign = -1;
my Date $aux_day = $start;
$start = $target;
$target = $aux_day;
}
my Date $current_day = $start;
my Int $count = 0;
while ($current_day.daycount <= $target.daycount) {
$count++ if self.is-workday($current_day);
$current_day = $current_day.Date::succ;
}
return $count * $sign;
}
method range(Date $begin, Date $end) {
my Date @slice;
my Int $from = ($begin.daycount, $end.daycount).min;
my Int $to = ($begin.daycount, $end.daycount).max;
for (@.holidays) -> $date { #--- The holidays are already sorted
push @slice, $date if ($from <= $date.daycount <= $to);
}
my $result_calendar = self.clone; #-- Requires a customized version of clone
$result_calendar.holidays = @slice;
return $result_calendar;
}
method clone {
my WorkdayCalendar $new = WorkdayCalendar.new;
given self {
$new.workdays = .workdays;
$new.holidays = .holidays;
$new.file = .file;
}
return $new;
}
}
#------------------------------------------------------------------------------#
class Workdate is Date {
has WorkdayCalendar $.calendar is rw;
# We try to provide the same constructors as the Date class
multi method new(:$year!, :$month, :$day, :$calendar = WorkdayCalendar.new) {
#my $D = callwith(year=>$year, month=>$month, day=>$day);
my $D = self.Date::new(year=>$year, month=>$month, day=>$day);
$D.calendar = $calendar;
return $D;
}
multi method new($year, $month, $day, $calendar = WorkdayCalendar.new) {
#my $D = callwith(year=>$year, month=>$month, day=>$day);
my $D = self.Date::new(year=>$year, month=>$month, day=>$day);
$D.calendar = $calendar;
return $D;
}
multi method new(Str $date, $calendar = WorkdayCalendar.new) {
#my $D = callwith($date);
my $D = self.Date::new($date);
$D.calendar = $calendar;
return $D;
}
multi method new(DateTime $dt, $calendar = WorkdayCalendar.new) {
#my $D = callwith($dt);
my $D = self.Date::new($dt);
$D.calendar = $calendar;
return $D;
}
multi method new(Date $d, $calendar = WorkdayCalendar.new) {
#my $D = callwith(year=>$d.year, month=>$d.month, day=>$d.day);
my $D = self.Date::new(year=>$d.year, month=>$d.month, day=>$d.day);
$D.calendar = $calendar;
return $D;
}
method succ { return $.calendar.workdays-away(self, +1) }
method pred { return $.calendar.workdays-away(self, -1) }
method is-workday { return $.calendar.is-workday(self) }
method is-weekend { return $.calendar.is-weekend(self) }
method is-holiday { return $.calendar.is-holiday(self) }
method workdays-away(Int $days) {
return $.calendar.workdays-away(self, $days);
}
method workdays-to(Date $target) {
return $.calendar.workdays-to(self, $target)
}
method networkdays(Date $target) {
return $.calendar.networkdays(self, $target)
}
method perl {
say "Workday.new($.year, $.month, $.day, " ~ $.calendar.perl ~ ")";
}
}
#------------------------------------------------------------------------------#
multi infix:<eq>(WorkdayCalendar:D $wc1, WorkdayCalendar:D $wc2) is export {
#--- No support for typed arrays yet AFAIK. Have to compare them in a "stringy" way
my Str (@wc1_string_holidays, @wc2_string_holidays);
for $wc1.holidays { push @wc1_string_holidays, "{$_.year}-{$_.month}-{$_.day}" };
for $wc2.holidays { push @wc2_string_holidays, "{$_.year}-{$_.month}-{$_.day}" };
my Bool $same_workdays = ($wc1.workdays ~~ $wc2.workdays);
my Bool $same_holidays = (@wc1_string_holidays ~~ @wc2_string_holidays);
return ?( $same_workdays && $same_holidays );
}
multi infix:<ne>(WorkdayCalendar:D $wc1, WorkdayCalendar:D $wc2) is export {
return !($wc1 eq $wc2);
}
multi infix:<->(Workdate:D $start, Workdate:D $target) is export {
if ($start.calendar ne $target.calendar) {
die "Both Workdates must have equivalent calendars to substract them";
}
return -1 * $start.workdays-to($target);
}
=begin pod
=head1 Introduction
The module provides two classes: C<WorkdayCalendar> and C<Workday>. Objects
of these classes allow date calculations to be made on a calendar that
takes workdays (also called "business days") into account.
Built on top of the C<Date> datatype, it uses a calendar file to specify how
many days a workweek has and which days are to be considered holidays.
By default, the I<workweek> is composed by B<Mon>, B<Tue>, B<Wed>, B<Thu>,
and B<Fri>. B<Sat> and B<Sun> form the I<weekend>.
Alhough most countries have a workweek of B<Mon> to B<Fri>, some have very
different ones.
More information about workweeks can be found at
L<http://en.wikipedia.org/wiki/Workweek>.
=head1 Calendar File format
# An example calendar file
W:Mon,Tue,Wed,Thu,Fri
H:2011/01/01
H:2011-04-05
This calendar specifies that B<Mon> to B<Fri> are to be considered workdays,
and that 2011/01/01 and 2011/04/05 are national holidays. You can use
C</> or C<-> as separators in a date. The format of the date B<must be> in
the order Year, Month, Day.
If the C<W:> specification is incorrect, the default workweek (B<Mon>,
B<Tue>, B<Wed>, B<Thu>, B<Fri>) is used. If a holiday (a row starting with
C<H:>) is not well defined, it is ignored.
Lines starting with C<#> are comments and will be ignored when parsing the file.
=head1 C<WorkdayCalendar> class
=head2 C<method new>
my $wdc1 = WorkdayCalendar.new;
my $wdc2 = WorkdayCalendar.new('calendar.cal');
Creates a new calendar. Optionally, accepts the name of a file using the
calendar format specified above. If a filename is not specified, the
calendar will have no holidays and a default workweek of B<Mon>, B<Tue>,
B<Wed>, B<Thu>, B<Fri>.
=head2 C<method clear>
Empties the information for holidays and workdays, and resets the
workweek to the default: B<Mon>, B<Tue>, B<Wed>, B<Thu>, B<Fri>.
=head2 C<method read(Str $calendar_filename)>
Reads the data of holidays and workdays from a calendar file.
=head2 C<method is-workday(Date $day)>
Returns C<True> if the day is part of the workweek and not a holiday.
=head2 C<method is-weekend(Date $day)>
Returns C<True> if the day is not part of the workweek.
=head2 C<method is-holiday(Date $day)>
Returns C<True> if the day has been defined as holiday in the calendar file.
=head2 C<method workdays-away(Date $start, Int $days)>
Returns a C<Date> that corresponds to the workday at which C<$days> working
days have passed. With this method you can ask questions like: "what is the
next working day for some date?" or "what is the previous working day of
some date?" or "what date is 2 working days from a date?".
Examples:
Considering the workdays = B<Mon Tue Wed Thu Fri>...
$start : July 29, 2011 (it is a Friday)
$days : +1
Return Value : Aug 1, 2011 (it is a Monday)
$start : July 30, 2011 (it is a Saturday)
$days : +1
Return Value : Aug 1, 2011 (it is a Monday)
This also works for a negative number of days.
=head2 C<method workdays-to(Date $start, Date $target)>
Returns the 'distance', in workdays, of C<$start> and C<$target> dates.
=head2 C<method networkdays(Date $start, Date $target)>
Works like the C<workdays-to> method, but emulates the NETWORKDAYS function in
Microsoft Excel.
Examples:
Start Target workdays-to networkdays
2011-07-07 2011-07-14 5 6
2011-07-07 2011-07-07 0 1
2011-07-07 2011-07-08 1 2
2011-07-07 2011-07-01 -4 -5
2011-01-01 2011-01-01 0 0
2011-01-01 2011-01-02 0 0
2011-01-01 2011-01-03 1 1
=head2 C<method range(Date $start, Date $end)>
Returns a part of a calendar as a new C<WorkdayCalendar> object, between the
C<$start> and C<$end> dates, inclusive. For example, if you have a calendar
that contains holiday information for 3 years, you can use C<range> to
obtain a new calendar that covers a period of 6 months of these 3 years.
Useful with the C<eq> operator for C<WorkdayCalendar> objects.
=head2 C<method perl>
Returns a string representing the contents of the C<WorkdayCalendar> attributes.
=head1 C<Workdate> class
Implemented as a subclass of C<Date>. It replaces C<Date>'s C<.succ> and C<.pred>
methods to take workdays into account and provides the functionality to perform
basic workdate calculations.
You can specify a previously created C<WorkdayCalendar> object as a parameter,
or none at all. If a C<WorkdayCalendar> is not specified, it uses a default
workweek of B<Mon> , B<Tue>, B<Wed>, B<Thu>, B<Fri> and no holidays.
Example:
# July 1st of 2011 is a Friday
my $wdate = Workdate.new(year=>2011, month=>07, day=>01); #--- Uses a default calendar with
#--- default workweek and no holidays
my $next_day = $wdate.succ; # $next_day is Monday, July 4, 2011
Another example:
my $CAL = WorkdayCalendar.new('example.cal'); # Some calendar file with 2011-Feb-2 as holiday
my $date = Workdate.new(year=>2011, month=>02, day=>01, calendar=>$CAL);
# February 1 of 2011 is a Tuesday
my $next_day = $date.succ; # $next_day is Thursday, February 3, 2011
=head2 C<method new>
my $wd1 = Workdate.new(year=>2000, month=>12, day=>01, calendar=>$aWorkdayCalendar);
my $wd2 = Workdate.new(2000, 12, 01, $aWorkdayCalendar);
my $wd3 = Workdate.new($aDateString, $aWorkdayCalendar);
my $wd4 = Workdate.new($aDateTimeObject, $aWorkdayCalendar);
my $wd4 = Workdate.new($aDateObject, $aWorkdayCalendar);
We try to provide the same constructors as the base Date class, plus another
to create C<Workdate>s from regular C<Date>s. Thus, we can create a
C<Workdate> in 4 different ways, from named and positional parameters, and
by using a C<Date> or a C<DateTime> object for specifying the date. In all
cases, the calendar is optional, and if it is not specified a default calendar
will be applied to the new C<Workdate>.
=head2 C<method succ>
Returns the next workdate.
=head2 C<method pred>
Returns the previous workdate.
=head2 C<method is-workday>
Returns True if the workdate is not a holiday and is not part of the weekend.
=head2 C<method is-weekend>
Returns True if the workdate is not part of the workweek.
=head2 C<method is-holiday>
Returns True if the workdate is reported as a holiday.
=head2 C<method workdays-away(Int $days)>
Returns the workdate that is C<$days> workdays from the given workdate.
=head2 C<method workdays-to(Date $target)>
Return the number of workdays until C<$target>.
=head2 C<method perl>
Returns a string representing the contents of the C<Workdate> attributes.
=head1 Operators
=head2 Comparison: C<$WorkdayCalendar_1 B<eq> $WorkdayCalendar_2>
Compares two calendars and returns True if they are equivalent. For that,
they must have the same holidays and the same workweek. For instance, this
would be as if they used the same calendar file.
You can use the C<range> method for C<WorkdayCalendar> objects to compare
smaller periods of time instead of a whole C<WorkdayCalendar>.
=head2 Comparison: C<$WorkdayCalendar_1 B<ne> $WorkdayCalendar_2>
Returns the opposite of C<eq>.
=head2 Arithmetic: C<Workdate $wd1 B<-> Workdate $wd2>
Returns the difference, in workdays, between C<$wd1> and C<$wd2>.
=end pod
# vim: ft=perl6