Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > DateTime format of date

Reply
Thread Tools

DateTime format of date

 
 
Justin C
Guest
Posts: n/a
 
      03-18-2009
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.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
Justin C
Guest
Posts: n/a
 
      03-19-2009
On 2009-03-18, Glenn Jackman <(E-Mail Removed)> 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.
>>
>> 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?

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

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      03-20-2009
Justin C <(E-Mail Removed)> wrote:
> On 2009-03-18, Glenn Jackman <(E-Mail Removed)> 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/"
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      03-20-2009
On 2009-03-20, Tad J McClellan <(E-Mail Removed)> wrote:

[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.

--
Justin C, by the sea.
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      03-20-2009
On 2009-03-19, Justin C <(E-Mail Removed)> wrote:
> On 2009-03-18, Glenn Jackman <(E-Mail Removed)> wrote:

*SKIP*
>> 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.


(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*
>> substr( my $fmtDate = $date->dmy(), 4, 2) = '';

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

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      03-23-2009
On 2009-03-20, Eric Pozharski <(E-Mail Removed)> wrote:
> On 2009-03-19, Justin C <(E-Mail Removed)> wrote:
>> On 2009-03-18, Glenn Jackman <(E-Mail Removed)> wrote:

> *SKIP*
>>> 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.

>
> (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.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments