Problems? Is your data what you think it is? PerlMonks

### Re: Determining the dayname for the last day of any given month

by davorg (Chancellor)
 on Jun 19, 2006 at 11:30 UTC ( #556229=note: print w/replies, xml ) Need Help??

Using only Perl standard modules:

```use strict;
use warnings;

use Time::Local;
use POSIX 'strftime';

while (1) {
print "Enter year (yyyy) and month (mm), separated by a space: ";
chomp(my \$input = <STDIN>);
last if !\$input;
next unless \$input =~ /(\d{4})\s+(\d\d?)/;

my (\$year, \$month) = (\$1, \$2);
my (\$y, \$m) = (\$year, \$month);

\$y -= 1900;
if (\$m == 12) {
\$m = 0;
++\$y;
}
my \$first = timelocal(0, 0, 0, 1, \$m, \$y);
my \$last = \$first - 24*60*60;

print "The last day of \$month/\$year will be a ",
strftime('%A', localtime \$last), "\n";
}
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Determining the dayname for the last day of any given month
by ikegami (Pope) on Jun 20, 2006 at 15:40 UTC

Not all days are 24*60*60 seconds long, and \$m suffers from off-by-one errors. Fix:

```  my (\$y, \$m) = (\$year-1900, \$month-1);

# Day before the first of next month:
my \$last = timelocal_nocheck(0, 0, 0, 1-1, \$m+1, \$y);

You'll need to import timelocal_nocheck from Time::Local

Updated. However, the fixed code doesn't work because timelocal_nocheck doesn't handle \$m+1 as I expect.

Not all days are 24*60*60 seconds long

Well the ones on the first of the month all are :)

But you're right, they aren't guaranteed to _always_ be. The easiest fix is probably to change the call to timelocal so it uses midday rather than midnight.

\$m suffers from off-by-one errors

I don't think it does. The value you get from the user is in the range 1-12. We want the next month, but timelocal wants the number in the range 0-11. So we already have the correct number (except we need to do some adjustment if the month is 12). It might not be the clearest algorithm in the world, but it _is_ correct.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

We want the next month

Duh! Of course!

Create A New User
Node Status?
node history
Node Type: note [id://556229]
help
Chatterbox?
 [Corion]: But I have a very positive experience with a four day workweek and a three day weekend. I can't easily go back though to full money. [Corion]: That is easy without having to pay for a house, a wife or children though. If I had any of these, or any two of these, the decision wouldn't be that easy. [ambrus]: wait. I understand no wife and children, but how do you not have to pay for a house? [hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash. [Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs [hippo]: The thesholds/benefits balance at the time was nuts. [hippo]: But that doesn't last because, you know ... politicians. :( [Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well [ambrus]: ah good. I pay only rent too. but that still costs significant money. [Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-09-21 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (249 votes). Check out past polls.

Notices?