checking for a valid date-time 
Author Message
 checking for a valid date-time

Does anyone know a better way than this:

start:
$a = <STDIN>;
chomp($a);
if ("$a" =~ /(\d{2})(\d{2})(\d{2})/) {
 $days = $1;
 $hours = $2;
 $minutes = $3;

Quote:
} else { print "$a isn't valid.\n" }

if ($days < 32 && $hours < 24 && $minutes < 60) {
 print "$a is valid\n";
Quote:
} else { print "$a isn't valid\n" }

goto start


Wed, 18 Jun 1902 08:00:00 GMT  
 checking for a valid date-time

say such a terrible thing:

Quote:
>Does anyone know a better way than this:

>start:
>$a = <STDIN>;
>chomp($a);
>if ("$a" =~ /(\d{2})(\d{2})(\d{2})/) {
> $days = $1;
> $hours = $2;
> $minutes = $3;
>} else { print "$a isn't valid.\n" }

>if ($days < 32 && $hours < 24 && $minutes < 60) {
> print "$a is valid\n";
>} else { print "$a isn't valid\n" }
>goto start

Well you could use a while(1) loop instead of the goto and you don't
need to stringify $a in the match and for that matter you could use a
better variable name than $a (which is special anyway, see perdoc -f
sort) like $input or something but what I think you want to know is:

perldoc Time::ParseDate
perldoc -f localtime

--

   :self-reference: n.  See {self-reference}.
From "The New Hackers Dictionary", version 4.2



Sat, 15 Mar 2003 09:47:41 GMT  
 checking for a valid date-time
On Mon, 25 Sep 2000 23:21:26 +0900,

Quote:
> Does anyone know a better way than this:

> start:
> $a = <STDIN>;
> chomp($a);
> if ("$a" =~ /(\d{2})(\d{2})(\d{2})/) {
>  $days = $1;
>  $hours = $2;
>  $minutes = $3;
> } else { print "$a isn't valid.\n" }

> if ($days < 32 && $hours < 24 && $minutes < 60) {
>  print "$a is valid\n";
> } else { print "$a isn't valid\n" }
> goto start

#!/usr/local/bin/perl -w
use strict;

while (<STDIN>)
{
    chomp;
    my ($day, $hour, $minute) = /^(\d\d)(\d\d)(\d\d)$/;

    print "$_ is ",
      !$minute || $day > 31 || $hour > 24 || $minute > 60 ? 'not ' : '',
      "valid\n";

Quote:
}

Note however, that this is slightly different from yours. Yours
matches the first six digits in a string, mine insists that the only
strings allowed have to have six digits and no more. Also note that
the check on '$day' is slightly weak, because for example, February
doesn't have 31 days. Without knowing anythjng about the month
however, you can't make it more specific.

Notes: mainly on style: Do use -w and the strict pragma. They'll help
you debug programs better. Don't use $a or $b unless you are in a sort
subroutine. They're special. Don't use gotos unless you are prepared
to justify it, and can come up with a damned good reason. Properly
indent your code; it's just too hard to read this way.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd.   | selective about its friends.
NSW, Australia                  |



Sat, 15 Mar 2003 11:40:40 GMT  
 checking for a valid date-time
On Mon, 25 Sep 2000 23:21:26 +0900, Dan and Shelly

Quote:

>Does anyone know a better way than this:

>start:
>$a = <STDIN>;
>chomp($a);
>if ("$a" =~ /(\d{2})(\d{2})(\d{2})/) {
> $days = $1;
> $hours = $2;
> $minutes = $3;
>} else { print "$a isn't valid.\n" }

>if ($days < 32 && $hours < 24 && $minutes < 60) {
> print "$a is valid\n";
>} else { print "$a isn't valid\n" }
>goto start

Perl modules are your friend.

  #!/usr/local/bin/perl -w

  use Time::ParseDate;

  while ($time = <DATA>) {
      chomp $time;
      $epoch_seconds = parsedate("$time", VALIDATE => 1);
      if (! $epoch_seconds) {
          print "$time is invalid\n";
      }
  }

  __DATA__
  12:00:00
  01:52:45
  25:15:00
  04:61:00
  15:05:64

prints:

  25:15:00 is invalid
  04:61:00 is invalid
  15:05:64 is invalid

--

Ah, but a man's reach should exceed his grasp
- or what's a heaven for? -- Robert Browning



Wed, 18 Jun 1902 08:00:00 GMT  
 checking for a valid date-time
On 26 Sep 2000 12:08:35 GMT,

Quote:
> On Mon, 25 Sep 2000 23:21:26 +0900, Dan and Shelly

> >Does anyone know a better way than this:

> >start:
> >$a = <STDIN>;
> >chomp($a);
> >if ("$a" =~ /(\d{2})(\d{2})(\d{2})/) {
> > $days = $1;
> > $hours = $2;
> > $minutes = $3;

[snip]

Quote:
> Perl modules are your friend.

>   #!/usr/local/bin/perl -w

>   use Time::ParseDate;

>   while ($time = <DATA>) {
>       chomp $time;
>       $epoch_seconds = parsedate("$time", VALIDATE => 1);
>       if (! $epoch_seconds) {
>           print "$time is invalid\n";
>       }
>   }

>   __DATA__
>   12:00:00
>   01:52:45
>   25:15:00
>   04:61:00
>   15:05:64

I'm not sure that that helps. The original code seems to suggest that
the input is a concatenation of three two-digit numbers, the first of
which specifies a day, the second an hour, and the third a minute.
Without munging that before giving it to parsedate, I doubt very much
that you'll be able to make it validate. Besides that, without
providing the context of a month and year,it would be impossible for
anything to do that correctly.

If it was only a time, ok. If it was only a date, ok as well. But
given the input, the only validation that is possible is a broad one.

Now.. it might very well be possible that the OP could get their data
in a more decent format, but that's an entirely different issue.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Useful Statistic: 75% of the people
Commercial Dynamics Pty. Ltd.   | make up 3/4 of the population.
NSW, Australia                  |



Sun, 16 Mar 2003 09:24:53 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. date/time - seconds == date/time?

2. Displaying the servers time-date not the browser reading the viewers computer time-date

3. checking time and date format

4. Check for valid perl regex

5. Check for valid hex value

6. Checking if a url is valid

7. How do I check for a valid URL

8. checking if mail address is valid

9. Checking if URL is valid/exist using PERL

10. A Script for checking emails valid?

11. Checking for valid Email...

12. check for valid e-mail address

 

 
Powered by phpBB® Forum Software