Skip to content

Commit

Permalink
Remove time crate in favor of manual impl
Browse files Browse the repository at this point in the history
Cuts down a few more dependencies.
  • Loading branch information
YaLTeR committed Oct 24, 2023
1 parent 909a45d commit 64ac316
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 73 deletions.
62 changes: 0 additions & 62 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ portable-atomic = { version = "1.5.0", default-features = false, features = ["fl
profiling = "1.0.11"
sd-notify = "0.4.1"
serde = { version = "1.0.189", features = ["derive"] }
time = { version = "0.3.30", features = ["formatting", "local-offset", "macros"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing = { version = "0.1.40", features = ["max_level_trace", "release_max_level_debug"] }
tracy-client = { version = "0.16.3", default-features = false }
Expand Down
32 changes: 22 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::ffi::OsStr;
use std::io::{self, Write};
use std::os::unix::prelude::OsStrExt;
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::ptr::null_mut;
use std::time::Duration;

use anyhow::Context;
use anyhow::{ensure, Context};
use directories::UserDirs;
use smithay::reexports::rustix::time::{clock_gettime, ClockId};
use smithay::utils::{Logical, Point, Rectangle};
use time::OffsetDateTime;

pub fn get_monotonic_time() -> Duration {
let ts = clock_gettime(ClockId::Monotonic);
Expand All @@ -29,16 +30,27 @@ pub fn make_screenshot_path() -> anyhow::Result<PathBuf> {
});
path.push("Screenshots");

let mut buf = [0u8; 256];
let name;
unsafe {
// are you kidding me
time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
};
let time = libc::time(null_mut());
ensure!(time != -1, "error in time()");

let tm = libc::localtime(&time);
ensure!(!tm.is_null(), "error in localtime()");

let format = b"Screenshot from %Y-%m-%d %H-%M-%S.png\0";
let rv = libc::strftime(
buf.as_mut_ptr().cast(),
buf.len(),
format.as_ptr().cast(),
tm,
);
ensure!(rv != 0, "error formatting time");

name = OsStr::from_bytes(&buf[..rv]);
}

let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
let desc = time::macros::format_description!(
"Screenshot from [year]-[month]-[day] [hour]-[minute]-[second].png"
);
let name = now.format(desc).context("error formatting time")?;
path.push(name);

Ok(path)
Expand Down

0 comments on commit 64ac316

Please sign in to comment.