Skip to content

Commit

Permalink
Merge pull request #13 from simonwiles/timezone-aware
Browse files Browse the repository at this point in the history
Timezone Aware
  • Loading branch information
samwho authored Sep 14, 2023
2 parents 040306c + 97b06c9 commit 44caa0a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 22 deletions.
152 changes: 152 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ owo-colors = { version = "3", features = ["supports-colors"] }
log = "0.4"
env_logger = "0.10"
human-panic = "1.0"
chrono = "0.4.30"

[dev-dependencies]
test-case = "3"
27 changes: 5 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use chrono::Local;
use clap::Parser;
use lazy_static::lazy_static;
use log::debug;
use owo_colors::{self, OwoColorize, Stream};
use std::{
Expand All @@ -10,10 +10,7 @@ use std::{
thread::{scope, sleep},
};
use terminal_size::{Height, Width};
use time::{
format_description::OwnedFormatItem, macros::format_description, Instant, OffsetDateTime,
UtcOffset,
};
use time::Instant;

#[derive(Parser, Clone, Debug)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -55,16 +52,6 @@ impl TestStats {
}
}

lazy_static! {
static ref DATE_FORMAT: OwnedFormatItem = format_description!("[year]-[month]-[day]").into();
static ref TIME_FORMAT: OwnedFormatItem = format_description!(
"[hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]"
)
.into();
static ref LOCAL_OFFSET: Option<UtcOffset> =
UtcOffset::local_offset_at(OffsetDateTime::UNIX_EPOCH).ok();
}

fn format_elapsed(seconds: f64) -> String {
let minutes = seconds / 60.0;
let hours = minutes / 60.0;
Expand Down Expand Up @@ -100,8 +87,8 @@ fn print_spacer(mut output: impl Write, args: &Args, last_spacer: &Instant) -> R
writeln!(output, "{}", "\n".repeat(args.padding - 1))?;
}

let now = OffsetDateTime::now_utc().to_offset(LOCAL_OFFSET.unwrap_or(UtcOffset::UTC));
let date_str = now.format(&DATE_FORMAT)?;
let now = Local::now();
let date_str = now.format("%Y-%m-%d").to_string();
let mut buf = Vec::new();
write!(
buf,
Expand All @@ -110,7 +97,7 @@ fn print_spacer(mut output: impl Write, args: &Args, last_spacer: &Instant) -> R
)?;
dashes -= date_str.len() + 1;

let time_str = now.format(&TIME_FORMAT)?;
let time_str = now.format("%H:%M:%S").to_string();
write!(
buf,
"{} ",
Expand Down Expand Up @@ -182,10 +169,6 @@ fn run(
owo_colors::set_override(true);
}

if LOCAL_OFFSET.is_none() {
debug!("could not determine local timezone offset, using UTC");
}

scope(|s| {
let last_line = Arc::new(RwLock::new(Instant::now()));
let last_spacer = Arc::new(RwLock::new(Instant::now()));
Expand Down

0 comments on commit 44caa0a

Please sign in to comment.