Perl - 替代 Date::Calc 来计算年度儒略日?

Perl - replacement for Date::Calc to calculate Julian Day of the Year?

我不是 Perl 程序员,但我正在开发一个使用 Date::Calc 模块的程序,就像这样

use Date::Calc ("Day_of_Year")
my $jday = Day_of_Year($yr[$n], $mon[$n}, $day[$n]);

我的模块一直有问题,我需要在我的代码中替换上面计算的 Julian day of year。

有人有什么想法吗?我没有安装模块的权限,所以我需要一个基本解决方案。

参见 Date::Converter。如果您无法安装模块(甚至无法安装到您自己的主目录,真的吗?),只需将相关代码复制到您的即可。

#!/usr/bin/perl
use strict;
use warnings;

use Time::Local qw[timegm];

# Computes the day of the year [1, 366] from the given year, month of the 
# year and day of the month.
# https://metacpan.org/pod/Time::Local#Year-Value-Interpretation
# 1 <= $m <= 12
# 1 <= $d <= 31 (day must be valid for the year and month)
sub ymd_to_doy {
    @_ == 3 or die q/Usage: ymd_to_doy(year, month, day)/;
    my ($y, $m, $d) = @_;
    my $t1 = timegm(0, 0, 0, $d, $m - 1, $y);
    my $t2 = timegm(0, 0, 0, 1, 0, $y);
    return 1 + int(($t1 - $t2) / 86_400);
}

use v5.10;
say ymd_to_doy(2015, 10, 11);
say ymd_to_doy(2015,  1,  1);
say ymd_to_doy(2015, 12, 31);
say ymd_to_doy(2012, 12, 31);

输出:

284
1
365
366