Re: DateTime format of date



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/"
.



Relevant Pages

  • Re: Help regarding Date
    ... my $mdate = strftime; ... specific and often use of Posix creates problems. ... Purl Gurl ... print $datetime; ...
    (perl.beginners)
  • Re: DateTime format of date
    ... Looking through the documentation for DateTime I find I can only find ... four digit year formats. ... You're already using POSIX, so ... you can just remove the first two digits of the year: ...
    (comp.lang.perl.misc)
  • WS Works Locally but not on Web Server
    ... I have a project that contains a WebService that works great when connecting ... Once we post the files to the test web server, ... BrickRed.RealEstateManagement.BackboneWS.RealEstatePointeBackbone.UTCToUserTime(Int32 iTimeZoneID, DateTime dtUTCTime) ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: ISO to Gregorian, strptime madness
    ... datetime the class in the datetime module has strftime but no strptime ... type object 'datetime.datetime' has no attribute ...
    (comp.lang.python)
  • Re: why not datetime.strptime() ?
    ... but it's not quite as trivial to implement as was strftime() support. ... > the datetime C code, strptime() is implemented in Python as part of the time ... > need to import the time module, call its strptimefunction with the input ...
    (comp.lang.python)