DateTime format of date

J

Justin C

I need to output the date as six digits DDMMYY, with leading zeros where
necessary, and only the last two digits of the year.

Looking through the documentation for DateTime I find I can only find
four digit year formats. I know there's always more than one way to do
it, but I can't see an easy way other than:

=== begin ===
#!/usr/bin/perl

use strict;
use warnings;
use POSIX;
use DateTime;

my $date = DateTime->now();

my $year = substr($date->year, 2, 2);
my $month = substr($date->dmy, 3, 2);
my $day = substr($date->dmy, 0, 2);

my $formattedDate = $day . $month . $year;
print "$formattedDate\n";
=== end ===

Is there a 'cleaner' way, just using the DateTime module?

Justin.
 
J

Justin C

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.
strftime = STRing Formatting of TIME?
.... and why is the documentation for it in DateTime when you don't need
the DateTime module for it to run? There's obviously still a *lot* for
me to learn.

However, you can just remove the first two digits of the year:

my $fmtDate = $date->dmy();
substr($fmtDate, 4, 2) = '';

or even as one line:

substr( my $fmtDate = $date->dmy(), 4, 2) = '';

Yeah, but it's not as clear/elegant as your first suggestion.

Thank you for you help. I hope you have the time to explain my queries
above.

Justin.
 
T

Tad J McClellan

Justin C said:
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.



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";
 
J

Justin C

[big snippage]
It appears that you should learn a bit about packages, modules
and exporting of symbols into the main:: namespace. Start with:

perldoc perlmod

[more big snippage]

I've read it, I even understand some of it. But I've left it as 'unread'
because I definitely need to come back to it.

I agree, I do need a better understanding of modules, especially as I've
written a couple... and they work (do not ask me how, really, I don't
know). So this looks like as good a time as any to take another step
forward in my Perl eddycation.

Thanks for the post. Though I'm not sure I'll thank you when I start
getting bogged down in the docs!

Justin.
 
E

Eric Pozharski

Works perfectly, thank you. But I'm not sure what's going on.
strftime = STRing Formatting of TIME?
... and why is the documentation for it in DateTime when you don't need
the DateTime module for it to run? There's obviously still a *lot* for
me to learn.

(mere attempt to make you more confused) Consider reading something
about Unix programming. That will make rounds about C (the very old
stuff the Perl is still written in, you know) obviously, but when you'll
succeed you'll acquire better understanding what Perl does.

*SKIP*
Yeah, but it's not as clear/elegant as your first suggestion.

I beg to differ. Either clearness or elegance is matter of taste. As
of me this one is cute and teaching.
 
J

Justin C

(mere attempt to make you more confused) Consider reading something
about Unix programming. That will make rounds about C (the very old
stuff the Perl is still written in, you know) obviously, but when you'll
succeed you'll acquire better understanding what Perl does.

Does reading K N King 'C. Programming: A Modern Approach' count? I'm
getting through it slowly :)

Justin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top