Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
use local time everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
amokfa committed Apr 15, 2023
1 parent aec3cf0 commit d327e0b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlin.io.path.copyTo

buildscript {
extra.apply {
set("sdk_version", "v0.6.2")
set("sdk_version", "v0.6.3")
set("kotlin_version", "1.7.0")
}
repositories {
Expand Down
15 changes: 13 additions & 2 deletions utilities/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
clap = { version = "4.0.32", features = ["derive"] }
time = "0.3.17"
time = { version = "0.3.20", features = ["local-offset"] }
regex = "1.7.0"
lazy_static = "1.4.0"

Expand Down
24 changes: 15 additions & 9 deletions utilities/src/bin/uplink_watchdog.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use std::io::Write;
use std::ops::Add;
use std::process::Command;
use std::thread::sleep;
use std::time::Duration;
use regex::Regex;
use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};
use utilities::next_wednesday_3am;

lazy_static::lazy_static! {
static ref UPLINK_MODE_REGEX: Regex = Regex::new("^.+Switching to (.+) mode!!$").unwrap();
}

fn current_time() -> OffsetDateTime {
OffsetDateTime::now_utc()
.to_offset(UtcOffset::current_local_offset()
.unwrap_or(UtcOffset::from_hms(5, 30, 0).unwrap()))
}

fn uplink_running() -> bool {
Command::new("pgrep")
.arg("-x")
Expand Down Expand Up @@ -44,12 +52,10 @@ fn _internet_working() -> bool {
}

fn main() {
let tomorrow_3am = OffsetDateTime::now_utc()
.date()
.next_day().unwrap()
.with_hms(22, 15, 0)
.unwrap()
.assume_offset(UtcOffset::UTC);
// safe as long as we don't modify environment variables
unsafe { time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound); }
let now = current_time();
let oh_boy_3am = next_wednesday_3am(now);

let current_exe_path = std::env::current_exe().unwrap();
let uplink_module_dir = current_exe_path
Expand All @@ -63,16 +69,16 @@ fn main() {
.output();
};

println!("waiting till: {tomorrow_3am}");
println!("{now}: waiting till: {oh_boy_3am}");
let mut _tics = 0;
loop {
let _ = std::io::stdout().flush();
let _ = std::io::stderr().flush();
sleep(Duration::from_secs(1));
_tics += 1;
let now = OffsetDateTime::now_utc();
let now = current_time();

if now > tomorrow_3am {
if now > oh_boy_3am {
println!("{now}: restarting uplink");
restart()
} else if !uplink_running() {
Expand Down
90 changes: 89 additions & 1 deletion utilities/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![feature(const_option)]
#![feature(const_result_drop)]

use std::fmt::{Debug, Display, Formatter};
use std::io::Write;
use std::ops::Add;
use std::time::Duration;
use time::{OffsetDateTime, UtcOffset};

#[derive(Debug)]
pub struct LazyFile {
Expand Down Expand Up @@ -62,4 +68,86 @@ impl<T: Debug> Trace<T> for T {
println!("{:?}", self);
self
}
}
}

pub fn next_wednesday_3am(anchor: OffsetDateTime) -> OffsetDateTime {
let current_weekday = anchor.weekday().number_days_from_sunday();
let wednesday_weekday = time::Weekday::Wednesday.number_days_from_sunday();
let three_am = time::Time::from_hms(3, 30, 0).unwrap();
let before_wednesday_case = current_weekday < wednesday_weekday || (current_weekday == wednesday_weekday && anchor.time() < three_am);
// let after_wednesday_case = current_weekday > wednesday_weekday || (current_weekday == wednesday_weekday && anchor.time() >= three_am);
if before_wednesday_case {
anchor.add(Duration::from_secs(60 * 60 * 24 * ((wednesday_weekday - current_weekday) as u64)))
.replace_time(three_am)
} else {
anchor.add(Duration::from_secs(60 * 60 * 24 * ((7 - (current_weekday - wednesday_weekday)) as u64)))
.replace_time(three_am)
}
}

pub const INDIA_OFFSET: UtcOffset = UtcOffset::from_hms(5, 30, 0).ok().unwrap();

mod test {
use time::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
use crate::{INDIA_OFFSET, next_wednesday_3am};

#[test]
fn t2() {
dbg!(OffsetDateTime::now_utc().to_offset(Utc))
}

#[test]
fn t1() {
assert_eq!(
next_wednesday_3am(
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 14).unwrap(),
Time::from_hms(13, 20, 0).unwrap()
).assume_offset(INDIA_OFFSET)
),
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 19).unwrap(),
Time::from_hms(3, 30, 0).unwrap()
).assume_offset(INDIA_OFFSET)
);

assert_eq!(
next_wednesday_3am(
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 18).unwrap(),
Time::from_hms(13, 20, 0).unwrap()
).assume_offset(INDIA_OFFSET)
),
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 19).unwrap(),
Time::from_hms(3, 30, 0).unwrap()
).assume_offset(INDIA_OFFSET)
);

assert_eq!(
next_wednesday_3am(
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 19).unwrap(),
Time::from_hms(1, 20, 0).unwrap()
).assume_offset(INDIA_OFFSET)
),
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 19).unwrap(),
Time::from_hms(3, 30, 0).unwrap()
).assume_offset(INDIA_OFFSET)
);

assert_eq!(
next_wednesday_3am(
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 19).unwrap(),
Time::from_hms(13, 20, 0).unwrap()
).assume_offset(INDIA_OFFSET)
),
PrimitiveDateTime::new(
Date::from_calendar_date(2023, Month::April, 26).unwrap(),
Time::from_hms(3, 30, 0).unwrap()
).assume_offset(INDIA_OFFSET)
);
}
}

0 comments on commit d327e0b

Please sign in to comment.