Simple question: how do I do day/month arithmetic?

C

Cyde Weys

This is a pretty simple question, and I know there's a way to
brute-force it by just giving it a look-up table of the number of days
in each month, but I was wondering if there's a more elegant way
(perhaps using some Perl calendar library or something?)

Anyway, I want to write a Perl script that says how many days are left
until a certain date. A sort of count-down timer, if you will. It will
be using some sort of system clock call to get the current date (also,
how do you do that?)

Thanks for the help. I know this is pretty easy stuff, but I'm a
beginner at Perl.
 
G

Gregory Toomey

Cyde said:
This is a pretty simple question, and I know there's a way to
brute-force it by just giving it a look-up table of the number of days
in each month, but I was wondering if there's a more elegant way
(perhaps using some Perl calendar library or something?)

Anyway, I want to write a Perl script that says how many days are left
until a certain date. A sort of count-down timer, if you will. It will
be using some sort of system clock call to get the current date (also,
how do you do that?)

Thanks for the help. I know this is pretty easy stuff, but I'm a
beginner at Perl.

Use the Date::Calc module.

gtoomey
 
G

Gunnar Hjalmarsson

Cyde said:
I want to write a Perl script that says how many days are left
until a certain date.

As Gregory mentioned, the Date::Calc module has a function for that task.

This is a solution that makes use of the standard module Time::Local
instead:

use Time::Local;
sub daysleft {
my ($y, $m, $d) = @_;
my $future = timelocal 0, 0, 0, $d, $m-1, $y-1900;
my $today = timelocal 0, 0, 0, (localtime)[3..5];
($future - $today) / 86400
}

my $futuredate = '2004-03-25';
print "It's ", daysleft( split /-/, $futuredate ),
" days til $futuredate.\n";
 
A

Anno Siegel

Gunnar Hjalmarsson said:
my $today = timelocal 0, 0, 0, (localtime)[3..5];
^ ^
Sorry for the interruption, but here we have another case where "()"
may be said to supply list context. Just thought I'd mention it.

Anno
 
G

Gunnar Hjalmarsson

Anno said:
Gunnar said:
my $today = timelocal 0, 0, 0, (localtime)[3..5];
^ ^
Sorry for the interruption, but here we have another case where
"()" may be said to supply list context. Just thought I'd mention
it.

Talking about context ... What are you referring to? ;-)
 
X

Xaonon

Cyde Weys <[email protected]> said:
This is a pretty simple question, and I know there's a way to
brute-force it by just giving it a look-up table of the number of days
in each month, but I was wondering if there's a more elegant way
(perhaps using some Perl calendar library or something?)

That would be Date::Calc.
Anyway, I want to write a Perl script that says how many days are left
until a certain date. A sort of count-down timer, if you will. It will
be using some sort of system clock call to get the current date (also,
how do you do that?)

Ha, just the other day I wrote a little script to do that very thing:

#!/usr/bin/perl

use strict;
use warnings;

use Date::Calc qw( Delta_Days );

my ($end_year, $end_month, $end_day) = ( 2012, 12, 21 );
my ($day, $month, $year) = (localtime)[3..5];
$year += 1900;
++$month;

my $left = Delta_Days( $year, $month, $day, $end_year, $end_month, $end_day );
print "There are $left days remaining.\n";

Substitute in your own values, of course, or get them from the command
line. (Incidentally, there are 3224 days remaining.)
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Anno said:
Gunnar said:
my $today = timelocal 0, 0, 0, (localtime)[3..5];
^ ^
Sorry for the interruption, but here we have another case where
"()" may be said to supply list context. Just thought I'd mention
it.

Talking about context ... What are you referring to? ;-)

Sorry, that's a cross-thread reference. A few people, Uri and myself
included, were discussing how list/scalar context was provided, but
I can't remember the topic this happened under. Of course, it's not
the one current topic with "context" in it :)

Anno
 
C

Clayton L. Scott

Cyde Weys said:
This is a pretty simple question, and I know there's a
way to in each month, but I was wondering if there's a more
elegant way (perhaps using some Perl calendar library or
something?)
Anyway, I want to write a Perl script that says how
many days are left until a certain date. A sort of
count-down timer, if you will. It will be using some sort of
system clock call to get the current date (also, how
do you do that?)

DateTime.pm is one of the better is not the best module in the
date/time space

http://datetime.perl.org

Clayton
 

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
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top