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


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

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