Perl编程:函数与示例详解
1. 日期转换函数示例
在Perl编程中,有时需要将特定格式的日期转换为Perl自1900年以来的秒数格式。以下是一个实现该功能的函数示例:
# convert this format back into Perl’s seconds-since-1900 format.
# the Time::Local module and the timelocal func do this.
sub date2time {
my $date = shift;
if ($date =~ /^\*\*\*/) { # error formatting, sort to top
return 0;
} else {
my ($m,$d,$y) = split(/\//,$date);
$m--; # months start from 0 in perl’s time format
return timelocal(0,0,0,$d,$m,$y);
}
}
这个函数 date2time 接受一个日期字符串作为参数。如果日期字符串以 *** 开头,说明格式错误,函数将返回0。否则,它会将日期字符串按 / 分割成年、月、日,由于Perl的时间格式中月份从0开始,所以月份要减1,最后使用 timelocal 函数将日期转换为秒数并返回
超级会员免费看
订阅专栏 解锁全文
5

被折叠的 条评论
为什么被折叠?



