Skip to content

getting ISO week

9 messages · Brian Ripley, Gustaf Rydevik, Hans W Borchers +1 more

#
Hi all,

Is there a simple function already implemented for getting the ISO
weeks of a Date object?
I couldn't find one, and so wrote my own function to do it, but would
appreciate a pointer to the "default" way. If a function is not yet
implemented, could the code below be of interest to submit to CRAN?

Best Regards,

Gustaf

--------------------

getweek<-function(Y,M=NULL,D=NULL){

  if(!class(Y)[1]%in%c("Date","POSIXt")) {
  date.posix<-strptime(paste(c(Y,M,D),collapse="-"),"%Y-%m-%d")
  }
  if(class(Y)[1]%in%c("POSIXt","Date")){
date.posix<-as.POSIXlt(Y)
Y<-as.numeric(format(date.posix,"%Y"))
M<-as.numeric(format(date.posix,"%m"))
D<-as.numeric(format(date.posix,"%d"))
  }


  LY<- (Y%%4==0 & !(Y%%100==0))|(Y%%400==0)
  LY.prev<- ((Y-1)%%4==0 & !((Y-1)%%100==0))|((Y-1)%%400==0)
  date.yday<-date.posix$yday+1
  jan1.wday<-strptime(paste(Y,"01-01",sep="-"),"%Y-%m-%d")$wday
  jan1.wday<-ifelse(jan1.wday==0,7,jan1.wday)
  date.wday<-date.posix$wday
  date.wday<-ifelse(date.wday==0,7,date.wday)


  ####If the date is in the beginning, or end of the year,
  ### does it fall into a week of the previous or next year?
  Yn<-ifelse(date.yday<=(8-jan1.wday)&jan1.wday>4,Y-1,Y)
  Yn<-ifelse(Yn==Y&((365+LY-date.yday)<(4-date.wday)),Y+1,Y)

  ##Set the week differently if the date is in the beginning,middle or
end of the year

  Wn<-ifelse(
      Yn==Y-1,
      ifelse((jan1.wday==5|(jan1.wday==6 &LY.prev)),53,52),
      ifelse(Yn==Y+1,1,(date.yday+(7-date.wday)+(jan1.wday-1))/7-(jan1.wday>4))
      )
    return(list(Year=Yn,ISOWeek=Wn))
}
#
strftime(x, "%V")

E.g.

strftime(as.POSIXlt(Sys.Date()), "%V")

is "50", and you might want as.numeric() on it.

Note that this is OS-dependent, and AFAIR Windows does not have it.
On Thu, 11 Dec 2008, Gustaf Rydevik wrote:

            

  
    
#
A slightly simpler version is

format(Sys.Date(), "%V")
On Thu, 11 Dec 2008, Prof Brian Ripley wrote:

            

  
    
#
format(d, "%U") and format(d, "%W") give week numbers using
different conventions.  See ?strptime

On Thu, Dec 11, 2008 at 7:43 AM, Gustaf Rydevik
<gustaf.rydevik at gmail.com> wrote:
#
On Thu, Dec 11, 2008 at 2:10 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
-----

On Thu, Dec 11, 2008 at 2:15 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
Thank you both for your replies!

I'm on windows, so prof Ripleys solution does not work (why is this
OS-dependent?).
Regarding Gabor's solution, neither convention follow the ISO 8601
standard, which is used in Europe (and Sweden in particular). See
http://en.wikipedia.org/wiki/ISO_8601#Week_dates .
So it seems that my function does fill a hole, however small....

I know that for me, working with week numbers, which are used quite
heavily in Sweden, have always been a major frustration.
Would it be possible to implement something similar to my solution in
base, and how should I go about making it fit in to the rest of the
date functions?

/Gustaf
#
Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:
Gabor,

the results of format(aDate, "W") appear to be incorrect anyway, see:

    format(as.Date("2008-01-01"), "%W")     #-> "00"

There is never a week 0, this should be week 1.

    format(Sys.Date(), "%W")                #-> "49"

but my business calendar says today's (Dec. 11, 2008) week is week 50
which is what Brian Ripleys proposed 'strftime(x, "%V")' returns.

There could be a format "%E" (not used up to now) for returning a
correct week number according to the European standard.

Yours,  Hans Werner
#
According to the definition in ?strptime (which is not the same as the
ISO definition):

format(x, "%W") returns

"Week of the year as decimal number (00?53) using Monday as the first
day of week (and typically with the first Monday of the year as day 1
of week 1). The UK convention."

The first day of 2008 is a Tuesday which means that 2008 starts in week 0.
On Thu, Dec 11, 2008 at 2:31 PM, Hans W. Borchers <hwborchers at gmail.com> wrote:
#
Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:
Yes I read that but it is still misleading and -- I think -- incorrect. 
See <www.dateandtime.org/calendar> to find out that this is week 50 even 
in the UK.
We would have had a lot of misplaced business meetings in our company if 
the week numbers in Great Britain, Germany, and Sweden would actually be 
different.

Hans Werner
#
Perhaps you mean is that the definition ought be otherwise but
at least according to one standard the definition is correct:

http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html
On Thu, Dec 11, 2008 at 3:01 PM, Hans W. Borchers <hwborchers at gmail.com> wrote: