Check if a date field has date or timestamp or date&timestamp

Hi,
In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp.
If it is date then append "<space>00:00:00" to it.
If it is a time stamp, store the data in a different file(invalid_dttmstmp.txt) and remove from this file.
If it is a date timestamp then do nothing.

Input file:

File1
====
Field 1
=====
00:00:00
<space>01:01:01
2012-02-25 00:30:14
2012-02-25

Expected output

File1
====
Field 1
=====
2012-02-25 00:30:14
2012-02-25 00:00:00

invalid_dttmstmp.txt
===============
Field 1
======
00:00:00
<space>01:01:01

I coded for date part and that works. I need help with time stamp validation

Thanks

---------- Post updated 02-26-12 at 12:01 AM ---------- Previous update was 02-25-12 at 11:44 PM ----------

Is it correct to use

touch -c -d 20120225 dummy --> to check the date

touch -c -t 201220252359.59 -->to check date&timestamp

Is this logic correct. Also can this handle around 4000 different values (if I go for loop) with in a few minutes (1-2min)?? :wall:

Here's a Perl program that does all that.

$
$ cat -n file1
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
     5  2012-02-25 00:30:14
     6  2012-02-25
$
$ cat -n process_dates.pl
     1  #!perl -w
     2  use File::Copy;
     3  my $orig = "file1";                # the original file
     4  my $bkup = "file1.bak";            # the backup, just in case
     5  my $temp = "file1.tmp";            # valid records go here
     6  my $invl = "invalid_dttmstmp.txt"; # and invalid go here
     7  copy ($orig, $bkup)  or die "Can't back up original file: $!";
     8  open (V, ">", $temp) or die "Can't open $temp for writing: $!";
     9  open (I, ">", $invl) or die "Can't open $invl for writing: $!";
    10  open (F, "<", $orig) or die "Can't read original file $orig: $!";
    11  while (<F>) {
    12    if (/^(Field|=+)/) {print I $_; print V $_}
    13    elsif (/^\s*\d+:/) {print I $_}
    14    elsif (/^\d{4}-\d\d-\d\d$/) {chomp; print V "$_ 00:00:00\n"}
    15    elsif (/^\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d$/) {print V $_}
    16  }
    17  close (V)    or die "Can't close $temp: $!";
    18  close (I)    or die "Can't close $invl: $!";
    19  rename ($temp, $orig);
$
$ perl process_dates.pl
$
$ # check the backup file
$ cat -n file1.bak
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
     5  2012-02-25 00:30:14
     6  2012-02-25
$
$ # check the original file
$ cat -n file1
     1  Field 1
     2  =====
     3  2012-02-25 00:30:14
     4  2012-02-25 00:00:00
$
$ # check the invalid data file
$ cat -n invalid_dttmstmp.txt
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
$
$

tyler_durden