Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Adding Days To YYYYMMDD Date Format

by sauoq (Abbot)
on May 17, 2012 at 00:25 UTC ( [id://970961]=note: print w/replies, xml ) Need Help??


in reply to Adding Days To YYYYMMDD Date Format

The right way to do this is to parse it into a number of seconds as would be returned by time() and then to add the number of seconds in a day times the number of days you want, and then convert it back.

use Date::Parse; my $day = 24*60*60; my $x = 3; # set this. my $xdays = $x * $day; print scalar localtime(str2time("20121231") + $xdays);

Update: Changed to produce the requested format and to use the given example. Apparently I have too much time on my hands.

use Date::Parse; my $date = "20120516"; my $day = 24*60*60; my $x = 24; # set this. my $xdays = $x * $day; # see perldoc -f localtime. my @t = (localtime(str2time($date) + $xdays))[5,4,3]; $t[0] += 1900; # localtime returns years since 1900. $t[1] += 1; # localtime returns month in range 0..11. printf "%04d%02d%02d\n", @t;

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: Adding Days To YYYYMMDD Date Format
by Tux (Canon) on May 17, 2012 at 07:36 UTC

    When doing a lot of these calculations with dates, keeping the dates in a standard localtime list can prove very useful. When iso-dates are to be generated like in this example, calculation is faster than printf:

    use Benchmark qw(cmpthese); my @d = localtime; cmpthese (-1, { prnt => sub { my $x = sprintf "%d%02d%02d", 1900 + $d[5], $d[4] + 1, $d[3]; }, calc => sub { my $x = (($d[5] + 1900) * 100 + $d[4] + 1) * 100 + $d[3]; }, }); => Rate prnt calc prnt 2025658/s -- -37% calc 3215550/s 59% --

    And yes, I did have a long running process where millions of these were done, so it did matter.


    Enjoy, Have FUN! H.Merijn
Re^2: Adding Days To YYYYMMDD Date Format
by Zoomie (Novice) on May 25, 2012 at 15:19 UTC

    Hi sauoq.

    I added the module Date::Parse from CPAN and your script runs perfectly.

    Thanks so much for your help...

Re^2: Adding Days To YYYYMMDD Date Format
by Zoomie (Novice) on May 17, 2012 at 16:07 UTC

    Hi Sauoq,

    I installed the Date module. (I use Active State Perl).

    When I test your script, I got the message:

    Can't locate Date/Parse.pm.

    Any idea what I should do?

    Thanks...

      Can't locate Date/Parse.pm.

      Any idea what I should do?

      Buy a chicken. Wait for full moon. At midnight, draw a pentragram with its blood. Place yourself and your computer inside the pentagram. Then, open a terminal and type cpan Date::Parse.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Try the Date::Calc solution below as it works using the Active State distribution.

Re^2: Adding Days To YYYYMMDD Date Format
by Anonymous Monk on Dec 05, 2018 at 11:22 UTC

    I understand that this is a veery old post. I needed a function to help me create a range of dates starting a few days ago and ending at the current date. I played with different dates and I discovered that something wrong happens around October. The "date" is not incremented anymore after 20181028 or 20171029 or 20161030 and so on. However, after that fatidic date it works fine again.

    See and example:

    Last date 20110101 Bill date 20181205 Data 20110102 Last date 20110101 Bill date 20181205 Data 20110103 Last date 20110101 Bill date 20181205 Data 20110104 ..... Last date 20110101 Bill date 20181205 Data 20111026 Last date 20110101 Bill date 20181205 Data 20111027 Last date 20110101 Bill date 20181205 Data 20111028 Last date 20110101 Bill date 20181205 Data 20111029 Last date 20110101 Bill date 20181205 Data 20111030 Last date 20110101 Bill date 20181205 Data 20111030 Last date 20110101 Bill date 20181205 Data 20111030 Last date 20110101 Bill date 20181205 Data 20111030 ......
    $last_date = <STDIN>; chomp $last_date; for (my $i = $last_date; $i <= $bill_date; $i = AddDate( $i, 1)) { push @letter_dates, $i; printf "Last date %s Bill date %s Data %s\n", $last_date, $bill_da +te, $i; } sub AddDate { my $date = $_[0]; my $x = $_[1]; # set this. my $day = 24*60*60; my $xdays = $x * $day; my @t = (localtime(str2time($date) + $xdays))[5,4,3]; $t[0] += 1900; # localtime returns years since 1900. $t[1] += 1; # localtime returns month in range 0..11. $return_date = sprintf "%04d%02d%02d", $t[0], $t[1], $t[2]; return $return_date; }

    2018-12-06 Athanasius added paragraph and code tags

      In the UK for example, daylight saving means 28 Oct 2018 has 25 hours (90000 seconds) so adding 86400 seconds does not increment the date

      #!perl use strict; use Date::Parse; my $epoch1 = str2time('20181028'); my $epoch2 = str2time('20181029'); my $secs = $epoch2 - $epoch1; printf "%d - %d %d\n",$epoch2,$epoch1,$secs;

      Try setting the timezone to one without daylight saving

      str2time('20181028','GMT');
      poj

Log In?
Username:
Password:

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

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

    No recent polls found