perl POSIX::strptime - 我的数据看起来像上午 9:31:00 - 我想提取分钟并执行计算

发布于 2024-10-26 02:58:51 字数 1004 浏览 5 评论 0原文

我想从逗号分隔文件第二列中存在的指定时间中提取分钟并执行计算。时间的格式如下:

9:31:00 AM

我想提取分钟值并计算迄今为止一天中的总分钟数。我在子例程 get_time 中执行此操作。但返回值始终为零,这让我认为我没有正确使用 POSIX::strptime 。任何见解都会很棒。谢谢。

#!/usr/bin/env perl  
use strict;  
use POSIX::strptime;  
use Text::CSV;  

sub get_time  
{  
    my($str) = @_;
    my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [3,4,5];
    print "$hour\n";
    return($hour*60 + $min)
}

open my $fh, "<", datafile.txt
my $csv = Text::CSV->new() or die "Failed to create Text::CSV object";
my $line = <$fh>;

die "Failed to parse line <<$line>>\n\n" unless $csv->parse($line);
my @columns = $csv->fields();

while ($line = <$fh>)
{
   chomp $line;
   die "Failed to parse line <<$line>>\n\n" unless $csv->parse($line);
   my @columns = $csv->fields();
   die "Insufficient columns in <<$line>>\n" if scalar(@columns) < 1;
   my $minute = get_time($columns[1]);
   print "$minute\n";
}

I want to extract the minute from a specified time that exists in the 2nd column of a comma delimited file and perform a calculation. The format of the time is as follows:

9:31:00 AM

I want to extract the minute value and calculate the total minutes in the day so far. I do this in subroutine get_time. But the returned value is always zero which makes me think that I am not using POSIX::strptime correctly. Any insight would be wonderful. Thank you.

#!/usr/bin/env perl  
use strict;  
use POSIX::strptime;  
use Text::CSV;  

sub get_time  
{  
    my($str) = @_;
    my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [3,4,5];
    print "$hour\n";
    return($hour*60 + $min)
}

open my $fh, "<", datafile.txt
my $csv = Text::CSV->new() or die "Failed to create Text::CSV object";
my $line = <$fh>;

die "Failed to parse line <<$line>>\n\n" unless $csv->parse($line);
my @columns = $csv->fields();

while ($line = <$fh>)
{
   chomp $line;
   die "Failed to parse line <<$line>>\n\n" unless $csv->parse($line);
   my @columns = $csv->fields();
   die "Insufficient columns in <<$line>>\n" if scalar(@columns) < 1;
   my $minute = get_time($columns[1]);
   print "$minute\n";
}

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

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

发布评论

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

评论(2

难得心□动 2024-11-02 02:58:51

在 get_time 中,您有以下行

my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [3,4,5];`

根据 docs, strptime 返回

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("string", "Format");

所以看起来您

my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [0,1,2];

需要。

祝你好运!

In get_time, you have the line

my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [3,4,5];`

According to the docs, strptime returns

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("string", "Format");

So it looks like you need

my ($sec, $min, $hour) = (POSIX::strptime($str, '%I:%M:%S')) [0,1,2];

instead.

Best of luck!

世俗缘 2024-11-02 02:58:51

如果不知道时区和可能的日期,就不可能正确确定一天中某个时间之前的分钟数。

$ perl -MDateTime -E'
   for (10..16) {
      my $dt = DateTime->new(
         year      => 2011,
         month     => 3,
         day       => $_,
         hour      => 9,
         minute    => 31,
         second    => 0,
         time_zone => "America/New_York",
      );
      ( my $ref_dt = $dt->clone )->truncate(to => "day");
      my $minutes = ($dt - $ref_dt)->in_units("minutes");
      say($dt->ymd, " ",  $minutes);
   }
'
2011-03-10 571
2011-03-11 571
2011-03-12 571
2011-03-13 511    <------
2011-03-14 571
2011-03-15 571
2011-03-16 571

It's impossible to correctly determining the number of a minutes in a day before a certain time without knowing the time zone and possibly the date.

$ perl -MDateTime -E'
   for (10..16) {
      my $dt = DateTime->new(
         year      => 2011,
         month     => 3,
         day       => $_,
         hour      => 9,
         minute    => 31,
         second    => 0,
         time_zone => "America/New_York",
      );
      ( my $ref_dt = $dt->clone )->truncate(to => "day");
      my $minutes = ($dt - $ref_dt)->in_units("minutes");
      say($dt->ymd, " ",  $minutes);
   }
'
2011-03-10 571
2011-03-11 571
2011-03-12 571
2011-03-13 511    <------
2011-03-14 571
2011-03-15 571
2011-03-16 571
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文