in reply to checking dates in strings
use strict; my @last_day = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); sub is_leap_year { my $year = shift; my $result; if ($year % 4) { $result = 0; } else { $result = 1; if (!($result % 100)) { $result = 0; if (!($result % 400)) { $result = 1; } } } return $result; } sub is_validate_date { my $date = shift; $date =~ m/(\d+)-(\d+)-(\d+)/; my $year = $3; my $month = $2; my $day = $1; my $result = 1; if (($month > 12) || ($month < 1)) { $result = 0; } else { if ($day > $last_day[$month - 1]) { $result = 0; } else { if (!is_leap_year($year) && ($month == 2) && ($day > 28)) +{ $result = 0; } } } return $result; } my $date = "29-02-2000"; print is_validate_date($date);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: checking dates in strings
by FamousLongAgo (Friar) on Nov 11, 2002 at 01:10 UTC | |
by pg (Canon) on Nov 11, 2002 at 03:10 UTC |