Skip to content

Commit

Permalink
replace usage of chrono with time
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Oct 6, 2024
1 parent b25b41d commit c312209
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 89 deletions.
67 changes: 3 additions & 64 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ publish.workspace = true

[dependencies]
ahash = "0.8"
meowtonin = { workspace = true }
chrono = "0.4"
crossbeam-channel = "0.5"
meowtonin = { workspace = true }
parking_lot = { workspace = true }
scopeguard = "1"
thread-priority = "1"
time = { version = "0.3", features = ["formatting", "macros"] }
6 changes: 3 additions & 3 deletions crates/logger/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: MPL-2.0
use chrono::{DateTime, Utc};
use time::OffsetDateTime;

/// A log message and a timestamp of when it was logged.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(target_pointer_width = "32", repr(align(64)))]
#[cfg_attr(target_pointer_width = "64", repr(align(128)))]
pub(crate) struct LogMessage {
/// The time the message was logged.
pub timestamp: DateTime<Utc>,
pub timestamp: OffsetDateTime,
/// The log message itself.
pub message: String,
/// Whether to format the message or not.
Expand All @@ -17,7 +17,7 @@ pub(crate) struct LogMessage {
impl LogMessage {
/// Creates a new log message at the current in time.
pub fn new(message: impl Into<String>, format: bool) -> Self {
let timestamp = Utc::now();
let timestamp = OffsetDateTime::now_utc();
let message = message.into();
Self {
timestamp,
Expand Down
13 changes: 11 additions & 2 deletions crates/logger/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-License-Identifier: MPL-2.0

use crate::{counter::THREAD_COUNTER, message::LogMessage};
use crossbeam_channel::Receiver;
use std::{
Expand All @@ -25,7 +24,17 @@ fn log_thread(path: PathBuf, rx: Receiver<LogMessage>) {
if log.format {
let mut lines = log.message.lines();
if let Some(first) = lines.next() {
let _ = writeln!(file, "[{}] {}", log.timestamp.format("%F %T%.3f"), first);
let formatter = time::macros::format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]"
);
let _ = writeln!(
file,
"[{}] {}",
log.timestamp
.format(&formatter)
.unwrap_or_else(|_| unreachable!("invalid formatter somehow??")),
first
);
for line in lines {
let _ = writeln!(file, " - {}", line);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ license.workspace = true
publish.workspace = true

[dependencies]
meowtonin = { workspace = true }
slotmap = { workspace = true }
aneri-core = { path = "../core", features = ["runtime"] }
meowtonin = { workspace = true }
parking_lot = { workspace = true }
chrono = "0.4"
slotmap = { workspace = true }
time = { version = "0.3", features = ["formatting", "macros"] }
24 changes: 9 additions & 15 deletions crates/time/src/timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use chrono::Local;
use std::{
fmt::Write,
time::{SystemTime, UNIX_EPOCH},
};
use std::time::{SystemTime, UNIX_EPOCH};
use time::OffsetDateTime;

#[byond_fn]
pub fn unix_timestamp() -> String {
Expand All @@ -17,14 +14,11 @@ pub fn unix_timestamp() -> String {
}

#[byond_fn]
pub fn human_readable_timestamp(millis_precision: usize) -> String {
let now = Local::now();
let mut result = now.format("%Y-%m-%d %H:%M:%S").to_string();

if millis_precision > 0 {
let millis = now.timestamp_subsec_millis();
let _ = write!(result, ".{:0width$}", millis, width = millis_precision);
}

result
pub fn human_readable_timestamp() -> String {
let formatter = time::macros::format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]"
);
OffsetDateTime::now_utc()
.format(&formatter)
.unwrap_or_else(|_| unreachable!("invalid formatter?"))
}

0 comments on commit c312209

Please sign in to comment.