datastorm-open / suncalc

R package for calculating sun/moon positions and phases, linked to suncalc.js
Other
43 stars 10 forks source link

Issue with New Zealand time zone and daylight saving #8

Closed yvanrichard closed 10 months ago

yvanrichard commented 4 years ago

getSunlightTimes(date = as.Date('2021-09-26'), lat = -41.28664, lon = 174.77557, keep = c('sunrise', 'sunset'), tz='NZ')

returns

        date       lat      lon             sunrise              sunset
1 2021-09-25 -41.28664 174.7756 2021-09-26 07:05:21 2021-09-26 19:21:59

While the output date/times are for the correct day, the returned date value is not the date that is passed to the function.

bart1 commented 1 year ago

I encountered the same/similar issue:

a <- "Pacific/Auckland"
b <- "Europe/Amsterdam"
Sys.time()
#> [1] "2023-06-06 20:24:53 CEST"
library(suncalc)
d <- data.frame(date = as.Date(structure(c(
  1234354605, 1234389623, 1234516516.998, 1234525540.999,
  1234577723.001
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), lat = 42, lon = -73.9)
d
#>         date lat   lon
#> 1 2009-02-11  42 -73.9
#> 2 2009-02-11  42 -73.9
#> 3 2009-02-13  42 -73.9
#> 4 2009-02-13  42 -73.9
#> 5 2009-02-14  42 -73.9
withr::with_timezone(a, getSunlightTimes(
  data = d,
  keep = c("sunrise", "sunset")
))
#>         date lat   lon             sunrise              sunset
#> 1 2009-02-10  42 -73.9 2009-02-10 11:59:08 2009-02-10 22:23:17
#> 2 2009-02-10  42 -73.9 2009-02-10 11:59:08 2009-02-10 22:23:17
#> 3 2009-02-12  42 -73.9 2009-02-12 11:56:35 2009-02-12 22:25:52
#> 4 2009-02-12  42 -73.9 2009-02-12 11:56:35 2009-02-12 22:25:52
#> 5 2009-02-13  42 -73.9 2009-02-13 11:55:16 2009-02-13 22:27:09
withr::with_timezone(b, getSunlightTimes(
  data = d,
  keep = c("sunrise", "sunset")
))
#>         date lat   lon             sunrise              sunset
#> 1 2009-02-11  42 -73.9 2009-02-11 11:57:52 2009-02-11 22:24:35
#> 2 2009-02-11  42 -73.9 2009-02-11 11:57:52 2009-02-11 22:24:35
#> 3 2009-02-13  42 -73.9 2009-02-13 11:55:16 2009-02-13 22:27:09
#> 4 2009-02-13  42 -73.9 2009-02-13 11:55:16 2009-02-13 22:27:09
#> 5 2009-02-14  42 -73.9 2009-02-14 11:53:56 2009-02-14 22:28:26

Created on 2023-06-06 with reprex v2.0.2

bart1 commented 1 year ago

For reference I use version 0.5.1

tamsin-milla commented 1 year ago

I am encountering this same issue - has anyone found any alternatives or ways around this?

bthieurmel commented 10 months ago

Same as #2, fix (I hope) in dev/github version.