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

Hello, Anyone know how to calculate the epoch from the following data? Year Week_of_Year Day_of_Week Basically, what I want to do is average a bunch of daily/hourly data points into a single weekly data point with a date of the data point being the monday for the week. To do that I have to determine the date for the Monday of each week of year. Any help our guidance would be helpful! Thanks in advance, John
  • Comment on Getting Epoch from year, woy and dow...

Replies are listed 'Best First'.
Re: Getting Epoch from year, woy and dow...
by dragonchild (Archbishop) on Jan 27, 2005 at 18:15 UTC
    Look at DateTime, Date::Calc, and/or Date::Manip. Those are the three standard date-handline modules. Which you pick is probably going to be personal preference.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Getting Epoch from year, woy and dow...
by Aristotle (Chancellor) on Jan 27, 2005 at 19:47 UTC

    Obligatory “DateTime rocks” post:

    Anyone know how to calculate the epoch from the following data? Year Week_of_Year Day_of_Week

    use DateTime; my $epoch = DateTime ->new( year => 2002 ) ->add( weeks => 10, days => 4 ) ->epoch;

    date of the data point being the monday for the week.

    use DateTime; my $dt = DateTime ->new( year => 2002 ) ->add( weeks => 10, days => 4 ); # ... my $epoch = $dt->truncate( to => 'week' )->epoch;

    Makeshifts last the longest.

Re: Getting Epoch from year, woy and dow...
by fglock (Vicar) on Jan 27, 2005 at 18:57 UTC

    This will print the list of epochs for all mondays in the year:

    use Date::Tie; use strict; tie my %dt, 'Date::Tie', weekday => 1, week => 1, weekyear => 2005, hour => 0, minute => 0, second => 0; while ( $dt{weekyear} == 2005 ) { print $dt{epoch}, "\n"; $dt{week}++; } 1104710400 1105315200 1105920000 1106524800 1107129600 1107734400 ...
Re: Getting Epoch from year, woy and dow...
by fglock (Vicar) on Jan 27, 2005 at 18:47 UTC
    use Date::Tie; tie my %dt, 'Date::Tie', weekday => 3, week => 5, weekyear => 2005, hour => 0, minute => 0, second => 0; print $dt{$_}, " " for qw( weekyear week weekday year month day hour epoch ); print "\n"; # 2005 05 3 2005 02 02 18 1107302400

    update: zeroed h:m:s