sylph001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

Recently I'm writing a small perl program and I find out that for some certain day-of-the-month numbers, I need to count out the corresponding day-of-the-week numbers.

I could write a routine to do that work but turns out it will be complicated.

I think there must be some precedences done for doing this work.

I guess there might be some useful functions in modules like Local::Time or so, but I feel not familiar with the wider industry and standard way that most people do it with.

So, could you kindly lead me to the best/most-common way to do this date calculation?

 

Many thanks

</body> </html>

Replies are listed 'Best First'.
Re: Way to calculate day-of-the-week
by hippo (Archbishop) on Dec 24, 2014 at 11:11 UTC

    "best" is subjective of course. Ordinarily I would use Time::Piece as it is core. It comes with the day_of_week method which does what you intend and uses strptime for date parsing. However, this task is very much TIMTOWTDI. There are a plethora of date and time parsing/manipulation modules and it pays to be at least passingly familiar with the most popular ones.

Re: Way to calculate day-of-the-week
by blindluke (Hermit) on Dec 24, 2014 at 09:20 UTC

    Hello.

    There is a great module (meta-module) called Task::Kensho, that groups tried things that people use for common purposes under one umbrella. Look at the Dates section for modules related to Dates.

    I would recommend DateTime, with the following usage (from module synopsis):

    use DateTime; $dt = DateTime->new( year => 1964, month => 10, day => 16, ); $dow = $dt->day_of_week; # 1-7 (Monday is 1)

    - Luke

      I have never had a time related problem that I could not do with Date::Time. It probably is always overkill, but it gets the job done. Why look further?
      Bill

        There are some reasons you won't find Date::Time under the Task::Kensho modules.

        For one, look at this review of the module you mentioned. Or the fact that the module synopsis still starts with the line:

        This is just some suggestions, as nothing is implemented yet. I'm open to critisism.

        The fact that you 'never had a time related problem' that you could not do with one particular tool is still not enough to be sure that there is no reason to look further into other tools. Even curiosity is reason enough to look further.

        As always, TIMTOWTDI.

        - Luke

Re: Way to calculate day-of-the-week
by Anonymous Monk on Dec 25, 2014 at 00:04 UTC
Re: Way to calculate day-of-the-week
by clueless newbie (Curate) on Dec 24, 2014 at 13:03 UTC
      Please, please, please don't encourage people to do this when there are perfectly good, well-maintained, well-tested date/time modules available to do the task. People copy-pasting or re-inventing their own date/time routines is an incredibly common source of bugs and maintenance nightmares.

        Y’see?   If you had logged-in, you would have accumulated 17 experience-points and counting.)

        This is fundamentally excellent advice.   If you “roll your own” solution to [any ...] “well-known Thing That Has Already Been Done™,” merely to avoid installing and useing a well-known CPAN module that does the entire task, then you have imposed an unnecessary maintenance burden upon the future maintainers of your code.   (“Goody for you that you now work at Google, but I’m still here ...”)   If you had used the CPAN tool, then your successors would not have to dumpster-dive into that piece of logic to be sure that you did it right to discover the subtle bug in your one-of-a-kind code.   Instead, they could have relied upon the self-tests that the CPAN module includes, knowing that the module would not have been installed at all if any of those self-tests had failed.

        Also:   if you are maintaining a legacy code-base that did these things in a particular way, e.g. using a CPAN module that you really don’t like but that does do the job correctly, please “keep using that module.”   Don’t unnecessarily introduce “two sources of Truth.”