Perl Calendar Question

A

amerar

Hi All,

I need to write some Perl code where I take the current day, subtract 3
from it, and get the proper day of the month.

So, if today is June 15th, then 15-3 would give me June 12th.
But if today is July 1st, then 1-3 'should' give me June 28th.
And then there are leap years......

Is there any code to do this? I'm not sure how to write something like
this......

Thanks,

Arthur
 
M

mothra

Hi All,

I need to write some Perl code where I take the current day, subtract
3 from it, and get the proper day of the month.
use strict;
use warnings;
use DateTime;
use DateTime::Duration;

my $d = DateTime::Duration->new (
days => 3
);

my $dt = DateTime->today();
my $new_dt = $dt -$d;

print $new_dt;

Hope this helps

Mothra
 
B

Brian McCauley

I need to write some Perl code where I take the current day, subtract 3
from it, and get the proper day of the month.

So, if today is June 15th, then 15-3 would give me June 12th.
But if today is July 1st, then 1-3 'should' give me June 28th.
And then there are leap years......

Is there any code to do this?

Take a look on CPAN - I'm sure at several of the Date::* modules will do
this. I'd probably use Date::Manip but it's quite possibly overkill.
 
I

ioneabu

Hi All,

I need to write some Perl code where I take the current day, subtract 3
from it, and get the proper day of the month.

So, if today is June 15th, then 15-3 would give me June 12th.
But if today is July 1st, then 1-3 'should' give me June 28th.
And then there are leap years......

Is there any code to do this? I'm not sure how to write something like
this......

Thanks,

Arthur

#!/usr/bin/perl

use strict;
use warnings;

my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
my $mday = $lt[3];
print $mday."\n";

got my info from perldoc -f localtime

wana
 
B

Brian McCauley

my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
my $mday = $lt[3];

You could have used a list slice.

my $mday = (localtime(time - 60*60*24*3))$lt[3];

But this is one of the classic mistakes. Not all days are 24h. In most
places in the world there is one 23h day and one 25h day each year.
 
G

Gunnar Hjalmarsson

Brian said:
my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
my $mday = $lt[3];

You could have used a list slice.

my $mday = (localtime(time - 60*60*24*3))$lt[3];

But this is one of the classic mistakes. Not all days are 24h. In most
places in the world there is one 23h day and one 25h day each year.

True. One way to take DST into consideration only using a standard module:

use Time::Local;
my ($d, $m, $y) = ( localtime timelocal( 0, 0, 12,
(localtime $^T)[3..5] ) - 3 * 86400 )[3..5];
printf "%d-%02d-%02d\n", $y+1900, $m+1, $d;
 
J

John W. Krahn

Brian said:
my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
my $mday = $lt[3];

You could have used a list slice.

my $mday = (localtime(time - 60*60*24*3))$lt[3];
^^^
Don't you mean:

my $mday = (localtime(time - 60*60*24*3))[3];


:)

John
 
B

Brian McCauley

Gunnar said:
Brian said:
my @lt = localtime(time()-(60*60*24*3)); #now minus 3 days
my $mday = $lt[3];
But this is one of the classic mistakes. Not all days are 24h. In
most places in the world there is one 23h day and one 25h day each year.

True. One way to take DST into consideration only using a standard module:

use Time::Local;

Simpler is to rely on the fact thar DST transition takes place at night,
not the middle of the day:

my $now = time;
my $hour = (localtime $now)[2];
my $middayish = $now + ( 12 - $hour ) * 60 * 60;
my $three_days_ago = $middayish - 3 * 24 * 60 * 60;
my $mday = (localtime $three_days_ago)[3];

Of course, in practice, you'd not have so many intermediate variables.
 
G

Gunnar Hjalmarsson

Brian said:
Simpler is to rely on the fact thar DST transition takes place at night,
not the middle of the day:

Well, that is just what I did as well.

my ($d, $m, $y) = ( localtime timelocal( 0, 0, 12,
(localtime $^T)[3..5] ) - 3 * 86400 )[3..5];
printf "%d-%02d-%02d\n", $y+1900, $m+1, $d;
my $now = time;
my $hour = (localtime $now)[2];
my $middayish = $now + ( 12 - $hour ) * 60 * 60;
my $three_days_ago = $middayish - 3 * 24 * 60 * 60;
my $mday = (localtime $three_days_ago)[3];

Only that your way does not make use of a module.
 
A

Ala Qumsieh

Brian said:
Take a look on CPAN - I'm sure at several of the Date::* modules will do
this. I'd probably use Date::Manip but it's quite possibly overkill.

I have used Date::Calc before for the exact same reason. Works great.

--Ala
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top