perl 使用 localtime 來算出現在系統時間,
底下作一個 func_date_time function
再拿一個值給 func_date_time 讓它顯示秒、分、時、日、月、年、週
use strict;
sub func_date_time {
my ($date_second,$date_minute,$date_hour,$date_day,$date_month,$date_year,$date_week)=localtime(time);
$date_month++;
$date_year+=1900;if (length($date_second) == 1) {$date_second = “0”.”$date_second”;}
if (length($date_minute) == 1) {$date_minute = “0”.”$date_minute”;}
if (length($date_hour) == 1) {$date_hour = “0”.”$date_hour”;}
if (length($date_day) == 1) {$date_day = “0”.”$date_day”;}
if (length($date_month) == 1) {$date_month = “0”.”$date_month”;}my $which_time = shift;
if ($which_time) {
if ($which_time =~ /second/) {
return $date_second;
} elsif ($which_time =~ /minute/){
return $date_minute;
} elsif ($which_time =~ /hour/){
return $date_hour;
} elsif ($which_time =~ /day/){
return $date_day;
} elsif ($which_time =~ /month/){
return $date_month;
} elsif ($which_time =~ /year/){
return $date_year;
} elsif ($which_time =~ /week/){
return $date_week;}
} else {
return “$date_year/$date_month/$date_day $date_hour:$date_minute:$date_second”;
}
}print my $sec = func_date_time(‘second’);
print ” “;
print my $min = func_date_time(‘minute’);
print ” “;
print my $hr = func_date_time(‘hour’);
print ” “;
print my $day = func_date_time(‘day’);
print ” “;
print my $month = func_date_time(‘month’);
print ” “;
print my $year = func_date_time(‘year’);
print ” “;
print my $week = func_date_time(‘week’);
留言