Skip to content

Commit

Permalink
Make ctime() return in expected formatting (#29)
Browse files Browse the repository at this point in the history
This *should* be returning with local timezone, using `iana-time-zone` to determine that. However on my system it only ever returns UTC. That's probably a bug in iana-time-zone, or my system config, tho.

In any case the expected formatting seems to match:

;ctime()
=> "Sat Oct 07 17:23:43 2023 UTC"
  • Loading branch information
rdaum committed Oct 7, 2023
1 parent f3a82d0 commit f311a3c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pwhash = "1.0.0" # For MOO's hokey "crypt" function, which is unix's crypt(3) ba
md5 = "0.7.0" # For MOO's "string_hash"
rand = "0.8.5"
onig = "6.4.0"
chrono-tz = "0.8.3"
iana-time-zone = "0.1.57"

## Compiler grammar/parser
pest = "2.7.1"
Expand Down
2 changes: 2 additions & 0 deletions crates/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pwhash.workspace = true
md5.workspace = true
rand.workspace = true
onig.workspace = true
chrono-tz.workspace = true
iana-time-zone.workspace = true

## Error declaration/ handling
thiserror.workspace = true
Expand Down
17 changes: 14 additions & 3 deletions crates/kernel/src/builtins/bf_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::sync::Arc;
use std::time::{Duration, SystemTime};

use async_trait::async_trait;
use chrono::{DateTime, Local};
use chrono::{DateTime, Local, TimeZone};
use chrono_tz::{OffsetName, Tz};
use iana_time_zone::get_timezone;
use metrics_macros::increment_counter;
use tokio::sync::oneshot;
use tracing::{debug, error, info, warn};
Expand Down Expand Up @@ -298,8 +300,17 @@ async fn bf_ctime<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
};

let date_time: DateTime<Local> = chrono::DateTime::from(time);

Ok(Ret(v_string(date_time.to_rfc2822())))
let tz_str = get_timezone().unwrap();
let tz: Tz = tz_str.parse().unwrap();
let offset = tz.offset_from_local_date(&date_time.date_naive()).unwrap();
let abbreviation = offset.abbreviation();
let datetime_str = format!(
"{} {}",
date_time.format("%a %b %d %H:%M:%S %Y"),
abbreviation
);

Ok(Ret(v_string(datetime_str.to_string())))
}
bf_declare!(ctime, bf_ctime);
async fn bf_raise<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
Expand Down

0 comments on commit f311a3c

Please sign in to comment.