SHOULD I USE DATE::MANIP
If you look in CPAN, you'll find that there are a number
of Date and Time packages. Is Date::Manip the one you
should be using? In my opinion, the answer is no about
90% of the time. This sounds odd coming from the author
of the software, but read on.
You will probably want to read the rest of that section before making a decision to use that module :)
/J\ | [reply] [d/l] |
The decision to use that module or not depends mostly on performance, as pointed out by davorg in his book. We pay a high price when we have a high number of dates to scan and convert.
My personal choice is to use a regex if I only have a small script with just one spot dealing with simple dates.
However, when dealing with databases, I use Date::Manip, even if I have only one occurrence in the script. The reason is that in this way, I have a consistent management of dates in all my database scripts.
We should compare efficiency vs. ease of coding. I mostly use Perl for database maintenance and data migration (with DBI, of course!) Especially for the latter, Date::Manip is wonderful. I can get dates from the most exotic formats and throw them at the database. We might say that, having a million records to migrate, it can take five minutes more than using a simple regex. True, but if our simple regex was too simple and not smart enough to deal with different date formats, then we might end up coding for hours trying to get it right, while Date::Manip will relieve us of such burden.
Every situation is different. We should make some accurate measurement if we want to include Date::Manip for web usage. But for administration, AFAIAC, it's a godsend.
_ _ _ _
(_|| | |(_|><
_|
| [reply] [d/l] |
#!/usr/bin/perl -w
use strict;
$_ = "[27/Feb/2002:05:05:51 -0800]";
my %months = (
Jan => '01',
Feb => '02',
# ...
Dec => '12',
);
m|\[(\d*)/(\D*)/(\d*):(\d*:\d*:\d*) [^\]]*\]|;
print "$3-$months{$2}-$1 $4";
That regex can probably be polished up a bit, but it should suit your purposes. If you do want to go ahead with Date::Manip, something like this should work:
#!/usr/bin/perl -w
use strict;
use Date::Manip;
$_ = "[27/Feb/2002:05:05:51 -0800]";
s/\[|\]//g; # Strip the brackets for Date::Manip
$_ = ParseDate($_);
s/://g; # Strip the colons for MySQL
print;
If you decide to use either implementation, please test them thoroughly before use.
-- grummerX
| [reply] [d/l] [select] |