Having spent some time yesterday creating a simple web application which has some date/time stuffs in it, I realized how horrible the Unix API (and thus, PHP's API) and MySQL's date/time handling are. Conversion between month that starts from 0 and 1, year that needs to be substracted/added with 1900, these certainly have bitten (and will continue to bite) people's behind on a regular basis.

Luckily Perl 5.10 will include Time::Piece which will make localtime/gmtime become saner and more straightforward. It even includes many bonuses like leap year- and DST checking, strftime(), simple date calculation, etc.

Unfortunately we can't modify date/time elements in a Time::Piece object, e.g. $t->mon(1) (setting the month to January).

Now on to the quiz question: what would be the simplest and most straightforward way in Perl to, given a Unix timestamp t, returns two Unix timestamps t1 and t2 that are the start and end of day (or week, or month, or year) where t1 <= t <= t2.


In reply to 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.