Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Calendar Question

Reply
Thread Tools

Perl Calendar Question

 
 
amerar@iwc.net
Guest
Posts: n/a
 
      06-01-2005

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

 
Reply With Quote
 
 
 
 
mothra
Guest
Posts: n/a
 
      06-01-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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:uration;

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

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

print $new_dt;

Hope this helps

Mothra


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      06-01-2005
(E-Mail Removed) wrote:

> 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.
 
Reply With Quote
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      06-01-2005


(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-01-2005
(E-Mail Removed) wrote:

> 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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-01-2005
Brian McCauley wrote:
> (E-Mail Removed) wrote:
>> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-01-2005
Brian McCauley wrote:
> (E-Mail Removed) wrote:
>
>> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-02-2005

Gunnar Hjalmarsson wrote:

> Brian McCauley wrote:
>
>> (E-Mail Removed) wrote:
>>
>>> 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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-02-2005
Brian McCauley wrote:
> Gunnar Hjalmarsson wrote:
>> 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:


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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      06-02-2005
Brian McCauley wrote:
> (E-Mail Removed) wrote:
>
>> 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 have used Date::Calc before for the exact same reason. Works great.

--Ala
 
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
Calendar GadGets does not show calendar iccsi Windows 64bit 4 08-14-2011 01:52 PM
How to Synchronise Hotmail Calendar with Standard Outlook Calendar using Outlook Connector ?? Synapse Syndrome Computer Support 0 12-02-2007 04:19 AM
Thunderbird Calendar with Exchange 2003 Calendar and Public FoldersCalendar jincmcse Firefox 1 09-03-2005 02:46 AM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net 3 06-23-2004 01:41 PM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net Web Controls 0 06-23-2004 12:06 PM



Advertisments