Re: Two digit year with DateTime?
by haukex (Archbishop) on Mar 11, 2019 at 11:49 UTC
|
I would recommend against a two-digit year, but if you must, see DateTime's strftime method:
use warnings;
use strict;
use DateTime;
my $start_of_week = DateTime->today()->truncate( to => 'week' );
for ( 0..6 ) {
my $teden = $start_of_week->clone()->add( days => $_ );
print $teden->strftime('%m-%d-%y'), "\n";
}
__END__
03-11-19
03-12-19
03-13-19
03-14-19
03-15-19
03-16-19
03-17-19
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Two digit year with DateTime?
by hdb (Monsignor) on Mar 11, 2019 at 13:34 UTC
|
DateTime lets you specify a formatter, which is used whenever you print or stringify a DateTime object:
use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
my $start_of_week =
DateTime->today()
->truncate( to => 'week' );
$start_of_week->set_formatter( DateTime::Format::Strptime->new(pattern
+ => "%d-%m-%y") );
for ( 0..6 ) {
my $teden = $start_of_week->clone()->add( days => $_ );
print "$teden\n";
}
This is useful when you print in many places of your code and want the same format. Remark: installing DateTime::Format::Strptime unleashed a cascade of dependencies on my Perl install...
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Two digit year with DateTime?
by thanos1983 (Parson) on Mar 11, 2019 at 11:52 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;
use feature 'say';
# my $tz = new Date::Manip::TZ;
say UnixDate(DateCalc(
ParseDate('yesterday'),
$_ . " days later") , "%m-%d-%y") for (1..7);
__END__
$ perl test.pl
03-11-19
03-12-19
03-13-19
03-14-19
03-15-19
03-16-19
03-17-19
Update: Removing unnecessary line of code.
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: Two digit year with DateTime?
by johngg (Canon) on Mar 11, 2019 at 18:30 UTC
|
Time::Piece has been a core module since at least 5.10 so can save you having to install extra modules.
johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MTime::Piece -E
+'
my $tp = localtime();
say qq{2-digit year - }, $tp->yy();'
2-digit year - 19
I hope this is helpful.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
Re: Two digit year with DateTime?
by karlgoethebier (Abbot) on Mar 11, 2019 at 17:46 UTC
|
Two numbers for the year is a bad idea. And to avoid the monster you may take a look at DateTime::Tiny. Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help
| [reply] [Watch: Dir/Any] [d/l] |
Re: Two digit year with DateTime?
by ikegami (Patriarch) on Mar 12, 2019 at 00:24 UTC
|
As an aside, DateTime->today() very likely incorrect because it doesn't return the local date. If you want the local date, you could use
DateTime->today( time_zone => 'local' )
However, that will fail on a day without midnight. The following is the correct approach is:
DateTime
->now( time_zone => 'local' )
->set_time_zone('floating')
->truncate( to => 'day' )
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Two digit year with DateTime?
by misc (Friar) on Mar 13, 2019 at 00:07 UTC
|
So, that's the well known Year2029 Bug.
Oh. I see. It will even repeat from now on in ten years, every day.
(Not like the Year 2000-Bug.)
But anyways, you don't write what you are trying to do,
so here comes some general advice.
And I don't know, which level you are in programming,
so my 2 cents are even more unspecified.
I'd try to accomplish things as simple as possible.
Reading your question, I immediately thought of a regex,
next thought was grep/map.
If you know these tools, you can accomplish much more
than only changing the digits of a date.
And even when you decide other, (hopefully)
and use the date in another format,
you've learned something valuable.
Best wishes, Michael
| [reply] [Watch: Dir/Any] |
Re: Two digit year with DateTime?
by Anonymous Monk on Mar 12, 2019 at 01:14 UTC
|
Pox on you for using 2-digit year. If you are being forced to do that, do pass the pox along.
| [reply] [Watch: Dir/Any] |