Re: Date processor
by mr.nick (Chaplain) on Feb 23, 2001 at 22:41 UTC
|
I'm not sure what format MySQL's date field is, but Time::ParseDate will parse your example string into a Unix format (# seconds since the epoch).
use Time::ParseDate;
my $time=parsedate("2-19-01 3:22 PM");
print scalar localtime($time),"\n";
| [reply] [Watch: Dir/Any] [d/l] |
|
The easy way to use this more elegant approach of mr.nick would be to INSERT
using the MySQL helper function:
INSERT INTO x (y) VALUES (FROM_UNIXTIME(?));
You might want to check out 7.4.11 of the MySQL docs
for more detailed information.
| [reply] [Watch: Dir/Any] [d/l] |
|
I'm with you. I think that using CPAN modules for converting dates, XML etc. is preferable to rolling one's own regexes. There is just too much opportunity for mischief with malformed dates, leap years, invalid input, etc.
Personally I prefer Date::Manip for these sorts of tasks, but to each his own. Or a Serge Gainsbourg said, "A son gout est l'appetit, l'appetit est a son gout." (One's taste is according to one's appetite, one's appetite is according to one's taste.")
| [reply] [Watch: Dir/Any] |
(ichimunki) re: Date processor
by ichimunki (Priest) on Feb 23, 2001 at 22:50 UTC
|
This might not be a bad way to go if you are the only one who ever uses this script. But I'd think a series of splits combined with a clear "if" rather than the embedded ? will make the code a lot clearer and perhaps execute more quickly.
my $time_stamp = "2-19-01 3:22 PM";
my($date, $time, $am_pm) = split(/ /, $time_stamp);
my($month, $day, $short_year) = split(/-/, $date);
my($hours, $min) = split(/:/, $time);
if ($am_pm =~ /pm/i) {
$hours += 12;
}
my $year = 2000 + short_year;
my $sql_time_stamp = "$year-$month-$day $hours:$min"
I know this looks a lot more like "baby talk Perl", but ask yourself which one you can read more easily without background info. Also ask yourself which one is easier to change if your date format (into or out of this function) ever changes. | [reply] [Watch: Dir/Any] [d/l] |
Re: Date processor
by tadman (Prior) on Feb 23, 2001 at 22:45 UTC
|
There doesn't appear to be anything fundamentally wrong
with your strategy of using a regexp. There is no point
in using the s/// substitution on your string, however,
seeing as you are matching the entire string (/^...$/),
and assignment using '=', such as you have done,
is probably faster.
If you were changing only part of the string from one
format to another, you would likely be using s///, but
since you are modifying the variables mathematically,
you would have to use the s///e format, with the 'e'
for eval switch turned on. That will evaluate the
second part of the regexp, performing the math and
replacing with the result.
| [reply] [Watch: Dir/Any] |
Re: Date processor
by enoch (Chaplain) on Feb 23, 2001 at 22:52 UTC
|
If you really wantto do this via code and not a prebuilt module, here it is (assuming date format is 'mmm dd yyyy hh:mm(am|pm)' that is 'Feb 19 2001 3:42pm').
use strict;
my $str = "2-19-01 3:49pm";
my %months = qw(1 Jan
2 Feb
3 Mar);
my ($month,$day,$year,$time) = split /-|\s/,$str;
my $dataDate = "$months{$month} $day 20$year $time\n";
print $dataDate;
Jeremy | [reply] [Watch: Dir/Any] [d/l] |
Re: Date processor
by SilverB1rd (Scribe) on Feb 23, 2001 at 22:39 UTC
|
I dont see how a s/// is going to help you any.
For more information on regexp try going, here and here
Price of Freedom is Eternal Vigilance
| [reply] [Watch: Dir/Any] |
Re: Date processor
by tune (Curate) on Feb 24, 2001 at 00:19 UTC
|
| [reply] [Watch: Dir/Any] |