【问题标题】:Get timestamps between 8am to 5pm from timestamps list从时间戳列表中获取上午 8 点到下午 5 点之间的时间戳
【发布时间】:2022-01-16 16:30:36
【问题描述】:

我有一个提取日期时间戳的列表。从这个列表中,我想为每个日期/时间打印上午 8 点到下午 5 点之间的时间戳。但我下面的代码不会产生任何输出。

数据:

2021-Sep-16 21:24:48
2021-Sep-17 03:31:05
2021-Sep-17 08:30:23
2021-Sep-17 09:42:43
2021-Sep-17 12:43:14
2021-Sep-17 14:43:23
2021-Sep-17 15:50:34
2021-Sep-17 16:50:35
2021-Sep-18 03:31:05
2021-Sep-18 08:30:23
2021-Sep-18 09:42:43
2021-Sep-18 12:43:14
2021-Sep-18 14:43:23
2021-Sep-18 15:50:34
2021-Sep-18 22:50:35

预期输出:

2021-Sep-17 08:30:23
2021-Sep-17 09:42:43
2021-Sep-17 12:43:14
2021-Sep-17 14:43:23
2021-Sep-18 08:30:23
2021-Sep-18 09:42:43
2021-Sep-18 12:43:14
2021-Sep-18 14:43:23

我的代码:

sub read_timestamps{
    my $file = './logs/file.log';
    open(IN, "<" ,"$file") or die "Couldn't open file $!";
    open(OUT, ">" ,"_trash/tmp/raw/0-out.txt") or die "Couldn't open file $!";

    my @content = <IN>;
    my $start="08:00:00";
    my $end = "17:00:00";

    foreach $lines(@content){
        if($lines = ~/$start/../$end/){
             print OUT ("$1 \n") if($lines =~ /(\d{4}-\w+-\d{2} \d\d:\d\d:\d\d)/);
        }
    } 
}

【问题讨论】:

    标签: date perl time


    【解决方案1】:
    while (<>) {
       my ( $h ) = / (\d+)/
          or next;
    
       print if $h >= 8 && $h < 17;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多