Hello,
I want to verify the format date like 2013-03-08 (YYYY-MM-DD)
It doesn't work because the pattern matching notation below returns false while the date is right.
Can you help me ? Thanks in advance
case "$6" in ([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# Nothing, OK !
;;
(*) echo 'Fatal, $6 eq '"'$6'"', Right format is YYYY-MM-DD' >&2
;;
esac
Yoda
March 9, 2013, 1:18am
2
Even if you fix this pattern, I don't think it will be reliable because this will not cover conditions like leap year.
So If you have GNU date
, you can simply do:
[[ "$( date -d "$1" +%F 2>&1 | grep invalid )" = "" ]] && echo "$1 is valid" || echo "$1 is invalid"
It's OK, I have forgotten some cases
case "$6" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# nothing, OK !
;;
(*)
echo 'Fatal, $6 = '"'$6'"', Date non conforme OU absente' >&2
#exit 1
;;
esac
---------- Post updated at 02:13 AM ---------- Previous update was at 02:13 AM ----------
Even if you fix this pattern, I don't think it will be reliable because this will not cover conditions like leap year.
So If you have GNU date
, you can simply do:
[[ "$( date -d "$1" +%F 2>&1 | grep invalid )" = "" ]] && echo "$1 is valid" || echo "$1 is invalid"
Thank you for this alternative
I would like to simplify this pattern notation
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
with that
([1-9][0-9][0-9][0-9]-[0(1-9) | 1(0-2)]-[0(1-9) | 1(0-9) | 2(0-9) | 3(0-1)])
But it doesn't work.
Is this notation
[0(1-9) | 1(0-2)]
means
(01 10) (01 11) (01 12)....(09 10) (09 11) (09 12)....(09 12)
Thank you
---------- Post updated 11-03-13 at 01:13 AM ---------- Previous update was 10-03-13 at 04:14 AM ----------
Hello,
Can anyone help me ?
I wanted to simplfy this first pattern with the second, but it doesn't work.
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
([1-9][0-9][0-9][0-9]-[0(1-9) | 1(0-2)]-[0(1-9) | 1(0-9) | 2(0-9) | 3(0-1)])
Thanks in advance
How about perl
#!/usr/bin/perl
use strict;
use Time::Local;
my $dt="2013-12-01";
my ($year,$month,$date)=unpack("A4 x1 A2 x1 A2",$dt);
eval {
timelocal(0,0,0,$date,$month-1,$year-1900);
print "Correct date\n";
} or die "Date is not correct $@\n";