The Time::Local core module, along with localtime, can get you day, month and year periods pretty easily:
#!/usr/bin/perl use strict; use warnings; use Time::Local; my $t = time; ## or, plug in your own timestamp my ($day_start, $day_end) = day_period ($t); my ($month_start, $month_end) = month_period ($t); my ($year_start, $year_end) = year_period ($t); print "The day starts at ", scalar localtime $day_start, "\n"; print "The day ends at ", scalar localtime $day_end, "\n"; print "The month starts at ", scalar localtime $month_start, "\n"; print "The month ends at ", scalar localtime $month_end, "\n"; print "The year starts at ", scalar localtime $year_start, "\n"; print "The year ends at ", scalar localtime $year_end, "\n"; sub day_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, $mday, $mon, $year); my $t2 = timelocal (0, 0, 0, $mday+1, $mon, $year); wantarray ? ($t1, $t2) : \($t1, $t2) } sub month_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, 1, $mon, $year); my $t2 = timelocal (0, 0, 0, 1, $mon+1, $year); wantarray ? ($t1, $t2) : \($t1, $t2) } sub year_period { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me shift; my $t1 = timelocal (0, 0, 0, 1, 0, $year ); my $t2 = timelocal (0, 0, 0, 1, 0, $year+1); wantarray ? ($t1, $t2) : \($t1, $t2) }
Output:
The day starts at Sat Nov 3 00:00:00 2007 The day ends at Sun Nov 4 00:00:00 2007 The month starts at Thu Nov 1 00:00:00 2007 The month ends at Sat Dec 1 00:00:00 2007 The year starts at Mon Jan 1 00:00:00 2007 The year ends at Tue Jan 1 00:00:00 2008
If you need week-long periods, you can either use a heavier, non-core module like Date::Manip, or write the logic yourself (hope you like special cases!).

Update: And if you want the periods to end just at the end of the day/month/year rather than at the beginning of the next one... subtract 1 from $XXXX_end.


In reply to Re: Date calculation: start and end of period by gamache
in thread Date calculation: start and end of period by dgaramond2

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.