如何在没有任何模块的情况下从日历日期转换为序数日期
how to convert from a calendar date to an ordinal date with out any modules
我需要在日志文件中搜索时间范围。时间戳在 JDE Julian date format 中,我无法使用模块。
日期格式为 YYYYJJJHHMMSS
,其中 JJJ
是 Julian 中的日期。
我需要在不使用模块的情况下在 JDE 中转换用户输入。
这些数字是 date/time 值这一事实没有区别。它们是整数(尽管是相当大的整数)并且可以用与任何其他整数完全相同的方式进行比较。
while (<$your_input_filehandle>) {
# I have no idea of the format of your input data, so I can't
# begin to implement extract_timestamp()
my $this_records_timestamp = extract_timestamp($_);
if ($this_records_timestamp >= $min_timestamp and
$this_records_timestamp <= $max_timestamp) {
# timestamp is within the given range
}
}
更新: 将 YYYYMMDD 转换为 YYYYJJ
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $in_format = '%Y%m%d';
my $out_format = '%Y%j';
my $in_date = shift
|| die "Please pass date in format YYYYMMDD\n";
my $date = Time::Piece->strptime($in_date, $in_format);
say $date->strftime($out_format);
您应该至少可以访问核心模块 POSIX
:
perl -MPOSIX=mktime -pe'@F=/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;$j=sprintf"%03d",1+(localtime(mktime(@F[5,4,3,2],$F[1]-1,$F[0]-1900)))[7];s/.{4}\K.{4}/$j/;'
或与Time::Piece
perl -MTime::Piece -ne'print Time::Piece->strptime($_,"%Y%m%d%H%M%S\n")->strftime("%Y%j%H%M%S\n")'
我需要在日志文件中搜索时间范围。时间戳在 JDE Julian date format 中,我无法使用模块。
日期格式为 YYYYJJJHHMMSS
,其中 JJJ
是 Julian 中的日期。
我需要在不使用模块的情况下在 JDE 中转换用户输入。
这些数字是 date/time 值这一事实没有区别。它们是整数(尽管是相当大的整数)并且可以用与任何其他整数完全相同的方式进行比较。
while (<$your_input_filehandle>) {
# I have no idea of the format of your input data, so I can't
# begin to implement extract_timestamp()
my $this_records_timestamp = extract_timestamp($_);
if ($this_records_timestamp >= $min_timestamp and
$this_records_timestamp <= $max_timestamp) {
# timestamp is within the given range
}
}
更新: 将 YYYYMMDD 转换为 YYYYJJ
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $in_format = '%Y%m%d';
my $out_format = '%Y%j';
my $in_date = shift
|| die "Please pass date in format YYYYMMDD\n";
my $date = Time::Piece->strptime($in_date, $in_format);
say $date->strftime($out_format);
您应该至少可以访问核心模块 POSIX
:
perl -MPOSIX=mktime -pe'@F=/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;$j=sprintf"%03d",1+(localtime(mktime(@F[5,4,3,2],$F[1]-1,$F[0]-1900)))[7];s/.{4}\K.{4}/$j/;'
或与Time::Piece
perl -MTime::Piece -ne'print Time::Piece->strptime($_,"%Y%m%d%H%M%S\n")->strftime("%Y%j%H%M%S\n")'