Perl-Sensitive Sunglasses PerlMonks

### Answer: How to get time in seconds from localtime(time) to next 21st date of the month?

by poolpi (Hermit)
 on Feb 06, 2009 at 10:47 UTC Need Help??

Q&A > dates and times > How to get time in seconds from localtime(time) to next 21st date of the month? - Answer contributed by poolpi

POSIX example

```#!/usr/bin/perl
use strict;
use warnings;
use POSIX ();

my ( \$sec, \$min, \$hour, \$mday, \$mon, \$year, \$wday, \$yday, \$isdst ) =
POSIX::localtime(time);

print POSIX::difftime(
POSIX::mktime(
0, 0, 12, 21,
( \$mday >= 21 ? ( ( \$mon + 1 ) % 11 ) : \$mon ),
( ( \$mon == 11 and \$mday >= 21 ) ? \$year + 1 : \$year )
),
POSIX::mktime( \$sec, \$min, \$hour, \$mday, \$mon, \$year )
),
"\n";

• Comment on Answer: How to get time in seconds from localtime(time) to next 21st date of the month?
Replies are listed 'Best First'.
Re: Answer: How to get time in seconds from localtime(time) to next 21st date of the month?
by doug (Pilgrim) on Feb 06, 2009 at 18:00 UTC
#!/usr/bin/perl

use strict;
use warnings;

use Date::Manip;

my \$now = ParseDate 'now';
my \$then = ParseDate 'next month';
substr(\$then,6,2) = 21;
my \$delta = DateCalc \$now, \$then;
my \$seconds = Delta_Format \$delta, 0, '%sh';
print "seconds=\$seconds days=", \$seconds/(60*60*24), "\n";

Directly hacking the Date::Manip date format to stick a 21 in the day field is ugly, but couldn't remember how to do it with the API.

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-06-28 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (638 votes). Check out past polls.