Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Sure - but the Mondays we're interested in would probably need to be in local timezone and DST, not GMT. I'm not really sure the code would fail, but I wouldn't bet I get the correct list of dates regardless when the calculation is done either
Turns out they have Mondays in GMT too. :)

As long as the parsing of the date occurs in GMT and the output of the date happens in GMT, there will be no issues.

I'm not sure the back-and-forth between localtime and gmtime works OK
Back and forth between localtime and gmtime will always bite you. Use DateTime if you are doing timezone conversions. However, this problem domain doesn't involve timezone conversions. If the date is parsed using timegm then output using gmtime, there is no issue. It always works.

Date calculation is not as trivial as people say (or possibly as trivial as my original post says). Date calculation is also not as complicated as other people say. In the case asked for by the OP, using timegm and gmtime as reciprocal functions works just fine and works for date/times in all timezones. The key on this issue is that if you use gmtime, you must parse using timegm.

To make the issue matter even less in this case the OP is asking for dates - not times. Because we don't care about times - I could've used timelocal/localtime as long as my initial time was normalized to noon because there is no DST timeshift that spans more than 2 hours. I would've used localtime but purists really would've put up a fight then (but it also would've made my code longer :( ).

In my original code post, I actually don't include any date parsing. Date parsing/converting to time is almost trivial - but not really (you should use DateTime unless you know what you're doing - and maybe even if you do). My code as posted will still give the Mondays till the end of the year. What may go wrong is that because it is based on NOW GMT, it may exclude or included one extra Monday at the beginning of the list based on which Timezone you are in. If you didn't know that by looking at the code then you should use DateTime (there is a theme here - use DateTime).

Another thing to remember is that epoch time is the same everywhere in the world at the same time because it is based on UTC. So doing integer arithmetic on an operation like the one given is just fine and won't have any issues.

There are those that will argue that you should always worry about leap seconds and DST - and if you use DateTime you can let it worry about it for you. But if you have a problem domain that doesn't require worrying about those things, you have more options open for solving the problem. Figuring out when or when not to use DateTime is generally easy to do with experience. However, it is almost always easier just to use DateTime.

my @a=qw(random brilliant braindead); print $a[rand(@a)];

In reply to Re^5: What is the best way to get a list of all Mondays until the end of the year? by Rhandom
in thread What is the best way to get a list of all Mondays until the end of the year? by wenD

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2018-04-25 09:36 GMT
    Find Nodes?
      Voting Booth?