in reply to Data format verification for insertion into a database
user[29]~/bin>mysql-date-fixer next tuesday 2003-04-29 01:06:55 user[30]~/bin>mysql-date-fixer Jan 1 2021 2021-01-01 00:00:00 user[31]~/bin>mysql-date-fixer 982321156 2001-02-16 03:59:16
# script name mysql-date-fixer # NB: Date::Manip is great but it is slow, and this is fun code # but just something i did to play with Date::Manip and not # that polished use Date::Manip; use warnings; use strict; my $date; my $date_thing_to_check = join(' ', @ARGV); unless ( $date_thing_to_check ) { print "What date are you trying to figure out?\n\t"; chomp ($date_thing_to_check = <STDIN>); } # string of numbers not like 200304242116, we'll call it epoch secs if ( $date_thing_to_check =~ /^(?!19|20)\d{7,10}$/ ) { $date = ParseDateString("epoch $date_thing_to_check"); $date = ParseDate($date); } else { # something date-ish, ParseDate is very forgiving $date = ParseDate($date_thing_to_check); } unless ( $date ) { die "I couldn't figure that one out...\n", "\tPlease try again with simplified input, ", "write numbers in Arabic.\n"; } # a mySQL format, YYYY-MM-DD HH:MM:SS print UnixDate( $date,"%Y-%m-%d %H:%M:%S"), "\n";
|
|---|