[英]Perl Time, date conversion Error
This is a perl script for sql data pulling each day for 100 days starting from Oct 1 and SQL is quite picky in date formats(yyyy-mm-dd), so I've written the script as follows. 这是一个Perl脚本,用于从10月1日开始每天提取100天的sql数据,并且SQL在日期格式(yyyy-mm-dd)中非常挑剔,因此我将脚本编写如下。 However, at a specific day, on 2011-11-06, the time to date conversion is incorrect, and start and end date become the same.
但是,在特定的日期,2011年11月6日,到日期的时间转换不正确,并且开始日期和结束日期变为相同。
$srt_date='2011-11-06'
$end_date='2011-11-06'
I don't know if this is perl error or something else. 我不知道这是perl错误还是其他错误。
use DBI;
use DBD::Oracle qw(:ora_types);
use Compress::Zlib;
use FileHandle;
use Date::Parse;
use Date::Format;
$st_day=str2time('2011-10-1');
@days=(0..100);
foreach $daynum (@days){
$dt1 = $st_day+3600*(24*$daynum);
$dt2 = $st_day+3600*(24*($daynum+1));
$srt_date = time2str("%d-%h-%Y", $dt1);
$end_date = time2str("%d-%h-%Y", $dt2);
print $srt_date, ',' ,$end_date, '\n';
my $sqlGetEid = "select x,y from z where DATETIME>='$srt_date' and DATETIME<'$end_date'";
}
Here's how DateTime
handles the DST transitions correctly: 以下是
DateTime
正确处理DST转换的方法:
use strict; #ALWAYS!
use warnings; #ALWAYS!
use DateTime;
my $st_day = '2011-10-1';
my ($year, $month, $day) = split /-/, $st_day;
my $dt = DateTime->new(
year => $year,
month => $month,
day => $day,
time_zone => 'local',
);
my @days = 0..100;
foreach my $daynum (@days) {
my $dt1 = $dt->ymd;
my $dt2 = $dt->add(days => 1)->ymd;
printf "%s,%s\n", $dt1, $dt2;
}
I'm not sure what you want to achieve exactly, but why bother executing 100 SQL statements when you can get away with something like: 我不确定您要实现的目标是什么,但是为什么在执行以下操作时却不愿意执行100条SQL语句:
SELECT trunc(datetime, 'DD') truncdate, x,y
FROM z WHERE datetime between '2011-10-01'
AND to_date('20111001', 'YYYYMMDD') + 99
Populate a hash with truncdate
as key, and if your dates are ISO 8601, you'll get the same ordering by looping over the hash with a regular ( cmp
) sort. 使用
truncdate
作为键填充哈希,如果您的日期是ISO 8601,则通过使用常规( cmp
)排序遍历哈希将获得相同的顺序。
EDIT : I'll clarify how you could do this: 编辑 :我将澄清您如何执行此操作:
my $sth = $mdbh->prepare("SELECT trunc(datetime, 'DD') truncdate, x,y
FROM z WHERE datetime between '2011-10-01'
AND to_date('20111001', 'YYYYMMDD') + 99
ORDER BY truncdate");
$sth->execute();
my $lastdate = "";
my $fh;
while (my $row = $sth->fetchrow_hashref()) {
# If new date, create new file
if ($row->{truncdate} ne $lastdate) {
close($fh) if $fh;
open($fh, ">", "$row->{truncdate}.csv") or die "Unable to create file '$row->{truncdate}.csv': $!\n";
}
print $fh "$row->{x},$row->{y}\n";
$lastdate = $row->{truncdate};
}
close($fh) if $fh;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.