Get The Sunrise And Sunset Times For A Specific Location In Rust Including Daylight Saving Time
January 2025
---
chrono = "0.4.39"
chrono-tz = "0.10.0"
lazy_static = "1.5.0"
sunrise = "1.0.1"
tzf-rs = "0.4.9"
---
use DateTime;
use Tz;
use lazy_static;
use sunrise_sunset;
use DefaultFinder;
lazy_static!
Output:
Daylight Time Sunrise Example: 2024-11-02 07:42:04 EDT
Daylight Time Sunset Example: 2024-11-02 18:38:12 EDT
Standard Sunrise Time Example: 2024-11-03 06:42:52 EST
Standard Sunset Time Example: 2024-11-03 17:37:26 EST
Notes
- This code panics if there's an issue (i.e. error handling could be improved)
- Handles daylight saving time changes
-
From what I can tell it's not necessary to convert the date into UTC before running the
sunrise_sunsetfunction. There might be a use case though based on some time zones where that would be necessary, but I haven't figured out that edge case yet if it exists.
end of line
References
The crate that does the main date handling
The crate that allows using named time zones for chrono's DateTime
A macro crate that helps speed up searching for time zones in chrono_tz when multiple searches are needed
The crate that does the calculation for sunrise and sunset given a specific lat/long
The create the provides time zones for specific lat/long coordinates