2024-08-21 10:14:01 +03:00
|
|
|
use std::ops::Sub;
|
2024-08-06 23:01:59 +03:00
|
|
|
|
2024-08-21 10:14:01 +03:00
|
|
|
use chrono::{DateTime, Local, TimeDelta, TimeZone, Utc};
|
2024-08-14 21:49:36 +03:00
|
|
|
use chrono::LocalResult::Single;
|
2024-08-06 23:01:59 +03:00
|
|
|
use log::{debug, error, info, trace, warn};
|
2024-08-14 21:49:36 +03:00
|
|
|
use nostr_sdk::Timestamp;
|
2024-08-06 23:01:59 +03:00
|
|
|
|
2024-08-25 11:16:05 +03:00
|
|
|
pub const CHARACTER_THRESHOLD: usize = 3;
|
|
|
|
|
2024-08-06 23:01:59 +03:00
|
|
|
pub fn some_non_empty(str: &str) -> Option<String> {
|
2024-08-08 15:09:39 +03:00
|
|
|
if str.is_empty() { None } else { Some(str.to_string()) }
|
2024-08-06 23:01:59 +03:00
|
|
|
}
|
|
|
|
|
2024-08-21 10:14:01 +03:00
|
|
|
/// Parses the hour from a plain number in the String,
|
|
|
|
/// with max of max_future hours into the future.
|
|
|
|
pub fn parse_hour(str: &str, max_future: i64) -> Option<DateTime<Local>> {
|
|
|
|
str.parse::<u32>().ok().and_then(|hour| {
|
|
|
|
let now = Local::now();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
now.date().and_hms_opt(hour, 0, 0).map(|time| {
|
|
|
|
if time - now > TimeDelta::hours(max_future) {
|
|
|
|
time.sub(TimeDelta::days(1))
|
|
|
|
} else {
|
|
|
|
time
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-19 22:16:19 +03:00
|
|
|
pub fn parse_date(str: &str) -> Option<DateTime<Utc>> {
|
2024-08-19 11:45:12 +03:00
|
|
|
// Using two libraries for better exhaustiveness, see https://github.com/uutils/parse_datetime/issues/84
|
2024-08-19 22:16:19 +03:00
|
|
|
match interim::parse_date_string(str, Local::now(), interim::Dialect::Us) {
|
2024-08-19 11:45:12 +03:00
|
|
|
Ok(date) => Some(date.to_utc()),
|
|
|
|
Err(e) => {
|
2024-08-19 22:16:19 +03:00
|
|
|
match parse_datetime::parse_datetime_at_date(Local::now(), str) {
|
2024-08-19 11:45:12 +03:00
|
|
|
Ok(date) => Some(date.to_utc()),
|
|
|
|
Err(_) => {
|
2024-08-25 11:16:05 +03:00
|
|
|
warn!("Could not parse date from \"{str}\": {e}");
|
2024-08-19 11:45:12 +03:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-19 22:16:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-22 11:13:35 +03:00
|
|
|
/// Turn a human-readable relative timestamp into a nostr Timestamp.
|
|
|
|
/// - Plain number as hour, 18 hours back or 6 hours forward
|
|
|
|
/// - Number with prefix as minute offset
|
|
|
|
/// - Otherwise try to parse a relative date
|
2024-08-19 22:16:19 +03:00
|
|
|
pub fn parse_tracking_stamp(str: &str) -> Option<Timestamp> {
|
2024-08-22 11:13:35 +03:00
|
|
|
if let Some(num) = parse_hour(str, 6) {
|
|
|
|
return Some(Timestamp::from(num.to_utc().timestamp() as u64));
|
|
|
|
}
|
2024-08-19 22:16:19 +03:00
|
|
|
let stripped = str.trim().trim_start_matches('+').trim_start_matches("in ");
|
|
|
|
if let Ok(num) = stripped.parse::<i64>() {
|
|
|
|
return Some(Timestamp::from(Timestamp::now().as_u64().saturating_add_signed(num * 60)));
|
|
|
|
}
|
|
|
|
parse_date(str).and_then(|time| {
|
2024-08-22 11:13:35 +03:00
|
|
|
let stamp = time.to_utc().timestamp();
|
|
|
|
if stamp > 0 {
|
|
|
|
Some(Timestamp::from(stamp as u64))
|
2024-08-19 11:45:12 +03:00
|
|
|
} else {
|
|
|
|
warn!("Can only track times after 1970!");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-21 11:56:15 +03:00
|
|
|
/// Format DateTime easily comprehensible for human but unambiguous.
|
|
|
|
/// Length may vary.
|
|
|
|
pub fn format_datetime_relative(time: DateTime<Local>) -> String {
|
|
|
|
let date = time.date_naive();
|
|
|
|
let prefix =
|
|
|
|
match Local::now()
|
|
|
|
.date_naive()
|
|
|
|
.signed_duration_since(date)
|
|
|
|
.num_days() {
|
|
|
|
-1 => "tomorrow ".into(),
|
|
|
|
0 => "".into(),
|
|
|
|
1 => "yesterday ".into(),
|
|
|
|
-3..=3 => date.format("%a ").to_string(),
|
|
|
|
//-10..=10 => date.format("%d. %a ").to_string(),
|
|
|
|
-100..=100 => date.format("%b %d ").to_string(),
|
|
|
|
_ => date.format("%y-%m-%d ").to_string(),
|
|
|
|
};
|
|
|
|
format!("{}{}", prefix, time.format("%H:%M"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format a nostr timestamp with the given formatting function.
|
|
|
|
pub fn format_as_datetime<F>(stamp: &Timestamp, formatter: F) -> String
|
|
|
|
where
|
|
|
|
F: Fn(DateTime<Local>) -> String,
|
|
|
|
{
|
2024-08-14 21:49:36 +03:00
|
|
|
match Local.timestamp_opt(stamp.as_u64() as i64, 0) {
|
2024-08-21 11:56:15 +03:00
|
|
|
Single(time) => formatter(time),
|
2024-08-14 21:49:36 +03:00
|
|
|
_ => stamp.to_human_datetime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 11:56:15 +03:00
|
|
|
/// Format nostr Timestamp relative to local time
|
|
|
|
/// with optional day specifier or full date depending on distance to today.
|
|
|
|
pub fn format_timestamp_relative(stamp: &Timestamp) -> String {
|
|
|
|
format_as_datetime(stamp, format_datetime_relative)
|
2024-08-14 21:49:36 +03:00
|
|
|
}
|
|
|
|
|
2024-08-21 11:56:15 +03:00
|
|
|
/// Format nostr timestamp with the given format.
|
|
|
|
pub fn format_timestamp(stamp: &Timestamp, format: &str) -> String {
|
|
|
|
format_as_datetime(stamp, |time| time.format(format).to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format nostr timestamp in a sensible comprehensive format with consistent length and consistent sorting.
|
|
|
|
///
|
|
|
|
/// Currently: 18 characters
|
|
|
|
pub fn format_timestamp_local(stamp: &Timestamp) -> String {
|
|
|
|
format_timestamp(stamp, "%y-%m-%d %a %H:%M")
|
2024-08-14 21:49:36 +03:00
|
|
|
}
|
|
|
|
|
2024-08-21 11:56:15 +03:00
|
|
|
pub fn format_timestamp_relative_to(stamp: &Timestamp, reference: &Timestamp) -> String {
|
|
|
|
// Rough difference in days
|
|
|
|
match (stamp.as_u64() as i64 - reference.as_u64() as i64) / 80_000 {
|
|
|
|
0 => format_timestamp(stamp, "%H:%M"),
|
|
|
|
-3..=3 => format_timestamp(stamp, "%a %H:%M"),
|
|
|
|
_ => format_timestamp_local(stamp),
|
|
|
|
}
|
|
|
|
}
|