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

I have a script that I've been writing that automates weekly data retrieval. I've been using Date::Calc's Week_of_Year function to determine that it's time to retreive data. My problem is that the data's week ends on Saturday but Week_of_Year ends on Sunday, therefore if data are recieved on Sunday it continues looking for data even though the data have already been retrieved.
Earlier today I asked this question in the cb, but unfortunately the suggested "%U" in strftime does not work the same as Week_of_Year.
print strftime("WEEK Number%n%U%n%nYear%n%Y", 1,1,1,31,11,103) WEEK Number 52 Year 2003 printf "Week Number\n%d\n\nYear\n%d", Week_of_Year(2003,12,31) Week Number 1 Year 2004
Does anyone know how to use an offset or have any suggestions for a work around?

Sweetblood

Replies are listed 'Best First'.
Re: Date::Calc Week_of_Year offset
by Zaxo (Archbishop) on Oct 21, 2004 at 02:20 UTC

    I think that Date::Calc's answer is distinctly odd. POSIX::strftime() has three distinct WoY formats, %U, %V, and %W, none of which behave as does Date::Calc::Week_of_Year().

    December 31, 2003 was a Wednesday, so that Sunday-based week had four days in 2003, and by most standards should be counted as a 2003 week. Except that Date::Calc uses, as you say, a Monday based week, which flips the week to 2004. That date is an edge case.

    The manpage man 3 strftime documents what the formats present, and perldoc Date::Calc documents the rules its Week_of_Year() function follows.

    Is there any reason you can't set this up as a weekly cron job? I don't see why you need the week number, unless the data you retrieve is labelled that way. If so, you should look at docs or source of whatever produces the data to see if any of these functions match what it does in all cases.

    After Compline,
    Zaxo

Re: Date::Calc Week_of_Year offset
by neilh (Pilgrim) on Oct 21, 2004 at 02:03 UTC
    I'm not sure about strftime, but in the Unix date programme, %V is the Week of the Year with Monday as the first day. It could be the same.

    Neil

      Yes you're right, It would be "%V" for a Sunday start of week. The cb suggestion was to use "%U" but I believe they meant "%V" but this is not really the problem using strftime really the problem is partial weeks such as 12/31/2003.

      Thanks!

      Sweetblood

        Once again comparing to Unix date, the partial week at the end of the year is defined as week number 0.

        Neil

Re: Date::Calc Week_of_Year offset
by TedPride (Priest) on Oct 21, 2004 at 04:29 UTC
    Pardon me for asking stupid questions, but why not just do something like:
    use strict; use warnings; my $dir = "/whatever"; my $time = time(); my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time); my $btime = $time - $wday * 86400 - $hour * 3600 - $min * 60 - $sec; if (!-e "$dir/$btime.mrk") { # process all records timestamped ($btime - 604799) to $btime # create file "$dir/$btime.mrk" }
    This will process all records from the past week, assuming the server time is set correctly. If the server time is not set correctly, it will still process the records weekly, but the process won't run on the first access Sunday morning.
Re: Date::Calc Week_of_Year offset
by chanio (Priest) on Oct 22, 2004 at 05:04 UTC
Re: Date::Calc Week_of_Year offset
by fglock (Vicar) on Oct 23, 2004 at 16:45 UTC

    You could use an ICal expression:

    use strict; use DateTime::Format::ICal; my $s = DateTime::Format::ICal->parse_recurrence( recurrence => "freq=yearly;byweekno=10,20,30;wkst=SU" ); my $dt = DateTime->today; print "Next occurrence: ", $s->next( $dt )->datetime, "\n"; # Next occurrence: 2005-03-11T00:00:00