perl时间转换

在使用perl进行脚本开发时,不可避免地需要进行时间转换,以下例子是将系统时间转换为标准时间:

#!/usr/bin/perl
use strict;
use DBI;

$|=1;

my $username = "root" ;
my $password = "123456";
my $dbhost = "db-server";
my $dbport = "3306";

my $now = time();
my $date = time_2_local($now);
my $sql = "update tablename set DateTime='$date' where id='1340848776'";
print("sql is $sql\n");

if ($sql)
{
my $dbh;
my $dbh = DBI->connect("dbi:mysql:dbname:$dbhost:$dbport",$username,$password);
my $sth;
$sth = $dbh->prepare($sql);
$sth->execute();

$sth->finish();
$dbh->disconnect(); #close the connection
}
die;

sub time_2_local
{
my $now = shift;
my ($seconds,$minute,$hour,$day,$month,$year) = localtime($now);
$year = $year+1900;
$month = $month+1;
if ($month < 10)
{
$month = "0".$month;
}
if ($day < 10)
{
$day = "0".$day;
}
if ($hour < 10)
{
$hour = "0".$hour;
}
if ($minute < 10)
{
$minute = "0".$minute;
}
if ($seconds < 10)
{
$seconds = "0".$seconds;
}

return "$year-$month-$day $hour:$minute:$seconds";
}


以下例子是标准时间转系统时间:

#!/usr/bin/perl

use Class::Date qw(date localdate);
use strict;
$|=1;

my $unixtime = date("2012-07-06 14:48:54")->epoch;
print $unixtime ."\n";
$unixtime = date("2002-03-03 10:06");
print $unixtime ."\n";

die;

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...