http://www.perlmonks.org?node_id=741862


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

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 Re: How to get time in seconds from localtime(time) to next 21st date of the month?
  • Download Code

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.