Time conversion is inevitable when you use Perl for script development. The following example shows how to convert the system time to the Standard Time:
#! /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 = '000000 '";
Print ("SQL is $ SQL \ n ");
If ($ SQL)
{
My $ DBH;
My $ DBH = DBI-> connect ("DBI: mysql: dbname: $ dbhost: $ dbport", $ username, $ password );
My $ something;
$ Something = $ DBH-> prepare ($ SQL );
$ Something-> execute ();
$ Something-> 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 ";
}
The following example shows the standard time to system time:
#! /Usr/bin/perl
Use class: Date QW (date localdate );
Use strict;
$ | = 1;
My $ unixtime = date ("14:48:54")-> epoch;
Print $ unixtime. "\ n ";
$ Unixtime = date ");
Print $ unixtime. "\ n ";
Die;
Time conversion is inevitable when you use Perl for script development. The following example shows how to convert the system time to the Standard Time:
#! /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 = '000000 '";
Print ("SQL is $ SQL \ n ");
If ($ SQL)
{
My $ DBH;
My $ DBH = DBI-> connect ("DBI: mysql: dbname: $ dbhost: $ dbport", $ username, $ password );
My $ something;
$ Something = $ DBH-> prepare ($ SQL );
$ Something-> execute ();
$ Something-> 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 ";
}
The following example shows the standard time to system time:
#! /Usr/bin/perl
Use class: Date QW (date localdate );
Use strict;
$ | = 1;
My $ unixtime = date ("14:48:54")-> epoch;
Print $ unixtime. "\ n ";
$ Unixtime = date ");
Print $ unixtime. "\ n ";
Die;