Perl: the Markov chain saw 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 08:36 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

Two examples. (didactical purpose)

```#!/usr/bin/perl
use strict;
use warnings;
use v5.10.0;
use Time::Piece;
use Time::Seconds;
use DateTime;

my \$dt = DateTime->now( time_zone => 'floating' );

my \$next = {
mon => ( \$dt->mday >= 21 ? ( \$dt->mon + 1 ) % 12 : \$dt->mon ),
year =>
( ( \$dt->mon == 12 and \$dt->mday >= 21 ) ? \$dt->year + 1 : \$dt->
+year ),
day => 21,
hms => '12:00:00'
};

\$next->{'dt'} = DateTime->new(
year  => \$next->{'year'},
month => \$next->{'mon'},
day   => \$next->{'day'},
hour  => substr( \$next->{'hms'}, 0, 2 )
);

my \$t = localtime;

print Time::Seconds->new(
Time::Piece->strptime(
sprintf( "%s-%s-%sT%s",
\$next->{'year'}, \$next->{'mon'}, \$next->{'day'}, \$next->{'
+hms'} ),
"%Y-%m-%dT%T"
) - \$t
)->seconds, "\n";

print \$next->{'dt'}->subtract_datetime_absolute(\$dt)->seconds, "\n";

# Output :
#  1308641
#  1308641

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

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-05-23 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (172 votes). Check out past polls.

Notices?