in reply to Re: Is there an easy way to get the start date of the current week?
in thread Is there an easy way to get the start date of the current week?

This can break in two ways:

  1. In a locale where "Sun" is not Sunday
  2. If Monday is the first day of the week, in which case you will have to go forward (for the question asked). You've chosen your sub name to be clear that you're going backward though.

Enjoy, Have FUN! H.Merijn
  • Comment on Re^2: Is there an easy way to get the start date of the current week?

Replies are listed 'Best First'.
Re^3: Is there an easy way to get the start date of the current week?
by james2vegas (Chaplain) on Aug 22, 2010 at 07:26 UTC
    The scalar value of localtime is explicitly not locale dependent. It is a fixed format, see ctime(3)
      perlfunc also mentions this
      This scalar value is not locale dependent but is a Perl builtin. ... To get somewhat similar but locale dependent date strings, set up your locale environment variables appropriately ... use POSIX qw(strftime);
        That's a curious way of phrasing it. It seems to suggest that anything that's a Perl builtin isn't locale dependent. But grepping through the perlfunc manual page does reveal builtins that have a locale dependency: lc, lcfirst, (s)printf, sort, uc, ucfirst. And then there are regular expressions.

      I stand corrected.

      I however probably still wouldn't rely on it as there are probably way too many other functions that /do/ depend on the locale, or suddenly behave different if POSIX comes into play (or when it explicitely does not), or when modules happen to be in place that overrule this default.


      Enjoy, Have FUN! H.Merijn
Re^3: Is there an easy way to get the start date of the current week?
by BrowserUk (Patriarch) on Aug 22, 2010 at 06:54 UTC
    In a locale where "Sun" is not Sunday

    But then the routine would not be called lastSunday() either...

      Not to be arguing, but what /would/ it be called then, when using the script in a multi-langual environment, or when you do not know what the locale will be at execution time?

      I really like the simplicity of the approach. I always want to answer the stupid question "How do I print yesterday in perl?" with 'print "yesterday"', which is a perfect answer, but obviously not what the question was about. I've hit this locale problem way too often to not care, and if you'd use a regex match, the easy way out is to use { local $ENV{LC_TIME} = "en_US"; ... }.


      Enjoy, Have FUN! H.Merijn

        How about:

        sub firstDayOfWeek { my $now = time; my @now = localtime $now; return $now - ( $now[ 6 ] * 2 - 1 ) * 43200; }