gagolews / stringi

Fast and portable character string processing in R (with the Unicode ICU)
https://stringi.gagolewski.com/
Other
304 stars 44 forks source link

WeekOfMonth extraction from stri_datetime_fields() #350

Closed Kaketo closed 5 years ago

Kaketo commented 5 years ago

I have a little problem with function stri_datetime_fields(). I need to extract week of the month from date . The problem is that when the month starts during weekend (Friday-Sunday) the week iterator starts on 0 and in other situations it starts on 1. Is this function supposed to work like this?

Example code where I'm having a problem:

for(i in c(1:9)){
  end_date <- paste('2019-0',i,'-01', sep='')
  stri_date <- stri_datetime_parse(end_date, format = 'yyyy-MM-dd')
  print(paste('Date: ',end_date,
              ' Week of month: ',stri_datetime_fields(stri_date, tz = 'Europe/Warsaw', locale = 'pl')$WeekOfMonth,
              ' Day of week: ',stri_datetime_fields(stri_date, tz = 'Europe/Warsaw', locale = 'pl')$DayOfWeek,
              sep=''))
}

Output is:

[1] "Date: 2019-01-01 Week of month: 1 Day of week: 3"
[1] "Date: 2019-02-01 Week of month: 0 Day of week: 6"
[1] "Date: 2019-03-01 Week of month: 0 Day of week: 6"
[1] "Date: 2019-04-01 Week of month: 1 Day of week: 2"
[1] "Date: 2019-05-01 Week of month: 1 Day of week: 4"
[1] "Date: 2019-06-01 Week of month: 0 Day of week: 7"
[1] "Date: 2019-07-01 Week of month: 1 Day of week: 2"
[1] "Date: 2019-08-01 Week of month: 1 Day of week: 5"
[1] "Date: 2019-09-01 Week of month: 0 Day of week: 1"
gagolews commented 5 years ago

That's the behavior imposed by the ICU library. I guess that's a feature, not a bug.