use std::path::Path;
use serde_json::{Map, Value};
pub const SECTION: &str = "appearance";
pub const SCHEDULE_KEY: &str = "schedule";
pub const SUNRISE_KEY: &str = "sunrise";
pub const SUNSET_KEY: &str = "sunset";
pub const MODE_KEY: &str = "mode";
pub const AUTOMATIC_MODE: &str = "automatic";
const MINUTES_PER_DAY: i32 = 24 * 60;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DaySchedule {
pub sunrise: u16,
pub sunset: u16,
}
impl DaySchedule {
pub const FIXED: Self = Self {
sunrise: 7 * 60,
sunset: 19 * 60,
};
pub fn is_night(self, minute_of_day: u16) -> bool {
if self.sunrise <= self.sunset {
minute_of_day < self.sunrise || minute_of_day >= self.sunset
} else {
minute_of_day >= self.sunset && minute_of_day < self.sunrise
}
}
}
impl Default for DaySchedule {
fn default() -> Self {
Self::FIXED
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalMoment {
pub day_of_year: u16,
pub minute_of_day: u16,
pub utc_offset_minutes: i32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Location {
pub latitude: f64,
pub longitude: f64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ScheduleConfig {
pub follow_sun: bool,
pub fixed: DaySchedule,
}
impl Default for ScheduleConfig {
fn default() -> Self {
Self {
follow_sun: true,
fixed: DaySchedule::FIXED,
}
}
}
impl ScheduleConfig {
pub fn from_section(section: &Map<String, Value>) -> Self {
let time = |key: &str, fallback: u16| {
section
.get(key)
.and_then(Value::as_str)
.and_then(parse_hhmm)
.unwrap_or(fallback)
};
Self {
follow_sun: section.get(SCHEDULE_KEY).and_then(Value::as_str) != Some("custom"),
fixed: DaySchedule {
sunrise: time(SUNRISE_KEY, DaySchedule::FIXED.sunrise),
sunset: time(SUNSET_KEY, DaySchedule::FIXED.sunset),
},
}
}
pub fn resolve(self, location: Option<Location>, moment: LocalMoment) -> DaySchedule {
if self.follow_sun
&& let Some(location) = location
&& let Some(sun) = sun_times(location, moment.day_of_year, moment.utc_offset_minutes)
{
return sun;
}
self.fixed
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Scheduled {
pub config: ScheduleConfig,
pub location: Option<Location>,
}
impl Scheduled {
pub fn from_settings(settings: &Map<String, Value>) -> Self {
let section = settings
.get(SECTION)
.and_then(Value::as_object)
.cloned()
.unwrap_or_default();
Self {
config: ScheduleConfig::from_section(§ion),
location: timezone_location(),
}
}
pub fn is_night(&self, moment: LocalMoment) -> bool {
self.config
.resolve(self.location, moment)
.is_night(moment.minute_of_day)
}
}
pub fn is_automatic(mode: &str) -> bool {
mode == AUTOMATIC_MODE
}
pub fn parse_hhmm(value: &str) -> Option<u16> {
let (hours, minutes) = value.trim().split_once(':')?;
if minutes.len() != 2 {
return None;
}
let hours: u16 = hours.parse().ok()?;
let minutes: u16 = minutes.parse().ok()?;
(hours < 24 && minutes < 60).then(|| hours.saturating_mul(60).saturating_add(minutes))
}
pub fn format_hhmm(minute_of_day: u16) -> String {
let minute_of_day = minute_of_day % (24 * 60);
tilo_formats::machine!(
"a clock time the settings document stores for a person's schedule, read back by parse_hhmm",
format!("{:02}:{:02}", minute_of_day / 60, minute_of_day % 60)
)
}
pub fn sun_times(
location: Location,
day_of_year: u16,
utc_offset_minutes: i32,
) -> Option<DaySchedule> {
use std::f64::consts::PI;
let gamma = 2.0 * PI / 365.0 * (f64::from(day_of_year) - 1.0);
let equation_of_time = 229.18
* (0.000_075 + 0.001_868 * gamma.cos()
- 0.032_077 * gamma.sin()
- 0.014_615 * (2.0 * gamma).cos()
- 0.040_849 * (2.0 * gamma).sin());
let declination = 0.006_918 - 0.399_912 * gamma.cos() + 0.070_257 * gamma.sin()
- 0.006_758 * (2.0 * gamma).cos()
+ 0.000_907 * (2.0 * gamma).sin()
- 0.002_697 * (3.0 * gamma).cos()
+ 0.001_48 * (3.0 * gamma).sin();
let latitude = location.latitude.to_radians();
let cos_hour_angle = 90.833_f64.to_radians().cos() / (latitude.cos() * declination.cos())
- latitude.tan() * declination.tan();
if !(-1.0..=1.0).contains(&cos_hour_angle) {
return None;
}
let hour_angle = cos_hour_angle.acos().to_degrees();
let local = |utc_minutes: f64| -> Option<u16> {
if !utc_minutes.is_finite() {
return None;
}
#[allow(
clippy::cast_possible_truncation,
reason = "a finite sunrise/sunset is within a day or two of noon, far inside i32"
)]
let minutes = (utc_minutes.round() as i32)
.saturating_add(utc_offset_minutes)
.rem_euclid(MINUTES_PER_DAY);
u16::try_from(minutes).ok()
};
Some(DaySchedule {
sunrise: local(720.0 - 4.0 * (location.longitude + hour_angle) - equation_of_time)?,
sunset: local(720.0 - 4.0 * (location.longitude - hour_angle) - equation_of_time)?,
})
}
pub fn timezone_location() -> Option<Location> {
let zone = system_timezone()?;
[
"/usr/share/zoneinfo/zone1970.tab",
"/usr/share/zoneinfo/zone.tab",
]
.iter()
.find_map(|table| std::fs::read_to_string(table).ok())
.and_then(|table| timezone_location_in(&zone, &table))
}
fn system_timezone() -> Option<String> {
if let Some(tz) = std::env::var_os("TZ").and_then(|v| v.into_string().ok()) {
let tz = tz.trim_start_matches(':');
if tz.contains('/') && !tz.starts_with('/') {
return Some(tz.to_string());
}
}
zone_from_link(&std::fs::read_link(Path::new("/etc/localtime")).ok()?)
}
fn zone_from_link(target: &Path) -> Option<String> {
let text = target.to_str()?;
let (_, zone) = text.split_once("zoneinfo/")?;
(!zone.is_empty()).then(|| zone.to_string())
}
pub fn timezone_location_in(zone: &str, table: &str) -> Option<Location> {
table
.lines()
.filter(|line| !line.starts_with('#'))
.find_map(|line| {
let mut columns = line.split('\t');
let _countries = columns.next()?;
let coordinate = columns.next()?;
(columns.next()? == zone).then_some(coordinate)
})
.and_then(parse_iso6709)
}
pub fn parse_iso6709(value: &str) -> Option<Location> {
let split = value
.char_indices()
.skip(1)
.find_map(|(i, c)| matches!(c, '+' | '-').then_some(i))?;
Some(Location {
latitude: parse_coordinate_part(value.get(..split)?, true)?,
longitude: parse_coordinate_part(value.get(split..)?, false)?,
})
}
fn parse_coordinate_part(value: &str, latitude: bool) -> Option<f64> {
let sign = match value.as_bytes().first().copied()? {
b'+' => 1.0,
b'-' => -1.0,
_ => return None,
};
let digits = value.get(1..)?;
let (degrees_end, minutes_end, with_seconds) = match (latitude, digits.len()) {
(true, 4) => (2, 4, false),
(true, 6) => (2, 4, true),
(false, 5) => (3, 5, false),
(false, 7) => (3, 5, true),
_ => return None,
};
let degrees: f64 = digits.get(..degrees_end)?.parse().ok()?;
let minutes: f64 = digits.get(degrees_end..minutes_end)?.parse().ok()?;
let seconds: f64 = if with_seconds {
digits.get(minutes_end..)?.parse().ok()?
} else {
0.0
};
let limit = if latitude { 90.0 } else { 180.0 };
if degrees > limit || minutes >= 60.0 || seconds >= 60.0 {
return None;
}
Some(sign * (degrees + minutes / 60.0 + seconds / 3600.0))
}
#[cfg(test)]
mod tests {
use super::*;
const BERLIN: Location = Location {
latitude: 52.5,
longitude: 13.366_666,
};
fn at(day_of_year: u16, hhmm: &str, utc_offset_minutes: i32) -> LocalMoment {
LocalMoment {
day_of_year,
minute_of_day: parse_hhmm(hhmm).unwrap(),
utc_offset_minutes,
}
}
fn near(actual: u16, expected: &str) {
let expected = parse_hhmm(expected).unwrap();
assert!(
actual.abs_diff(expected) <= 5,
"{} is not within five minutes of {}",
format_hhmm(actual),
format_hhmm(expected)
);
}
tilo_test_env::scenario! {
fn a_schedule_resolves_fixed_custom_and_malformed_settings;
phase without_a_location_the_fixed_seven_to_seven_applies;
{
let config = ScheduleConfig::default();
let schedule = config.resolve(None, at(100, "12:00", 0));
assert_eq!(format_hhmm(schedule.sunrise), "07:00");
assert_eq!(format_hhmm(schedule.sunset), "19:00");
assert!(schedule.is_night(parse_hhmm("06:59").unwrap()));
assert!(!schedule.is_night(parse_hhmm("07:00").unwrap()));
assert!(!schedule.is_night(parse_hhmm("18:59").unwrap()));
assert!(schedule.is_night(parse_hhmm("19:00").unwrap()));
}
phase custom_times_win_over_the_sun_and_may_wrap_midnight;
{
let mut section = Map::new();
section.insert(SCHEDULE_KEY.into(), "custom".into());
section.insert(SUNRISE_KEY.into(), "08:30".into());
section.insert(SUNSET_KEY.into(), "21:15".into());
let config = ScheduleConfig::from_section(§ion);
let schedule = config.resolve(Some(BERLIN), at(172, "12:00", 120));
assert_eq!(schedule.sunrise, parse_hhmm("08:30").unwrap());
assert_eq!(schedule.sunset, parse_hhmm("21:15").unwrap());
let inverted = DaySchedule {
sunrise: parse_hhmm("22:00").unwrap(),
sunset: parse_hhmm("06:00").unwrap(),
};
assert!(!inverted.is_night(parse_hhmm("23:00").unwrap()));
assert!(inverted.is_night(parse_hhmm("12:00").unwrap()));
}
phase malformed_settings_take_the_defaults;
{
let mut section = Map::new();
section.insert(SUNRISE_KEY.into(), "7am".into());
section.insert(SUNSET_KEY.into(), Value::from(19));
assert_eq!(
ScheduleConfig::from_section(§ion),
ScheduleConfig::default()
);
assert_eq!(parse_hhmm("24:00"), None);
assert_eq!(parse_hhmm("07:60"), None);
assert_eq!(format_hhmm(parse_hhmm("7:05").unwrap()), "07:05");
}
}
tilo_test_env::scenario! {
fn sun_times_come_from_the_location_and_the_time_zone;
phase berlin_sunrise_and_sunset_match_the_almanac;
{
let summer = sun_times(BERLIN, 172, 120).unwrap();
near(summer.sunrise, "04:43");
near(summer.sunset, "21:33");
}
phase polar_day_has_no_sunset_and_falls_back_to_fixed_times;
{
let tromso = Location {
latitude: 69.65,
longitude: 18.96,
};
assert_eq!(sun_times(tromso, 172, 120), None);
let schedule = ScheduleConfig::default().resolve(Some(tromso), at(172, "12:00", 120));
assert_eq!(schedule, DaySchedule::FIXED);
}
phase the_time_zone_names_a_location_in_tzdata;
{
let table = "# comment\nDE,DK,NO,SE,SJ\t+5230+01322\tEurope/Berlin\n\
AU\t-3352+15113\tAustralia/Sydney\tNew South Wales\n";
let berlin = timezone_location_in("Europe/Berlin", table).unwrap();
assert!((berlin.latitude - 52.5).abs() < 1e-9);
assert!((berlin.longitude - 13.366_666).abs() < 1e-3);
let sydney = timezone_location_in("Australia/Sydney", table).unwrap();
assert!(sydney.latitude < 0.0);
assert_eq!(timezone_location_in("Mars/Olympus", table), None);
assert_eq!(
zone_from_link(Path::new("../usr/share/zoneinfo/Europe/Berlin")).as_deref(),
Some("Europe/Berlin")
);
}
}
#[test]
fn automatic_theme_follows_the_schedule() {
let scheduled = Scheduled {
config: ScheduleConfig::default(),
location: Some(BERLIN),
};
assert!(!scheduled.is_night(at(172, "12:00", 120)));
assert!(scheduled.is_night(at(172, "23:00", 120)));
assert!(
scheduled.is_night(at(355, "17:00", 60)),
"dark before five in winter"
);
assert!(is_automatic("automatic"));
assert!(!is_automatic("dark"));
}
}