Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

week of the year

by fionbarr (Friar)
on Aug 07, 2014 at 13:46 UTC ( [id://1096623]=perlquestion: print w/replies, xml ) Need Help??

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

given a date like "080614" how can I derive week of the year = 32?

Replies are listed 'Best First'.
Re: week of the year
by choroba (Cardinal) on Aug 07, 2014 at 13:51 UTC
    Using Time::Piece:
    use Time::Piece; print 'Time::Piece'->strptime("080614", "%y%m%d")->week, "\n";

    The output is 24, not 32, so I'm not sure it's the solution you were after.

    Update: Using the %m%y%d format, you can get 33, which is almost the expected value. Maybe you are not using the ISO 8601 method to count the weeks? (At least the format of the date hints you don't.)

    The week number may be an unknown concept to some readers. The ISO 8601 standard defines that weeks begin on a Monday and week 1 of the year is the week that includes both January 4th and the first Thursday of the year. In other words, if the first Monday of January is the 2nd, 3rd, or 4th, the preceding days of the January are part of the last week of the preceding year. Week numbers range from 1 to 53.

    Using %m%d%y seems to return 32.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I'm going to go ahead and assume that you meant today ;)
      utilitarian@busybox ~/$ perl -MTime::Piece -E 'say Time::Piece->strpti +me("080614", "%m%d%y")->week'; 32

      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

        Hm, but in my timezone, today is 140807 ;]. Of course I would never put the d between the m and the y. Ever.


        Enjoy, Have FUN! H.Merijn
      "%y%m%d"

      Should be "%d%m%y"?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: week of the year
by SBECK (Chaplain) on Aug 07, 2014 at 14:43 UTC
    Date::Manip will do that (but note that it will parse your string as 2008-06-14, so you'll need to reformat it to get what you want):
    use Date::Manip; $d = ParseDate("2014-08-06"); print UnixDate($d,"%W");
    will give 32.
Re: week of the year
by GotToBTru (Prior) on Aug 07, 2014 at 14:15 UTC

    Considering the OP expects the answer 32, the only logical interpretation of the format is %m%d%y.

    use Time::Piece; print 'Time::Piece'->strptime("080614", "%m%d%y")->week, "\n";

    Output:

    32
    1 Peter 4:10
Re: week of the year
by AnomalousMonk (Archbishop) on Aug 07, 2014 at 13:54 UTC

    What the heck is the format of a date like "080614"? Offhand, I can posit six possibilities, and knowing the vagaries of calendrical notation(s), I'm sure there are many more.

      Feeding the trolls ...

      $ perl -lE'say scalar localtime "080614"' Thu Jan 1 23:23:34 1970

      Enjoy, Have FUN! H.Merijn
Re: week of the year
by ww (Archbishop) on Aug 07, 2014 at 14:13 UTC

    Super Search or most search engines would have provided your answer for minimal effort... and more quickly than by posting a lazy-in-the-extreme question. For a change, try searching and/or reading the documents, tutorials, help, etc.

    Lazy is a virtue when applied to problem cases like 'reinventing the wheel,; it is NOT a valid substitute for RTFM.

    You've been advised of this previously, so - - for laziness (in a definitely non-virtuous sense).

    Updated added para 1,sentence 1 re the ready availability of an answer both here and on the web, more generally.


    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
      I perhaps should have said 'favorite' way or 'most efficient' way. I did searches on SuperSearch, Stackoverflow and Google and have a good solution. I know I've 'been advised' previously and I assure you I do prior research but as we know there is more than one way to do it and I am continually impressed by the answers I receive from Perl Monks. As an aside, I think if it's worth answering my query, it's worth actually answering the query not castigating for being 'lazy'. Thanks to all who replied with solutions.
        re "I think if it's worth answering my query, it's worth actually answering the query not castigating for being 'lazy'."

        Actually, no... or, at least, I don't agree. You post SOPW like the OP in this thread with great regularity and with no indication that you've tried to help youself, other than unsupported claims like those in this node's direct parent.

        As I see it, if you're allowed, unchallenged, to post lazy questions, you'll continue and others will follow suit.

        OTOH, if challenged, perhaps you'll change your shiftless habits...
        and perhaps those who undermine the value of the Monastery by offering free-code rather than free-help-with-learning will reconsider before again satisfying your 'gim'més'.

        Downvoted.


        Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
        1. code
        2. verbatim error and/or warning messages
        3. a coherent explanation of what "doesn't work actually means.

        What adds to a post looking "lazy" is it being one line, unformatted, without further explanation or code. Better would have been something like this, nicely formatted with <p> and <code> tags: "I am currently using the following code to find the week number from a given data, code here. I've seen some other ways to do it, but I was wondering if you have any recommendations?" (See also How do I post a question effectively?)

Re: week of the year
by Ratazong (Monsignor) on Aug 08, 2014 at 06:51 UTC

    TIMTOWTDI: Using my favourite date-related module Date::Calc

    use Date::Calc qw(Week_of_Year); $date =~ /(\d\d)(\d\d)(\d\d)/; ($week,$year) = Week_of_Year(2000+$3,$1,$2);
    Please look at the documentation of the module why returning the year makes sense...

    HTH, Rata

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1096623]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found