Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong calculation of Isoweek for year 2015 (in tz != "UTC") #2407

Closed
MoebiusAV opened this issue Oct 10, 2017 · 3 comments
Closed

Wrong calculation of Isoweek for year 2015 (in tz != "UTC") #2407

MoebiusAV opened this issue Oct 10, 2017 · 3 comments
Milestone

Comments

@MoebiusAV
Copy link

@MoebiusAV MoebiusAV commented Oct 10, 2017

As posted in https://stackoverflow.com/questions/43944430/r-why-does-the-default-package-for-isoweek-change-depending-how-i-use-it/46655291#46655291 data.table isoweek function sometimes gives a different result of ISO standart.

I found an explination, running data.table function step by stepThe first step of function is converting Date as.POSIXlt.
The result will be different if you input a character or a Date
as.POSIXlt('2015-01-02')
[1] "2015-01-02 COT"

as.POSIXlt(as.Date('2015-01-02'))
[1] "2015-01-02 UTC"

As you can see, what change is the time Zone.
In the third step of function, in both cases, it returns a Date in Local time
(year_start <- as.POSIXct(paste0(as.POSIXlt(nearest_thurs)$year +1900L, "-01-01")))
[1] "2015-01-01 COT"
That way, in the forth and last step, the date difference between UTC and Local time gives some decimals 'aditional' to time difference between Local Time dates (in my case 5 hours, or 0.208). That is why the final result of weeks changes.

In conclusion, if you force date as character, the function will calculate everything in Local time and the result will be right
-- | --

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Oct 13, 2017

Following up on my SO comment -- without a reproducible example, I can't confirm this works, but it seems the answer is to change:

https://github.com/Rdatatable/data.table/blob/master/R/IDateTime.R#L275

xlt <- as.POSIXlt(x)

To

xlt <- as.POSIXlt(x, tz = 'UTC')

(that's what is seems is done by lubridate::isoweek -> lubridate::make_datetime):

# lubridate::isoweek
#  c/f https://github.com/tidyverse/lubridate/blob/e4ad2c3024c4e114c5f046c7fa0d73e2168f0d2b/R/accessors-week.r#L36
#  ** on CRAN, lubridate::isoweek is what now corresponds to
#      lubridate:::.other_week(x, 1)
x <- as.POSIXlt(x)
# lubridate::make_datetime
#   c/f https://github.com/tidyverse/lubridate/blob/c10c8cfff6a8d86e58c5865c1fb1c93463be647e/R/instants.r#L109
.POSIXct(numeric(), tz = tz) # default tz = 'UTC'

(actually, on further look, these look the same. leaving this for my own notes)

@SimonCoulombe
Copy link

@SimonCoulombe SimonCoulombe commented Oct 13, 2017

data.table::isoweek("2014-12-29")
[1] 1

data.table::isoweek(as.Date("2014-12-29"))
[1] 0

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Oct 14, 2017

@SimonCoulombe thanks! I've spotted the problem and will push a fix soon.

@mattdowle mattdowle added this to the v1.10.6 milestone Oct 14, 2017
@mattdowle mattdowle closed this in 47e6958 Oct 14, 2017
mattdowle added a commit that referenced this issue Nov 23, 2017
mattdowle added a commit that referenced this issue Nov 23, 2017
mattdowle added a commit that referenced this issue Nov 23, 2017
Strengthened test() to check type when comparing atomic vectors, too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants