在Perl中如何读取系统时间和日期? [英] How do you read the system time and date in Perl?

查看:164
本文介绍了在Perl中如何读取系统时间和日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读系统时钟(时间和日期),并在Perl中以人性化的格式显示。

I need to read the system clock (time and date) and display it in a human-readable format in Perl.

目前,我使用以下方法(我发现 here ):

Currently, I'm using the following method (which I found here):

#!/usr/local/bin/perl
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
print $theTime; 




当您运行程序时,您应该看到更可读的日期和这样的时间:

When you run the program, you should see a much more readable date and time like this:



9:14:42, Wed Dec 28, 2005 

这似乎更像是实际的生产代码。有更规范的方式吗?

This seems like it's more for illustration than for actual production code. Is there a more canonical way?

推荐答案

使用 localtime 函数:


在标量上下文中,localtime()返回
ctime(3)值:

In scalar context, localtime() returns the ctime(3) value:

$now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"


这篇关于在Perl中如何读取系统时间和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆