Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by basman (Initiate)
on Dec 20, 2000 at 03:14 UTC ( [id://47507]=perlquestion: print w/replies, xml ) Need Help??

basman has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me to get time from localtime(time) to next fixed day of the month (for example: 21st). I.E. I need to get a UNIX seconds value between current time and 12:00am of next 21st date. I would not use Date::Manip. Thanks in advance.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to get time in seconds from localtime(time) to next 21st date of the month?
by Fastolfe (Vicar) on Dec 20, 2000 at 04:19 UTC
    Yes I see your restriction against using Date::Manip, and I'm not going to bother asking what that's all about, but in the event someone else comes upon your question (which is the purpose for posting it into Q&A, right?), they may not have such restrictions placed on their development. In such a case, the following code might be useful:
    use Date::Manip; $diff = &DateCalc("now", "21st at midnight"); $seconds = &Delta_Format($diff, 0, "%st");
    You can certainly use other modules such as Date::Calc, but it will require additional logic.
Re: How to get time in seconds from localtime(time) to next 21st date of the month?
by davorg (Chancellor) on Dec 20, 2000 at 03:34 UTC

    This sounds pretty simple to achieve without modules. I won't write the code, but here's how I'd do it.

    Use time to get the current epoch seconds Use localtime to convert that to date/time If the date is greater than 21 then increment the month number end set the date to 21 and the time to 12am use Time::Local::timelocal to convert that time to epoch seconds Subtract that from your first number
Re: 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

    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";
      #!/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.
Re: How to get time in seconds from localtime(time) to next 21st date of the month?
by I0 (Priest) on Dec 20, 2000 at 03:19 UTC
    Would you use Time::Local or POSIX::mktime?

    Originally posted as a Categorized Answer.

Re: How to get time in seconds from localtime(time) to next 21st date of the month?
by extremely (Priest) on Dec 20, 2000 at 09:46 UTC
    This works nicely for GM time: $t=gmtime(time -(time % 86400)+86400 );

    You'd have to

    Originally posted as a Categorized Answer.

Re: 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

    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
Re: How to get time in seconds from localtime(time) to next 21st date of the month?
by basman (Initiate) on Dec 27, 2000 at 16:56 UTC
    I appreciate everyone who has responded, your guide is really helpful.

    Fastolfe> ...they may not have such restrictions placed on their development...

    I never thought to restrict or not to restrict anything. The reason is little web application I'm currently creating. There is not any necessity to include additional Perl module making this short task. And it's just my opinion.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://47507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-29 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found