Skip to content

Commit 1a3eb78

Browse files
committed
✨ Parse time using two_timer
Use fixed two_timer (custom git) - this one can parse "in 3 hours".
1 parent cead296 commit 1a3eb78

File tree

6 files changed

+34
-8
lines changed

6 files changed

+34
-8
lines changed

Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ serde = { version = "1.0", features = ["derive"] }
3535
teloxide = { version = "0.17", features = ["macros"] }
3636
tera = "1"
3737
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
38-
two_timer = "2.2" # doesn't parse "in 3 hours"
38+
two_timer = { version = "2.2", git = "https://github.com/berkus/two-timer.git", branch = "updated" }

bot/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ serde.workspace = true
2727
teloxide.workspace = true
2828
tera.workspace = true
2929
tokio.workspace = true
30-
two_timer.workspace = true
3130

3231
[target.'cfg(target_os="linux")'.dependencies]
3332
procfs = "0.17"

bot/src/bin/bot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Async Rust implementation of the bot
22
//
33
// To make it usable it misses natty parsing lib implementation in rust
4-
// (There are now several rust impls including https://lib.rs/crates/two_timer and https://lib.rs/crates/intervalle)
4+
// Use https://lib.rs/crates/two_timer
55

66
use {
77
aegl_bot::{

lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ publish = false
99
name = "libbot"
1010

1111
[dependencies]
12+
anyhow.workspace = true
1213
chrono.workspace = true
1314
chrono-tz.workspace = true
1415
culpa.workspace = true
1516
# entity.workspace = true
17+
two_timer.workspace = true

lib/src/datetime.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use {
2+
anyhow::bail,
23
chrono::{prelude::*, DateTime, Duration, TimeZone, Utc},
34
chrono_tz::{Europe::Moscow, Tz},
5+
culpa::throws,
46
std::fmt::Write,
57
};
68

@@ -10,6 +12,30 @@ use {
1012
pub type BotDateTime = chrono::DateTime<chrono::Utc>;
1113
pub type BotTime = chrono::NaiveTime; // UTC
1214

15+
#[throws(anyhow::Error)]
16+
pub fn parse_time_spec(timespec: impl AsRef<str>) -> DateTime<Tz> {
17+
let now = Local::now().with_timezone(&Moscow);
18+
// Parse input in MSK timezone...
19+
// @todo Honor TELEGRAM_BOT_TIMEZONE envvar
20+
let start_time = match two_timer::parse(
21+
timespec.as_ref(),
22+
two_timer::Config::new(now).default_to_past(false),
23+
) {
24+
Ok((start, _end, _found)) => start, //.and_utc(),
25+
Err(_) => {
26+
bail!("❌ Failed to parse time {}", timespec.as_ref())
27+
}
28+
};
29+
30+
// ...then convert back to UTC.
31+
// let start_time = start_time.unwrap().0; //.and_utc();
32+
33+
// use chrono::Offset;
34+
// let offset = start_time.offset().fix();
35+
36+
start_time
37+
}
38+
1339
fn time_diff_string(duration: Duration) -> String {
1440
let times = vec![
1541
(Duration::days(365), "year"),

0 commit comments

Comments
 (0)