Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Calculating latest 'Sunday' date on the first of the month

by Cicatrix (Novice)
on Sep 02, 2010 at 04:32 UTC ( #858481=perlquestion: print w/ replies, xml ) Need Help??
Cicatrix has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am in the process of writing my first Perl script. in which i am facing a problem on calculating the latest/recent Sunday's date on the first day of the month. Ex: for the current week, On 01/09/2010(yesterday) i was not able to calculate the last Sunday's date, i.e., 29/08/2010. I need help from you people to complete on my Perl script. Thank you

Comment on Calculating latest 'Sunday' date on the first of the month
Re: Calculating latest 'Sunday' date on the first of the month
by ikegami (Pope) on Sep 02, 2010 at 04:42 UTC

    Your question is unclear. I think you're asking for the last Sunday before the first day of the current month.

    use DateTime qw( ); my $dt = DateTime->today(); $dt->set_day(1); $dt->subtract( days => $dt->day_of_week() ); print($dt->ymd(), "\n");

    DateTime

      thanks ikegami.. its working absolutely fine...
      hey that code is again causing problem now... instead of calculating starting of this week its calculating last Sunday i.e, 29/08/2010 as the starting of the week. help!!

        Cicatrix:

        The code ikegami gave you, and the result you reported are what I'd expect from the specifications you gave in your original post. So if you're getting something incorrect, you need to be more clear in specifying the problem.

        Also, you report the problem several days after accepting the answer provided. I suspect that you didn't adequately test the code before reporting success. Normally, you want to test code against all the different types of input cases that will be encountered. So rather than setting $d = DateTime->today() only, you'd initialize $d to various dates, and compare the results of the code with the date you'd expect.

        I suggest you try Test::More and related packages (if you don't use them now), and use them to build tests for code before accepting it. You'll find that the tests, if written well, can also serve as documentation for your code, as it will show future programmers what behaviour to expect for the unusual cases.

        ...roboticus

        I've found your original post very unclear. (It's filled with grammar errors for starters.) I specified what I thought you meant and I provided a solution for that. If you want something else, specify what you want.
Re: Calculating latest 'Sunday' date on the first of the month
by GrandFather (Cardinal) on Sep 02, 2010 at 04:45 UTC

    We'd like to see what you have tried so we can provide better help. Otherwise, you may like to take a look at Date::Manip (the kitchen sink module for date manipulation).

    True laziness is hard work
Re: Calculating latest 'Sunday' date on the first of the month
by dasgar (Deacon) on Sep 02, 2010 at 04:45 UTC
Re: Calculating latest 'Sunday' date on the first of the month
by Anonymous Monk on Sep 02, 2010 at 06:25 UTC
      Date::Manip can do that easily. If you want all mondays from today to the end of the year, and $y contains the current year:
      @dates = ParseRecur('0:0:1*1:0:0:0','','today',"$y-12-31");
      

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://858481]
Approved by mr_mischief
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls