You example output is a bit different that what you asked for in text. I assume example output is what you want, generate date strings between previous and current...

If you can't install a module, this isn't much code to just roll a couple of simple routines that convert your string format into "epoch seconds" and also convert "epoch seconds" back into your string.

Notes: the $month value is from [0..11] instead of [1..12] to make it easy to use it as an index to look up some text for the month in an array.

If these are local times instead of UTC times, then you will have to consider what happens if the date span includes the daylight savings time switchover. The local versions, ie timelocal and localtime know about DST. And you can figure out if a time is during the DST period by a flag that is returned if you want some special handling of that case. Start with localtime and go from there. However, the DateTime module would make your life easier if you are indeed working in local time - you don't say... See other postings about installing modules. timegm() does know about leap years so that isn't a problem.

The Time::Local module will be installed already.

#!/usr/bin/perl -w use strict; use Time::Local; my $previous = '10111623'; my $current = '10111705'; my $onehour = 60*60; #hour in seconds is 60sec*60min my $cur = date2timegm($previous); my $stop = date2timegm($current); my @result; while ( ($cur+=$onehour) < $stop) { push(@result, timegm2date($cur)); } print join(',',@result),"\n"; #prints: 10111700,10111701,10111702,10111703,10111704 sub timegm2date # returns date string { my $epoch = shift; my ($yr,$mon,$day,$hour) = (gmtime($epoch))[5,4,3,2]; $mon++; $yr+= 1900; $yr = ($yr =~ m/(\d\d)$/)[0]; return (sprintf ("%02d%02d%02d%02d", $yr, $mon, $day, $hour)); } sub date2timegm #returns an epoch time in seconds { my $date = shift; my ($yr,$mon,$day,$hour) = $date =~ m/\d\d/g; --$mon; return (timegm(0,0,$hour,$day,$mon,$yr)); }

In reply to Re: Date and time difference by Marshall
in thread Date and time difference by Anonymous Monk

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.