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

Dates in Sunrise and Sunset results not always same as "date = " parameter #2

Open
paulr-bv opened this issue Nov 27, 2018 · 8 comments

Comments

@paulr-bv
Copy link

Hi, thanks for a great package. However, I'm wondering if I've found a bug with (at least) Sunrise and Sunset times?

It appears that when a timezone being checked is behind UTC, the "date" component of Sunrise and Sunset is the "day before" the date being checked.

Examples are below:

# GOOD
# London. In UTC. Dates for sunrise and sunset same as "date = " paramater
site_lat <- 51.50507
site_lon <- -0.11299
site_tz <- "UTC"
getSunlightTimes(date = ymd("2018-11-01"), lat = site_lat, lon = site_lon, tz = site_tz, keep = c("sunrise", "sunset"))
                 date      lat      lon             sunrise              sunset
1 2018-11-01 12:00:00 51.50507 -0.11299 2018-11-01 06:54:48 2018-11-01 16:35:45


# GOOD
# Sydney. Ahead of UTC. Dates for sunrise and sunset same as "date = " paramater
site_lat <- -33.8671417236
site_lon <- 151.2071075439
site_tz <- "Australia/Sydney"
getSunlightTimes(date = ymd("2018-11-01"), lat = site_lat, lon = site_lon, tz = site_tz, keep = c("sunrise", "sunset"))
                 date       lat      lon             sunrise              sunset
1 2018-11-01 12:00:00 -33.86714 151.2071 2018-11-01 05:56:29 2018-11-01 19:23:30


# BAD?
# Los Angles. Behind UTC. Dates for sunrise and sunset are DAY BEFORE the "date = " paramater
site_lat <- 34.052235
site_lon <- -118.243683
site_tz <- "US/Pacific"
getSunlightTimes(date = ymd("2018-11-01"), lat = site_lat, lon = site_lon, tz = site_tz, keep = c("sunrise", "sunset"))   
                 date      lat       lon             sunrise              sunset
1 2018-11-01 12:00:00 34.05224 -118.2437 2018-10-31 07:12:57 2018-10-31 18:02:40
@bthieurmel
Copy link
Contributor

Hi,

Which version are you using ? (sessionInfo()). I think this bug was fixed since 0.3 :

site_lat <- 34.052235
site_lon <- -118.243683
site_tz <- "US/Pacific"
getSunlightTimes(date = ymd("2018-11-01"), lat = site_lat, lon = site_lon, tz = site_tz, keep = c("sunrise", "sunset"))   

                 date      lat       lon             sunrise              sunset
1 2018-11-01 12:00:00 34.05224 -118.2437 2018-11-01 07:13:51 2018-11-01 18:01:43

@paulr-bv
Copy link
Author

paulr-bv commented Mar 7, 2019 via email

@tomauer
Copy link

tomauer commented Apr 23, 2019

I am having the same problem, but for locations ahead of UTC, it's giving me the solar times for the next day.

site_lat <- -27.53103
site_lng <- 152.8666
site_tz <- "Australia/Brisbane"
getSunlightTimes(date = as.Date("2015-12-17"), lat = site_lat, lon = site_lng, tz = site_tz, keep = c("solarNoon"))`
                 date       lat      lon           solarNoon
1 2015-12-17 12:00:00 -27.53103 152.8666 2015-12-18 11:46:04

I am using version 0.4.

mstrimas added a commit to mstrimas/suncalc that referenced this issue Mar 20, 2023
Process datetimes in UTC rather than system time zone to ensure returned date times are not offset by 1 day in `getSunlightTimes()`. Addresses the issue reported at datastorm-open#2
@mstrimas
Copy link

I believe this is happening because date times are being converted to the users local time zone prior to calling .getTimes(). For example, calculating solarNoon for a series of locations along the equator at various longitudes using my normal time zone ("America/Los_Angeles"), the 3 most eastern locations are a day ahead of the input date.

library(lutz)
library(suncalc)
library(tidyverse)

# default system time zone
Sys.timezone()
#> "America/Los_Angeles"

# example locations along the equator at various longitudes
data <- data.frame(date = as.Date("2023-06-01"), 
                   lat = 0, 
                   lon = seq(-150, 150, by = 25)) %>% 
  mutate(tz = tz_lookup_coords(lat = lat, lon = lon,
                               method = "accurate", warn = FALSE)) 

# solar noon
data %>% 
  mutate(solar_noon = pmap(list(date = date, lat = lat, lon = lon, tz = tz),
                           getSunlightTimes,
                           keep = "solarNoon") %>% 
           map("solarNoon") %>% 
           map_chr(as.character))

#          date lat  lon                tz          solar_noon
# 1  2023-06-01   0 -150        Etc/GMT+10 2023-06-01 11:59:17
# 2  2023-06-01   0 -125         Etc/GMT+8 2023-06-01 12:19:16
# 3  2023-06-01   0 -100         Etc/GMT+7 2023-06-01 11:39:16
# 4  2023-06-01   0  -75    America/Bogota 2023-06-01 11:59:15
# 5  2023-06-01   0  -50     America/Belem 2023-06-01 12:19:15
# 6  2023-06-01   0  -25         Etc/GMT+2 2023-06-01 11:39:14
# 7  2023-06-01   0    0           Etc/GMT 2023-06-01 11:59:13
# 8  2023-06-01   0   25 Africa/Lubumbashi 2023-06-01 12:19:13
# 9  2023-06-01   0   50         Etc/GMT-3 2023-06-01 11:39:12
# 10 2023-06-01   0   75         Etc/GMT-5 2023-06-01 11:59:12
# 11 2023-06-01   0  100      Asia/Jakarta 2023-06-02 12:19:20
# 12 2023-06-01   0  125     Asia/Makassar 2023-06-02 11:39:19
# 13 2023-06-01   0  150        Etc/GMT-10 2023-06-02 11:59:19

But if I manually change my computer's timezone to "Asia/Seoul", I end up with the 5 westernmost locations being a day behind the input date.

# system time zone after manually changing it to "Asia/Seoul"
Sys.timezone()
#> "Asia/Seoul"

# solar noon
data %>% 
  mutate(solar_noon = pmap(list(date = date, lat = lat, lon = lon, tz = tz),
                           getSunlightTimes,
                           keep = "solarNoon") %>% 
           map("solarNoon") %>% 
           map_chr(as.character))
data.frame(date = as.Date("2023-06-01"), 
           lat = 0, lon = seq(-150, 150, by = 25)) %>% 
  mutate(tz = tz_lookup_coords(lat = lat, lon = lon,
                               method = "accurate", warn = FALSE)) %>% 
  mutate(solar_noon = pmap(list(date = date, lat = lat, lon = lon, tz = tz),
                           getSunlightTimes,
                           keep = "solarNoon") %>% 
           map("solarNoon") %>% 
           map_chr(as.character))

#          date lat  lon                tz          solar_noon
# 1  2023-06-01   0 -150        Etc/GMT+10 2023-05-31 11:59:08
# 2  2023-06-01   0 -125         Etc/GMT+8 2023-05-31 12:19:08
# 3  2023-06-01   0 -100         Etc/GMT+7 2023-05-31 11:39:07
# 4  2023-06-01   0  -75    America/Bogota 2023-05-31 11:59:06
# 5  2023-06-01   0  -50     America/Belem 2023-05-31 12:19:06
# 6  2023-06-01   0  -25         Etc/GMT+2 2023-06-01 11:39:14
# 7  2023-06-01   0    0           Etc/GMT 2023-06-01 11:59:13
# 8  2023-06-01   0   25 Africa/Lubumbashi 2023-06-01 12:19:13
# 9  2023-06-01   0   50         Etc/GMT-3 2023-06-01 11:39:12
# 10 2023-06-01   0   75         Etc/GMT-5 2023-06-01 11:59:12
# 11 2023-06-01   0  100      Asia/Jakarta 2023-06-01 12:19:11
# 12 2023-06-01   0  125     Asia/Makassar 2023-06-01 11:39:10
# 13 2023-06-01   0  150        Etc/GMT-10 2023-06-01 11:59:10

If I modify this line https://github.com/mstrimas/suncalc/blob/master/R/getSunlightTimes.R#L85 to replace Sys.timezone() with "UTC", this example returns the correct date regardless of the system time zone. I'll submit a PR with that change.

@fegue
Copy link

fegue commented Aug 4, 2023

This is still a problem. Any plans to merge the pull request?
Love your package otherwise!

bthieurmel pushed a commit that referenced this issue Nov 7, 2023
@bthieurmel
Copy link
Contributor

New version available on github, and coming soon on CRAN.
If you want to test.

@mstrimas
Copy link

mstrimas commented Nov 8, 2023

Looks great, thanks for the fix!

@jpatt2
Copy link

jpatt2 commented Mar 18, 2024

I am still having an issue with timezone being incorrect when using getmoontimes function from suncalc package. Others have used my same code and same df but with different (correct) results, so I can't figure out what's going on. Here is an example of what happens. Notice the days reported back aren't the same as the day I am asking for. I've tried all different code and df's, time zones, etc. Nothing is working which leads me to believe it's something in the package and with my macbook.

image001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants