Hello.
I can use any particular (stupid or not) format when using bash date command.
Example :
~> date --date "now" '+%Y-%m-%d %H!%M!%S'
2019-06-03 12!55!33
or
~> date --date "now" '+%Y£%m£%d %H¤%M¤%S'
2019£06£03 12¤57¤36
or
~> date --date "now" '+%Y-%m-%d %H-%M-%S'
2019-06-03 12-58-51
But I can't verify if the input date is in a particular format.
I use this piece of code to make verification :
d='2019-05-31 17:00:00'
MY_FORMAT='+%Y-%m-%d %H-%M-%S'
if [[ "`date --date "$d" "$MY_FORMAT" 2>/dev/null`" == "$d" ]] ; then
echo "$d is valid to relative \"$MY_FORMAT\" date format"
else
echo "$d is NOT VALID to relative \"$MY_FORMAT\" date format"
fi
Example 1 : standard format and good date time ( Relative to date format template )
CURRENT FORMAT : +%Y-%m-%d %H:%M:%S
DATE TO BE VERIFIED : 2019-05-31 17:00:00
Printing input date in current format :
2019-05-31 17:00:00
.
Now doing the test
.
2019-05-31 17:00:00 is valid to relative "+%Y-%m-%d %H:%M:%S" date format
.
.
Good result for the test
Example 2 : standard format and and bad date time ( Relative to date format template )
CURRENT FORMAT : +%Y-%m-%d %H:%M:%S
DATE TO BE VERIFIED : 2019-05-31 17-00-00
Printing input date in current format :
date: invalid date �2019-05-31 17-00-00'
.
Now doing the test
.
2019-05-31 17-00-00 is NOT VALID to relative "+%Y-%m-%d %H:%M:%S" date format
.
.
Good result for the test.
But this is a coincidence.
In any data tested, the test will fail because the time format 'HH-MM-SS' is refused by the date command.
So the test will always failed.
Example 3 : personnal format and bad date time ( Relative to date format template )
CURRENT FORMAT : +%Y-%m-%d %H-%M-%S
DATE TO BE VERIFIED : 2019-05-31 17:00:00
Printing input date in current format :
2019-05-31 17-00-00
.
Now doing the test
.
2019-05-31 17:00:00 is NOT VALID to relative "+%Y-%m-%d %H-%M-%S" date format
.
.
Good result for the test
The date is accepted by the date command.
But the test fail because the time is not in the template format 'yyyy-mm-dd HH-MM-SS'.
Example 4 : personnal format and good date time ( Relative to date format template )
CURRENT FORMAT : +%Y-%m-%d %H-%M-%S
DATE TO BE VERIFIED : 2019-05-31 17-00-00
Printing input date in current format :
date: invalid date �2019-05-31 17-00-00'
.
Now doing the test
.
2019-05-31 17-00-00 is NOT VALID to relative "+%Y-%m-%d %H-%M-%S" date format
.
.
Bad result for the test.
The test will always failed because the time format 'HH-MM-SS' is refused by the date command.
So the test failed.
Is it possible to test the relative to the format used if the format used is not standard.
Any help is welcome