perl:将当前时间与特定时间比较

发布于 2025-02-03 05:33:31 字数 1003 浏览 1 评论 0原文

我是Perl的新手。我正在制作每天运行的脚本。我想检查当前时间不到特定时间。我当前的解决方案是我从localtime中提取小时 min ,然后对其进行比较。但是有人告诉我找到一个更好的解决方案。

当前代码:

my $flag;
for (;;) {
    $flag = GetValue();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    if( $flag ) {
        #do action here
        last;
    } elsif ( $hour <= 17 ) {
        if( $min <=30 ) {
            print STDOUT "Keep running till 17:30 \n";
            sleep 300;
        } else {
            die "Error";
        }
    } else {
        die "Error";
    }
}

基本上,语句$ flag = getValue();有时会意外返回未定义的值,并导致脚本失败。因此,我们想检查$标志是否定义。如果定义了它,则我们执行一些代码并退出for循环。否则我们检查时间。如果时间少于17:30,那么我们要等待一段时间,然后尝试再次获得标志的价值。我只需要更改将当前时间比较17:30的逻辑

我想要我的东西:

} elsif ( $current_time <= 17:30 ) {
   print STDOUT "Keep running till 17:30 \n";
   sleep 300;
}

如何实现这一目标?

I am very new to perl. I am working on a script that runs everyday. I want to check that current time is less than a specific time. My current solution is that I extract hour and min from localtime and then compare it. But I was told to find a better solution.

Current code:

my $flag;
for (;;) {
    $flag = GetValue();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    if( $flag ) {
        #do action here
        last;
    } elsif ( $hour <= 17 ) {
        if( $min <=30 ) {
            print STDOUT "Keep running till 17:30 \n";
            sleep 300;
        } else {
            die "Error";
        }
    } else {
        die "Error";
    }
}

Basically, the statement $flag = GetValue(); sometimes returns undefined value unexpectedly and causes the script to fail. So we want to check if $flag defined. If it is defined then we execute some code and exit the for loop. Else we check the time. If the time is less that 17:30 then we want to wait for some time and try to get the value of flag again. I only need to change the logic of comparing current time to 17:30

I want my something like:

} elsif ( $current_time <= 17:30 ) {
   print STDOUT "Keep running till 17:30 \n";
   sleep 300;
}

How do I achieve this?

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

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

发布评论

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

评论(1

绝情姑娘 2025-02-10 05:33:31

处理日期时间的好方法是使用一个好的库,例如 dateTime

my $dt_mark = DateTime
    -> now(time_zone => 'local')
    -> set(hour => 17, minute => 30);

for (;;) { 
   ...

   elsif ( DateTime->now(time_zone => 'local') <= $dt_mark ) {
      ...

避免为每个比较构建一个新对象,但这会使代码更加复杂。请注意,DateTime是一个大型库,这可能是一个过度的库。

如果目的确实是仅仅将当前时间与固定小时的时间进行比较,那么也许

elsif ( $hour < 17 or ($hour == 17 and $min <= 30) )

足够好了吗?

或形式和使用可以通过陈述目的进行词汇进行比较的字符串,

elsif ( sprintf("%02d%02d", $hour, $min) lt '1730' )

这样做

my (undef, $min, $hour) = localtime(time);

可能更清晰,并且直接指示localtime的返回的哪些部分。

也可以忽略时间,per localtime

localtime expr
localtime
...
如果省略了expr,则localtime使用当前时间(如time)。

The good and solid way to deal with date-times is to use a good library, like DateTime

my $dt_mark = DateTime
    -> now(time_zone => 'local')
    -> set(hour => 17, minute => 30);

for (;;) { 
   ...

   elsif ( DateTime->now(time_zone => 'local') <= $dt_mark ) {
      ...

One can avoid constructing a new object for every comparison but that would make the code more complicated. Note that DateTime is a large library and this could be an overkill.

If the purpose is indeed to merely compare the current time to a fixed hour-minute then perhaps

elsif ( $hour < 17 or ($hour == 17 and $min <= 30) )

is good enough?

Or form and use strings that can be lexically compared

elsif ( sprintf("%02d%02d", $hour, $min) lt '1730' )

For the stated purpose it is fine to do

my (undef, $min, $hour) = localtime(time);

This may be clearer as well as it directly indicates which parts of localtime's return are used.

Can also leave out time, per localtime

localtime EXPR
localtime
...
If EXPR is omitted, localtime uses the current time (as returned by time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文