Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for DateTime types #287

Merged
merged 22 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b856bcd
WIP: Initial support for DateTime types
eminence Dec 31, 2023
db65016
Apply review suggestions
eminence Jan 6, 2024
97260b6
Add new datetime functions and related tests
eminence Jan 6, 2024
de2eb70
Add tentative impl for timezone conversion
eminence Jan 7, 2024
6029da4
Better TypeErrors when dealing with DateTimes
eminence Jan 14, 2024
bd8f31f
Add tab-completion for timezones
eminence Jan 14, 2024
96f218b
Update numbat-wasm/Cargo.lock
eminence Jan 14, 2024
7e88c64
Include "local" in the list of all timezones for completion
eminence Jan 14, 2024
ad0985e
Move datetime functions out of core and into their own module
eminence Feb 8, 2024
ac2fcb3
Replace a bool flag in BinaryOperatorForDate with an enum with more m…
eminence Feb 8, 2024
8b08b76
Replace a panic with an unreachable
eminence Feb 8, 2024
08dda61
Throw a runtime error if the provided timezone is unknown
eminence Feb 8, 2024
9cca835
Minor things for datetime handling
eminence Feb 8, 2024
af8782c
In the tabcompletion code for timezones, recognize other conversion o…
eminence Feb 8, 2024
ff75a85
Remove Expression::DateTime from the AST as this wasn't actually used…
eminence Feb 8, 2024
61a1522
Fix string interpolation for datetimes
eminence Feb 8, 2024
cc41d18
Update/clarify comments
eminence Feb 8, 2024
8790f05
Further small fixups, from codereview
eminence Feb 9, 2024
d8bcf07
Switch to a new "subsec_nanos" function from chrono
eminence Feb 9, 2024
83be237
Allow tz conversion using any string type
eminence Feb 9, 2024
7ff15cf
Remove DateOperationResult
sharkdp Feb 9, 2024
4134906
Remove TODO
sharkdp Feb 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions Cargo.lock

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

15 changes: 15 additions & 0 deletions examples/datetime_tests.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let epoch = parse_datetime("1970-01-01T00:00:00Z")
assert_eq(to_unixtime(epoch), 0)

assert_eq(to_unixtime(epoch + 1000 milliseconds + 2 seconds), 3)

let x = parse_datetime("Wed, 20 Jul 2022 21:52:05 +0200")
assert_eq(to_unixtime(x), 1658346725)

assert_eq(to_unixtime(from_unixtime(1658346725)), 1658346725)

# 2020 was a leap year
let y = parse_datetime("2020-02-28T20:00:00Z")
assert(format_datetime("%Y/%m/%d", y + 12 hours) == "2020/02/29")
let z = parse_datetime("2021-02-28T20:00:00Z")
assert(format_datetime("%Y/%m/%d", z + 12 hours) == "2021/03/01")
1 change: 1 addition & 0 deletions numbat-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ itertools = "0.12"
toml = { version = "0.8.8", features = ["parse"] }
serde = { version = "1.0.195", features = ["derive"] }
terminal_size = "0.3.0"
chrono-tz = "0.8.5"

[dependencies.clap]
version = "4"
Expand Down
40 changes: 40 additions & 0 deletions numbat-cli/src/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustyline::{
pub struct NumbatCompleter {
pub context: Arc<Mutex<Context>>,
pub modules: Vec<String>,
pub all_timezones: Vec<&'static str>,
}

impl Completer for NumbatCompleter {
Expand Down Expand Up @@ -73,6 +74,45 @@ impl Completer for NumbatCompleter {
));
}

// does it look like we're tab-completing a timezone (via the conversion operator)?
sharkdp marked this conversation as resolved.
Show resolved Hide resolved
let complete_tz = line
.find("->")
.or_else(|| line.find("→"))
.or_else(|| line.find("➞"))
.or_else(|| line.find(" to "))
.and_then(|convert_pos| {
if let Some(quote_pos) = line.rfind('"') {
if quote_pos > convert_pos && pos > quote_pos {
return Some(quote_pos + 1);
}
}
None
});
if let Some(pos_word) = complete_tz {
let word_part = &line[pos_word..];
let matches = self
.all_timezones
.iter()
.filter(|tz| tz.starts_with(word_part))
.collect::<Vec<_>>();
let append_closing_quote = matches.len() <= 1;

return Ok((
pos_word,
matches
.into_iter()
.map(|tz| Pair {
display: tz.to_string(),
replacement: if append_closing_quote {
format!("{tz}\"")
} else {
tz.to_string()
},
})
.collect(),
));
}

let (pos_word, word_part) = extract_word(line, pos, None, |c| {
// TODO: we could use is_identifier_char here potentially
match c {
Expand Down
5 changes: 5 additions & 0 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ impl Cli {
completer: NumbatCompleter {
context: self.context.clone(),
modules: self.context.lock().unwrap().list_modules().collect(),
all_timezones: {
let mut all_tz: Vec<_> = chrono_tz::TZ_VARIANTS.map(|v| v.name()).into();
all_tz.push("local");
all_tz
},
},
highlighter: NumbatHighlighter {
context: self.context.clone(),
Expand Down
Loading
Loading