如何在Perl的不同时区安排未来的任务?

时间:2014-02-06 23:48:45

标签: perl datetime timezone localtime

我已经经历了很多答案,但似乎没有一个能解决我的问题。我想从纽约运行一个Perl脚本来安排在其他时区的计算机上执行任务,例如洛杉矶(或任何其他时区)。运行脚本的用户可以选择输入日期/时间和时区。

示例:

perl script.pl -action reboot LAHost.com 2014/012/12 15:00:00 'America/Los_Angeles'

此脚本应在洛杉矶当地时间下午3点在LAHost.com计算机上安排重启。

有没有人可以帮我找到使用DateTime或任何其他内置函数在perl中执行此操作的方法?

我是编程新手,目前正在学习Perl。请原谅我的无知。

2 个答案:

答案 0 :(得分:0)

您的问题显示为

  

如何将指定时区的日期和时间转换为当地时间?

use DateTime::Format::Strptime qw( );

my $date = '2014/012/12';
my $time = '15:00:00';
my $tz   = 'America/Los_Angeles';

my $format = DateTime::Format::Strptime->new(
   pattern   => '%Y/%m/%d %H:%M:%S',
   time_zone => $tz,
   on_error  => 'croak',
);

my $dt = $format->parse_datetime("$date $time");
$dt->set_time_zone('local');

print $format->format_datetime($dt), "\n";

答案 1 :(得分:0)

my $timeZone="Europe/London";
my $dateTime = DateTime->new(year => $year, month => $month,
                day => $day, hour => $hour, minute => $minute,
                second => $second, time_zone => $timeZone);
相关问题