in reply to Re: Adding Days To YYYYMMDD Date Format
in thread Adding Days To YYYYMMDD Date Format

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

Replies are listed 'Best First'.
Re^3: Adding Days To YYYYMMDD Date Format
by poj (Abbot) on Dec 05, 2018 at 12:35 UTC

    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