Sandeep Pathare wrote: > > I am a beginner of Perl. How do I convert and print the following strings > into a date format so that either the date is returned or "date not valid" > is printed. > Input data is: > > 792910171010163200 > 552910171010163200 > 552913171010163200 > 552910171010163200 > 552909171010163200 > 552909171010163200 > > For each of the data value, the output should like: > > 552910171010163200 Sat Nov 17 10:29:55 2001 > > Here is some hint I read in the documentation, but still can't figure out > how to use it: > > The 552910171010163200 which is Sat Nov 17 10:29:55 2001, > (HINT: localtime) should be parsed into HH:MM:SS (Zero padded) and > WDay MMM DD, YYYY - which should look like Sat Nov 17, 2001 > 55: 29: 10: 17:10: 101: 6: 320:0 > HH:MM:SS:17:Nov: year (1900 +101= 2001):Sat: 320thday of the year
Here is one way to do it: $ cat try.pl #!/usr/bin/perl -w use strict; use Time::Local qw(timelocal_nocheck); while ( <DATA> ) { chomp; my $date = timelocal_nocheck( unpack 'A2A2A2A2A2A3', $_ ); if ( $_ eq join( '', localtime( $date ) ) ) { print scalar localtime( $date ), "\n"; } else { print "Date not valid.\n"; } } __DATA__ 792910171010163200 552910171010163200 552913171010163200 552910171010163200 552909171010163200 552909171010163200 $ ./try.pl Date not valid. Sat Nov 17 10:29:55 2001 Sat Nov 17 13:29:55 2001 Sat Nov 17 10:29:55 2001 Date not valid. Date not valid. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]