Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

( #47507=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie basman on Dec 20, 2000 at 03:14 UTC
Q&A  > dates and times


Description:

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.

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

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.
Answer: How to get time in seconds from localtime(time) to next 21st date of the month?
contributed by davorg

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
Answer: How to get time in seconds from localtime(time) to next 21st date of the month?
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";
Answer: How to get time in seconds from localtime(time) to next 21st date of the month?
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

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2014-08-31 06:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (294 votes), past polls