1,比较2个日期相差的时间
use strict;
use Date::Manip;
my $now_time = time();
print $now_time . "\n";
my $now_time_1 = localtime();
print $now_time_1 . "\n";
my $now_time_2 = gmtime();
print $now_time_2 . "\n";
my $old_seconds = UnixDate("3 Nov 2008 15:30:02", "%s");
my $new_seconds = UnixDate("$now_time_1", "%s");
print $old_seconds . "\n";
print $new_seconds . "\n";
my $difference_in_seconds = $new_seconds - $old_seconds;
print $difference_in_seconds . "\n";
print "\n";
my $difference_in_minutes = $difference_in_seconds / 60;
my $difference_in_hours = $difference_in_seconds / 3600;
my $difference_in_days = $difference_in_seconds / 86400;
print $difference_in_minutes . "\n";;
print $difference_in_hours . "\n";;
print $difference_in_days . "\n";;
if ($difference_in_days < 3) {
print "less than 3 days, not send mail" . "\n";
} else {
print "than 3 days, send mail ok" . "\n";
}
2,比较2个日期的早晚
#!/usr/bin/perl -w
use strict;
use Date::Manip;
my $now_time_1 = localtime();
my $old_seconds = UnixDate("$now_time_1", "[%Y-%m-%d_%H:%M:%S]");
print $old_seconds . "\n";
my $date1 = "$now_time_1";
my $date2 = "2008/05/06";
my $flag = Date_Cmp($date1, $date2);
if($flag < 0) {
print "date1 is earlier!\n";
}
elsif($flag == 0) {
print "the two dates are identical!\n";
}
else {
print "date2 is earlier!\n";
}
3,判断是否是符合的日志格式
#!/usr/bin/perl -w
use strict;
use Date::Manip;
my $date = ParseDate("2008");
print $date . "\n";
if (! $date) {
print "Error in the date" . "\n";
} else {
print "in the date" . "\n";
}
4,简单的应用
#!/usr/bin/perl
use strict;
use warnings;
my ( $s, $m, $h, $d, $M,, $y ) = localtime(time);
print "sec is: $s\n";
print "min is: $m\n";
print "hour is: $h\n";
print "day is: $d\n";
$M = $M + 1;
print "mon is: $M\n";
$y = $y + 1900;
print "year is: $y\n";
阅读(1809) | 评论(0) | 转发(1) |