Skip to content

Commit 10b538a

Browse files
committed
Upgrade to time-0.3
This is primarily to support querying the current time in a wasm context, but also to modernize the use of the time crate.
1 parent 1bd3bd8 commit 10b538a

File tree

12 files changed

+68
-34
lines changed

12 files changed

+68
-34
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ jobs:
108108
profile: minimal
109109
# Target a minimum version that ships with common systems
110110
# out there. In particular, Raspbian testing as of
111-
# 2022-05-23.
112-
toolchain: 1.58
111+
# 2022-12-26.
112+
toolchain: 1.62
113113
default: true
114114
- uses: actions/checkout@v2
115115
- run: sudo apt update

client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ base64 = "0.13"
1717
bytes = "1.0"
1818
serde = { version = "1.0", features = ["derive"] }
1919
serde_json = "1.0"
20-
time = { version = "0.2", features = ["std"] }
20+
time = { version = "0.3", features = ["std"] }
2121
url = "2.2"
2222

2323
[dependencies.endbasic-core]

client/src/drive.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ impl Drive for CloudDrive {
4848
let response = self.service.borrow_mut().get_files(&self.username).await?;
4949
let mut entries = BTreeMap::default();
5050
for e in response.files {
51-
let date = time::OffsetDateTime::from_unix_timestamp(e.mtime as i64);
51+
let date = match time::OffsetDateTime::from_unix_timestamp(e.mtime as i64) {
52+
Ok(date) => date,
53+
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, format!("{}", e))),
54+
};
5255
entries.insert(e.filename, Metadata { date, length: e.length });
5356
}
5457
Ok(DriveFiles::new(
@@ -179,11 +182,17 @@ mod tests {
179182
let result = drive.enumerate().await.unwrap();
180183
assert_eq!(2, result.dirents().len());
181184
assert_eq!(
182-
&Metadata { date: time::OffsetDateTime::from_unix_timestamp(9000), length: 15 },
185+
&Metadata {
186+
date: time::OffsetDateTime::from_unix_timestamp(9000).unwrap(),
187+
length: 15
188+
},
183189
result.dirents().get("one").unwrap()
184190
);
185191
assert_eq!(
186-
&Metadata { date: time::OffsetDateTime::from_unix_timestamp(8000), length: 17 },
192+
&Metadata {
193+
date: time::OffsetDateTime::from_unix_timestamp(8000).unwrap(),
194+
length: 17
195+
},
187196
result.dirents().get("two").unwrap()
188197
);
189198
assert_eq!(&DiskSpace::new(10000, 100), result.disk_quota().as_ref().unwrap());

repl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313

1414
[dependencies]
1515
async-trait = "0.1"
16-
time = { version = "0.2", features = ["std"] }
16+
time = { version = "0.3", features = ["std"] }
1717

1818
[dependencies.endbasic-core]
1919
version = "0.9.99" # ENDBASIC-VERSION

repl/src/demos.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,39 @@ impl Default for DemosDrive {
5050
{
5151
let content = process_demo(include_bytes!("../examples/guess.bas"));
5252
let metadata = Metadata {
53-
date: time::OffsetDateTime::from_unix_timestamp(1608693152),
53+
date: time::OffsetDateTime::from_unix_timestamp(1608693152).unwrap(),
5454
length: content.len() as u64,
5555
};
5656
demos.insert("GUESS.BAS", (metadata, content));
5757
}
5858
{
5959
let content = process_demo(include_bytes!("../examples/gpio.bas"));
6060
let metadata = Metadata {
61-
date: time::OffsetDateTime::from_unix_timestamp(1613316558),
61+
date: time::OffsetDateTime::from_unix_timestamp(1613316558).unwrap(),
6262
length: content.len() as u64,
6363
};
6464
demos.insert("GPIO.BAS", (metadata, content));
6565
}
6666
{
6767
let content = process_demo(include_bytes!("../examples/hello.bas"));
6868
let metadata = Metadata {
69-
date: time::OffsetDateTime::from_unix_timestamp(1608646800),
69+
date: time::OffsetDateTime::from_unix_timestamp(1608646800).unwrap(),
7070
length: content.len() as u64,
7171
};
7272
demos.insert("HELLO.BAS", (metadata, content));
7373
}
7474
{
7575
let content = process_demo(include_bytes!("../examples/palette.bas"));
7676
let metadata = Metadata {
77-
date: time::OffsetDateTime::from_unix_timestamp(1671243940),
77+
date: time::OffsetDateTime::from_unix_timestamp(1671243940).unwrap(),
7878
length: content.len() as u64,
7979
};
8080
demos.insert("PALETTE.BAS", (metadata, content));
8181
}
8282
{
8383
let content = process_demo(include_bytes!("../examples/tour.bas"));
8484
let metadata = Metadata {
85-
date: time::OffsetDateTime::from_unix_timestamp(1608774770),
85+
date: time::OffsetDateTime::from_unix_timestamp(1608774770).unwrap(),
8686
length: content.len() as u64,
8787
};
8888
demos.insert("TOUR.BAS", (metadata, content));

std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async-channel = "1.5"
1616
async-trait = "0.1"
1717
futures-lite = "1.1"
1818
radix_trie = "0.2.1"
19-
time = { version = "0.2", features = ["std"] }
19+
time = { version = "0.3", features = ["formatting", "local-offset", "std"] }
2020

2121
[dependencies.endbasic-core]
2222
version = "0.9.99" # ENDBASIC-VERSION

std/src/storage/cmds.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ use std::cmp;
2828
use std::io;
2929
use std::rc::Rc;
3030
use std::str;
31+
use time::format_description;
32+
33+
use super::time_format_error_to_io_error;
3134

3235
/// Category description for all symbols provided by this module.
3336
const CATEGORY: &str = "File system
@@ -54,6 +57,9 @@ async fn show_dir(storage: &Storage, console: &mut dyn Console, path: &str) -> i
5457
let canonical_path = storage.make_canonical(path)?;
5558
let files = storage.enumerate(path).await?;
5659

60+
let format = format_description::parse("[year]-[month]-[day] [hour]:[minute]")
61+
.expect("Hardcoded format must be valid");
62+
5763
console.print("")?;
5864
console.print(&format!(" Directory of {}", canonical_path))?;
5965
console.print("")?;
@@ -63,7 +69,7 @@ async fn show_dir(storage: &Storage, console: &mut dyn Console, path: &str) -> i
6369
for (name, details) in files.dirents() {
6470
console.print(&format!(
6571
" {} {:6} {}",
66-
details.date.format("%F %H:%M"),
72+
details.date.format(&format).map_err(time_format_error_to_io_error)?,
6773
details.length,
6874
name,
6975
))?;

std/src/storage/fs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Drive for DirectoryDrive {
7575
// expect symlinks in the programs directory anyway. If we want to handle this
7676
// better, we'll have to add a way to report file types.
7777
let metadata = fs::metadata(de.path())?;
78-
let offset = match time::UtcOffset::try_current_local_offset() {
78+
let offset = match time::UtcOffset::current_local_offset() {
7979
Ok(offset) => offset,
8080
Err(_) => time::UtcOffset::UTC,
8181
};
@@ -207,7 +207,7 @@ mod tests {
207207
let drive = DirectoryDrive::new(dir.path()).unwrap();
208208
let files = block_on(drive.enumerate()).unwrap();
209209
assert_eq!(2, files.dirents().len());
210-
let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875);
210+
let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875).unwrap();
211211
assert_eq!(&Metadata { date, length: 0 }, files.dirents().get("empty.bas").unwrap());
212212
assert_eq!(&Metadata { date, length: 18 }, files.dirents().get("some file.bas").unwrap());
213213
}
@@ -239,8 +239,10 @@ mod tests {
239239
let drive = DirectoryDrive::new(dir.path()).unwrap();
240240
let files = block_on(drive.enumerate()).unwrap();
241241
assert_eq!(2, files.dirents().len());
242-
let metadata =
243-
Metadata { date: time::OffsetDateTime::from_unix_timestamp(1_588_757_875), length: 18 };
242+
let metadata = Metadata {
243+
date: time::OffsetDateTime::from_unix_timestamp(1_588_757_875).unwrap(),
244+
length: 18,
245+
};
244246
assert_eq!(&metadata, files.dirents().get("some file.bas").unwrap());
245247
assert_eq!(&metadata, files.dirents().get("a link.bas").unwrap());
246248
}

std/src/storage/mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Drive for InMemoryDrive {
4343
}
4444

4545
async fn enumerate(&self) -> io::Result<DriveFiles> {
46-
let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875);
46+
let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875).unwrap();
4747

4848
let mut entries = BTreeMap::new();
4949
for (name, (contents, _readers)) in &self.programs {

std/src/storage/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::fmt::{self};
2121
use std::io;
2222
use std::path::PathBuf;
2323
use std::str;
24+
use time::error::Format;
2425

2526
mod cmds;
2627
pub use cmds::*;
@@ -29,6 +30,14 @@ pub use fs::*;
2930
mod mem;
3031
pub use mem::*;
3132

33+
/// Converts a time formatting error to an I/O error.
34+
pub(crate) fn time_format_error_to_io_error(e: Format) -> io::Error {
35+
match e {
36+
Format::StdIo(e) => e,
37+
e => io::Error::new(io::ErrorKind::Other, format!("{}", e)),
38+
}
39+
}
40+
3241
/// Metadata of an entry in a storage medium.
3342
#[derive(Clone, Debug, Eq, PartialEq)]
3443
pub struct Metadata {

0 commit comments

Comments
 (0)