With Date::Manip, if that is accepted, something like the following code might be considered simple and straightforward (read: most of the inherent underlying difficulties remain hidden inside Date::Manip)):
use warnings; use strict; use Date::Manip; my $t = time(); my $date = ParseDateString( "epoch $t" ); my $day_start = Date_SetTime( $date, 0, 0, 0); my $day_end = Date_SetTime( $date, 23, 59, 59); print "\nDay: ", start_end( $day_start, $day_end ); my $err; my ($year, $month, $day, $day_of_week) = UnixDate( $date, '%Y','%m', '%d', '%w'); my $week_start = $day_of_week == 1 ? $date : DateCalc( $date, 'last Monday', \ +$err) ; $week_start = Date_SetTime( $week_start, 0, 0, 0); my $week_end = $day_of_week == 7 ? $date : DateCalc( $date, 'next Sunday', \$e +rr) ; $week_end = Date_SetTime( $week_end, 23, 59, 59); print "\nWeek: ", start_end( $week_start, $week_end ); my $month_start = Date_SetDateField( $date, 'D', 1 ); $month_start = Date_SetTime( $month_start, 0, 0, 0); my $next_month = DateCalc( $date, 'next month' ); my $month_end_epoch = UnixDate( $next_month, '%s' ) - 1; my $month_end = ParseDateString( "epoch $month_end_epoch" ); $month_end = Date_SetTime( $month_end, 23, 59, 59); print "\nMonth: ", start_end( $month_start, $month_end ); my $year_start = Date_SetDateField( $date, 'M', 1); # January $year_start = Date_SetDateField( $year_start, 'D', 1 ); # 1 $year_start = Date_SetTime( $year_start, 0, 0, 0); my $year_end = Date_SetDateField( $date, 'M', 12); # December $year_end = Date_SetDateField( $year_end, 'D', 31 ); # 31 $year_end = Date_SetTime( $year_end, 23, 59, 59 ); print "\nYear: ", start_end( $year_start, $year_end ); exit; sub start_end { my ($date_start, $date_end) = @_; return join ( '', 'starts at ', UnixDate( $date_start, '%O'), ', ends at ', UnixDate( $date_end, '%O'), "\n\t(", UnixDate( $date_start, '%s'), ' - ', UnixDate( $date_end, '%s'), ")\n", ); }

which produces output like
Day: starts at 2007-11-04T00:00:00, ends at 2007-11-04T23:59:59 (1194127200 - 1194213599) Week: starts at 2007-10-29T00:00:00, ends at 2007-11-04T23:59:59 (1193608800 - 1194213599) Month: starts at 2007-11-01T00:00:00, ends at 2007-11-30T23:59:59 (1193868000 - 1193954399) Year: starts at 2007-01-01T00:00:00, ends at 2007-12-31T23:59:59 (1167602400 - 1199138399)
A slight simplification is done anyway: timezone of the starting epoch is considered to be identical to the one in which the results are given.

Hope that helps.

Krambambuli
---
enjoying Mark Jason Dominus' Higher-Order Perl

In reply to Re: Date calculation: start and end of period by Krambambuli
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.