给定一个日期,返回该日期所在季度前一个季度的最后一天的日期。例如
2020-04-25 => 2020-03-31
2020-06-25 => 2020-03-31
2020-09-25 => 2020-06-30
2020-10-25 => 2020-09-30
如果给定日期在第一季度,则减去年份1
2020-03-25 => 2019-12-31
sub MAIN(Date(Str) $date) {
say $date.earlier(months => ($date.month - 1) % 3 + 1).last-date-in-month
}
这至少需要 Rakudo 2020.05。
sub MAIN(Str $day) {
my Date $d = Date.new($day);
my Int $year = $d.year;
my Int $month = $d.month;
my Int $quarter = ($month/3).ceiling; # the quarter that this date falls
my @last-days = ('12-31', '03-31', '06-30', '09-30');
# if the given date is in the first quarter, then `$year` minus 1
$year-=1 if $quarter == 1;
say sprintf('%s-%s', $year, @last-days[$quarter-1]);
}
将上述代码另存为quarter.raku
,输出为:
$ raku quarter.raku 2020-03-25
2019-12-31
$ raku quarter.p6 2020-04-25
2020-03-31
$ raku quarter.p6 2020-06-25
2020-03-31
$ raku quarter.p6 2020-07-25
2020-06-30
$ raku quarter.p6 2020-08-25
2020-06-30
$ raku quarter.p6 2020-09-25
2020-06-30
$ raku quarter.p6 2020-10-25
2020-09-30