Skip to content

Commit

Permalink
Implement nn::time methods for formatting the file log path (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
jozz024 authored Dec 2, 2023
1 parent 02da13b commit 6f1b8e2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ninput = { git = "https://github.com/blu-dev/ninput" }
lazysimd = { git = "https://github.com/Raytwo/lazysimd" }

[patch.crates-io]
nnsdk = { git = "https://github.com/Raytwo/nnsdk-rs", branch="account_fs" }
nnsdk = { git = "https://github.com/ultimate-research/nnsdk-rs"}
# Specifying latest commit because git only doesn't use latest changes
native-tls = { git = "https://github.com/skyline-rs/rust-native-tls", rev = "f202fca" }

Expand Down
39 changes: 5 additions & 34 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,14 @@ use std::{
use log::{LevelFilter, Metadata, Record, SetLoggerError};
use once_cell::sync::Lazy;
use parking_lot::Mutex;

use skyline::nn::time;
use crate::config;

/// Since we can't rely on most time based libraries, this is a seconds -> date/time string based on the `chrono` crates implementation
fn format_time_string(seconds: u64) -> String {
let leapyear = |year| -> bool { year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) };

static YEAR_TABLE: [[u64; 12]; 2] = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
];

let mut year = 1970;

let seconds_in_day = seconds % 86400;
let mut day_number = seconds / 86400;

let sec = seconds_in_day % 60;
let min = (seconds_in_day % 3600) / 60;
let hours = seconds_in_day / 3600;
loop {
let year_length = if leapyear(year) { 366 } else { 365 };

if day_number >= year_length {
day_number -= year_length;
year += 1;
} else {
break;
}
}
let mut month = 0;
while day_number >= YEAR_TABLE[if leapyear(year) { 1 } else { 0 }][month] {
day_number -= YEAR_TABLE[if leapyear(year) { 1 } else { 0 }][month];
month += 1;
}
fn get_time_string() -> String {
let datetime: time::CalendarTime = time::get_calendar_time();

format!("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", year, month + 1, day_number + 1, hours, min, sec)
format!("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second)
}

static LOG_PATH: &str = "sd:/ultimate/arcropolis/logs";
Expand Down Expand Up @@ -74,7 +45,7 @@ static FILE_WRITER: Lazy<FileLogger> = Lazy::new(|| {
let seconds = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Clock may have gone backwards!");
let path = Path::new(LOG_PATH).join(format!("{}.log", format_time_string(seconds.as_secs())));
let path = Path::new(LOG_PATH).join(format!("{}.log", get_time_string()));
let _ = std::fs::create_dir_all(LOG_PATH);
std::fs::File::create(path).map_or_else(
|_| {
Expand Down

0 comments on commit 6f1b8e2

Please sign in to comment.