为什么 Date::Calc 抱怨“不是有效日期”?

发布于 2024-07-13 13:14:06 字数 335 浏览 5 评论 0原文

以前,这个函数对我有用......

$this_day = Day_of_Week($lyear, $month, $day);

使用这个库......

use Date::Calc qw(Add_Delta_Days Day_of_Week Delta_Days);

但我需要另一种方法来获取相同的信息。

它返回的错误是

Date::Calc::Day_of_Week(): not a valid date

Any ideas?

Previously, this function worked for me...

$this_day = Day_of_Week($lyear, $month, $day);

using this lib..

use Date::Calc qw(Add_Delta_Days Day_of_Week Delta_Days);

But I need another way to get this same info.

the error it returned is

Date::Calc::Day_of_Week(): not a valid date

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡紫姑娘! 2024-07-20 13:14:06

错误消息表明您正在向 Date::Calc 传递无效日期。 不要那样做。 您可以使用 Date::Calc 的 check_date 函数来确定日期是否有效。

use Date::Calc qw(Add_Delta_Days check_date Day_of_Week Delta_Days);

$this_day = (check_date($lyear, $month, $day)
             ? Day_of_Week($lyear, $month, $day)
             : 'INVALID');

更正无效日期更为复杂,因为这取决于您获取无效日期的方式以及您想要对它们执行的操作。 例如,如果该日期可能超出范围,并且您想要将 4 月 31 日更正为 5 月 1 日,则可以使用

($lyear, $month, $day) = Add_Delta_Days($lyear, $month, 1,  $day-1);

但这不会更正无效的年份或月份。

The error message says you're passing Date::Calc an invalid date. Don't do that. You can use Date::Calc's check_date function to decide if the date is valid.

use Date::Calc qw(Add_Delta_Days check_date Day_of_Week Delta_Days);

$this_day = (check_date($lyear, $month, $day)
             ? Day_of_Week($lyear, $month, $day)
             : 'INVALID');

Correcting invalid dates is more complicated, because it depends on how you're getting invalid dates, and what you want to do about them. For example, if the day might be out of range, and you wanted to correct April 31 to May 1, you could use

($lyear, $month, $day) = Add_Delta_Days($lyear, $month, 1,  $day-1);

But that won't correct an invalid year or month.

醉城メ夜风 2024-07-20 13:14:06
use Posix qw(mktime);
my $epoch = mktime(sec, min, hour, mday, mon, year);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch);
use Posix qw(mktime);
my $epoch = mktime(sec, min, hour, mday, mon, year);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch);
柳絮泡泡 2024-07-20 13:14:06

它有点巴洛克风格,但我一直喜欢 Date::Manip。

不过,我认为您不需要替代方法。 更有可能的是,您需要停止提供 Date::Calc 无效日期。

它的错误值是什么?

It's a little baroque, but I always liked Date::Manip.

I don't think an alternate methodology is what you need, though. More likely, you need to stop feeding Date::Calc invalid dates.

What values is it erroring on?

往事随风而去 2024-07-20 13:14:06

您可以使用 Date::Simple

use Date::Simple (':all');
my $date = ymd($year, $month, $day);
my $dow = $date->day_of_week();

You could use Date::Simple.

use Date::Simple (':all');
my $date = ymd($year, $month, $day);
my $dow = $date->day_of_week();
黎夕旧梦 2024-07-20 13:14:06

作为 heeen 答案的替代方案,您可以使用 strftime:

use Posix qw(strftime);
my $wday = strftime('%w', sec, min, hour, mday, mon, year); # 0 = Sunday, 1 = Monday, etc...
my $day_name = strftime('%a', sec, min, hour, mday, mon, year); # Sun, Mon, etc...
my $day_name_long = strftime('%A', sec, min, hour, mday, mon, year); # Sunday, Monday, etc...

As an alternative to heeen's answer, you can use strftime:

use Posix qw(strftime);
my $wday = strftime('%w', sec, min, hour, mday, mon, year); # 0 = Sunday, 1 = Monday, etc...
my $day_name = strftime('%a', sec, min, hour, mday, mon, year); # Sun, Mon, etc...
my $day_name_long = strftime('%A', sec, min, hour, mday, mon, year); # Sunday, Monday, etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文