Re: DateTime format of date
- From: Tad J McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Fri, 20 Mar 2009 07:58:21 -0500
Justin C <justin.0903@xxxxxxxxxxxxxx> wrote:
On 2009-03-18, Glenn Jackman <glennj@xxxxxx> wrote:
At 2009-03-18 10:21AM, "Justin C" wrote:
I need to output the date as six digits DDMMYY, with leading zeros where
necessary, and only the last two digits of the year.
use POSIX;
use DateTime;
Is there a 'cleaner' way, just using the DateTime module?
Do you /need/ to use DateTime for this? You're already using POSIX, so
No, I don't need to use DateTime, and POSIX slipped in in error, I was
reading the DateTime docs, got to strftime and got confused. I
understood it as time input rather than output.
my $formattedDate = strftime "%d%m%y", localtime;
Works perfectly, thank you. But I'm not sure what's going on.
Glenn's code uses the "strftime" subroutine provided by the POSIX module.
strftime = STRing Formatting of TIME?
Right.
... and why is the documentation for it in DateTime
It isn't.
when you don't need
the DateTime module for it to run?
DateTime also defines something that happens to be named "strftime".
The documentation for DateTime documents the strftime that it provides.
The documentation for POSIX documents the strftime that it provides.
Which documentation applies is determined by which strftime()
you are using.
There's obviously still a *lot* for
me to learn.
It appears that you should learn a bit about packages, modules
and exporting of symbols into the main:: namespace. Start with:
perldoc perlmod
You can get the same output as from the POSIX module if you use
DateTime correctly:
--------------------------
#!/usr/bin/perl
use warnings;
use strict;
use DateTime; # exports nothing because it is object oriented
my $dt = DateTime->now(); # need a DateTime object to use DateTime module
my $dt_formattedDate = $dt->strftime("%d%m%y");
print "DateTime: $dt_formattedDate\n";
--------------------------
Your code has
use POSIX;
which will import lots (over 500 of them!) of symbols into the
main (global) namespace. Let's find out exaclty how many
(see the "Symbol Tables" section in perlmod.pod):
--------------------------
#!/usr/bin/perl
use warnings;
use strict;
BEGIN {
$_ = keys %main::;
warn "before: $_\n";
}
use POSIX;
BEGIN {
$_ = keys %main::;
warn "after: $_\n";
}
--------------------------
Importing hundreds of names into the global namespace like that is
a Very Bad Idea. So you should avoid using just "use POSIX;".
You can either import only the symbols (names) that you plan to use:
--------------------------
#!/usr/bin/perl
use warnings;
use strict;
use POSIX 'strftime'; # exports 1 symbol
my $posix_formattedDate = strftime "%d%m%y", localtime;
print "POSIX : $posix_formattedDate\n";
--------------------------
Or, even better, import NO symbols and use the fully-qualified name instead:
--------------------------
#!/usr/bin/perl
use warnings;
use strict;
use POSIX (); # exports no symbols because we told it not to
my $posix_formattedDate = POSIX::strftime "%d%m%y", localtime;
print "POSIX : $posix_formattedDate\n";
--------------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
.
- Follow-Ups:
- Re: DateTime format of date
- From: Justin C
- Re: DateTime format of date
- References:
- DateTime format of date
- From: Justin C
- Re: DateTime format of date
- From: Glenn Jackman
- Re: DateTime format of date
- From: Justin C
- DateTime format of date
- Prev by Date: Confusion in perlrun documentation
- Next by Date: FAQ 1.16 How can I convince others to use Perl?
- Previous by thread: Re: DateTime format of date
- Next by thread: Re: DateTime format of date
- Index(es):
Relevant Pages
|