I don't use any of the Data modules, and do not know how good they are... but I hope they would be fine to use for what you are doing.

However, I am a little concerned about some of the other suggestions, such as adding 86400 seconds to the current day, etc. The problem is with how our calendar works. Do you properly take into account leap-years with Feb 29th? Do you properly take into account that a time change may actually make a day last 23 hour or 25 hours (in which case, adding 86400 isn't enough to give you the next day)?

I have fought with these issues myself, and being a minimalist (as in, it is easier to run my code on any machine if I didn't have to install a bunch of perl modules prior to running the program), I settled on converting my dates to Julian dates and back again. The advantage of Julian dates is that your date is converted to an integer, and can then be operated on using normal math. When you need a real date, just convert the julian date back...

### ## Convert a YYYY-MM-DD date to a Julian date ### sub date_to_julian ( $ ) { my $date = shift @_; my $y = substr($date, 0, 4); my $m = substr($date, 5, 2); my $d = substr($date, 8, 2); my $j = int((1461 * ($y + 4800 + int(($m - 14) / 12))) / 4) + int((367 * ($m - 2 - 12 * int((($m - 14) / 12)))) / 12) - int((3 * int((($y + 4900 + int(($m - 14) / 12)) / 100))) / 4) + $d - 32075; return $j; } ### ## Convert a Julian date to YYYY-MM-DD date ### sub julian_to_date ( $ ) { my $j = shift @_; my ($l, $n, $i, $d, $m, $y); $l = $j + 68569; $n = int((4 * $l) / 146097); $l = $l - int((146097 * $n + 3) / 4); $i = int((4000 * ($l + 1)) / 1461001); $l = $l - int((1461 * $i) / 4) + 31; $j = int((80 * $l) / 2447); $d = $l - int((2447 * $j) / 80); $l = int($j / 11); $m = $j + 2 - (12 * $l); $y = 100 * ($n - 49) + $i + $l; return sprintf("%04d-%02d-%02d", $y, $m, $d); } ### ## Print all the dates between a start and end date ### my $start_date = date_to_julian("2005-07-12"); my $end_date = date_to_julian("2005-08-03"); for (my $i = $start_date; $i <= $end_date; $i++) { print julian_to_date($i), "\n"; }

The information on Julian calendars was found by doing an ordinary Google search. The formulas were gleaned from one of the sites I found. There are two types of formulas, one that operates on just dates, and another that includes the hours/minutes/seconds. I am using the simpler date-only version of the formulas.

Hope that helps!
Zucan


In reply to Re: How to increase date in a loop by Zucan
in thread How to increase date in a loop by nejcPirc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.