Date Manipulation: Week_Number back to Date

Discussion in 'Perl Misc' started by wls, Feb 2, 2005.

  1. wls

    wls Guest

    I've found a number of functions and expressions for taking dates and
    converting them to a week number.

    Interestingly enough, I need to go in the reverse direction.

    ( $day, $mon, $year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
    is 4-Feb-2007

    Is there such a beast, or does someone have a clever way to calculate
    it?

    Thanks,
    wls, Feb 2, 2005
    #1
    1. Advertising

  2. On 1 Feb 2005 18:20:51 -0800,
    wls <> wrote:
    > I've found a number of functions and expressions for taking dates and
    > converting them to a week number.
    >
    > Interestingly enough, I need to go in the reverse direction.
    >
    > ( $day, $mon, $year ) = ReverseTheProcess( 2007, 7 ); # Week 7 of 2007
    > is 4-Feb-2007


    Week 7 of 2007 is a range of seven days. 4 Feb 2007 is a Sunday, not a
    week.

    > Is there such a beast, or does someone have a clever way to calculate
    > it?


    The Date::Calc module has a method

    Monday_of_Week
    ($year,$month,$day) = Monday_of_Week($week,$year);

    which probably is what you want. If you need the Sunday instead of the
    Monday, also use Add_Delta_Days() or one of those.

    http://search.cpan.org/

    Martien
    --
    |
    Martien Verbruggen | Failure is not an option. It comes bundled
    | with your Microsoft product.
    |
    Martien Verbruggen, Feb 2, 2005
    #2
    1. Advertising

  3. Martien Verbruggen wrote:
    > wls wrote:
    >> I've found a number of functions and expressions for taking dates
    >> and converting them to a week number.
    >>
    >> Interestingly enough, I need to go in the reverse direction.


    <snip>

    > The Date::Calc module has a method
    >
    > Monday_of_Week
    > ($year,$month,$day) = Monday_of_Week($week,$year);


    That's probably the best way. Still, out of curiosity I gave it a try
    using my favorite module Time::Local. ;-)

    sub MondayOfWeek {
    my ($year, $week) = @_;
    require Time::Local;
    import Time::Local 'timegm';
    my $firstday = ( gmtime timegm(0,0,0,1,0,$year-1900) )[6];
    my ($d, $m, $y) = ( gmtime( ($week-1)*604800 +
    timegm(0,0,0,(1+(8-$firstday)%7),0,$year-1900) ) )[3..5];
    sprintf '%d-%02d-%02d', $y+1900, $m+1, $d
    }

    print MondayOfWeek( 2007, 7 ), "\n";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 2, 2005
    #3
  4. wls

    brian d foy Guest

    In article <>, Gunnar Hjalmarsson
    <> wrote:

    > Martien Verbruggen wrote:


    > > The Date::Calc module has a method
    > >
    > > Monday_of_Week
    > > ($year,$month,$day) = Monday_of_Week($week,$year);

    >
    > That's probably the best way. Still, out of curiosity I gave it a try
    > using my favorite module Time::Local. ;-)
    >
    > sub MondayOfWeek {


    > }



    This sort of thing makes me think there should be a big
    book of Date Recipes in <Foo>, just like we have Numerical
    Recipes in <Foo>. :)

    --
    brian d foy,
    Subscribe to The Perl Review: http://www.theperlreview.com
    brian d foy, Feb 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  2. W. eWatson
    Replies:
    17
    Views:
    497
    W. eWatson
    Aug 30, 2008
  3. Johan Allard

    Date manipulation in rails

    Johan Allard, Dec 10, 2004, in forum: Ruby
    Replies:
    1
    Views:
    106
    Tobias Luetke
    Dec 10, 2004
  4. Replies:
    2
    Views:
    84
    Josef Moellers
    Jan 4, 2006
  5. Lax
    Replies:
    7
    Views:
    114
    Anno Siegel
    Feb 7, 2006
Loading...

Share This Page