perl时间转换

前端之家收集整理的这篇文章主要介绍了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;

猜你在找的Perl相关文章