Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: parse "every day at 9am" and weekday schedule formats

Louis Beaumontc9fd825fbaf2
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index b03955675..2b59b78f4 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -3721,6 +3721,11 @@ pub fn parse_schedule(schedule: &str) -> Option<ParsedSchedule> {
if let Some(d) = parse_duration_str(s) {
return Some(ParsedSchedule::Interval(d));
}
+ // Try "every day at Xam/pm" and "every <weekday> at Xam/pm"
+ // These are generated by the UI schedule dropdown.
+ if let Some(cron) = parse_human_schedule(s) {
+ return Some(ParsedSchedule::Cron(Box::new(cron)));
+ }
// Try cron expression (5 or 6 field)
// cron crate requires 7 fields (sec min hour dom month dow year),
// so we pad short expressions.
@@ -3735,6 +3740,52 @@ pub fn parse_schedule(schedule: &str) -> Option<ParsedSchedule> {
None
}
+/// Parse human-readable schedules like "every day at 9am", "every monday at 6pm".
+fn parse_human_schedule(s: &str) -> Option<CronSchedule> {
+ let s = s.to_lowercase();
+ let s = s.strip_prefix("every").unwrap_or(&s).trim();
+
+ // Extract "at Xam/pm" from the end
+ let (prefix, hour) = if let Some(at_pos) = s.find(" at ") {
+ let time_str = s[at_pos + 4..].trim();
+ let h = parse_time_str(time_str)?;
+ (s[..at_pos].trim(), h)
+ } else {
+ return None;
+ };
+
+ // "day" → every day at that hour
+ // "monday", "tuesday", etc. → specific weekday
+ let cron_str = match prefix {
+ "day" => format!("0 0 {} * * * *", hour),
+ "monday" | "mon" => format!("0 0 {} * * 1 *", hour),
+ "tuesday" | "tue" => format!("0 0 {} * * 2 *", hour),
+ "wednesday" | "wed" => format!("0 0 {} * * 3 *", hour),
+ "thursday" | "thu" => format!("0 0 {} * * 4 *", hour),
+ "friday" | "fri" => format!("0 0 {} * * 5 *", hour),
+ "saturday" | "sat" => format!("0 0 {} * * 6 *", hour),
+ "sunday" | "sun" => format!("0 0 {} * * 0 *", hour),
+ _ => return None,
+ };
+
+ CronSchedule::from_str(&cron_str).ok()
+}
+
+/// Parse "9am", "12pm", "6pm", "14", "9" into a 24-hour number.
+fn parse_time_str(s: &str) -> Option<u32> {
+ let s = s.trim().to_lowercase();
+ if let Some(h) = s.strip_suffix("am") {
+ let n: u32 = h.trim().parse().ok()?;
+ if n == 12 { Some(0) } else if n <= 12 { Some(n) } else { None }
+ } else if let Some(h) = s.strip_suffix("pm") {
+ let n: u32 = h.trim().parse().ok()?;
+ if n == 12 { Some(12) } else if n <= 12 { Some(n + 12) } else { None }
+ } else {
+ let n: u32 = s.parse().ok()?;
+ if n < 24 { Some(n) } else { None }
+ }
+}
+
/// Check if a pipe should run now given its schedule and last run time.
fn should_run(schedule: &str, last_run: DateTime<Utc>) -> bool {
match parse_schedule(schedule) {
@@ -4270,6 +4321,47 @@ mod tests {
}
}
+ #[test]
+ fn test_parse_schedule_human_daily() {
+ match parse_schedule("every day at 9am") {
+ Some(ParsedSchedule::Cron(_)) => {}
+ other => panic!("expected cron for 'every day at 9am', got {:?}", other.is_some()),
+ }
+ match parse_schedule("every day at 12pm") {
+ Some(ParsedSchedule::Cron(_)) => {}
+ other => panic!("expected cron for 'every day at 12pm', got {:?}", other.is_some()),
+ }
+ match parse_schedule("every day at 6pm") {
+ Some(ParsedSchedule::Cron(_)) => {}
+ other => panic!("expected cron for 'every day at 6pm', got {:?}", other.is_some()),
+ }
+ }
+
+ #[test]
+ fn test_parse_schedule_human_weekday() {
+ match parse_schedule("every monday at 9am") {
+ Some(ParsedSchedule::Cron(_)) => {}
+ other => panic!("expected cron for 'every monday at 9am', got {:?}", other.is_some()),
+ }
+ }
+
+ #[test]
+ fn test_should_run_human_daily() {
+ // "every day at 9am" should fire if last run was yesterday
+ let yesterday = Utc::now() - chrono::Duration::hours(25);
+ assert!(should_run("every day at 9am", yesterday));
+ }
+
+ #[test]
+ fn test_parse_time_str() {
+ assert_eq!(parse_time_str("9am"), Some(9));
+ assert_eq!(parse_time_str("12pm"), Some(12));
+ assert_eq!(parse_time_str("12am"), Some(0));
+ assert_eq!(parse_time_str("6pm"), Some(18));
+ assert_eq!(parse_time_str("1pm"), Some(13));
+ assert_eq!(parse_time_str("14"), Some(14));
+ }
+
#[test]
fn test_parse_schedule_garbage() {
assert!(parse_schedule("not a schedule").is_none());