mirror of
https://github.com/jhbruhn/ics-adapter.git
synced 2025-03-15 03:25:49 +00:00
Fix filter time to start at 00:00
This commit is contained in:
parent
2a2821c5b2
commit
868d27a113
1 changed files with 4 additions and 12 deletions
16
src/main.rs
16
src/main.rs
|
@ -1,4 +1,3 @@
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
use warp::{
|
use warp::{
|
||||||
Filter,
|
Filter,
|
||||||
Reply,
|
Reply,
|
||||||
|
@ -9,14 +8,6 @@ use anyhow::Result;
|
||||||
use icalendar::{Component, Calendar, EventLike};
|
use icalendar::{Component, Calendar, EventLike};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
fn now_timestamp_secs() -> i64 {
|
|
||||||
let start = SystemTime::now();
|
|
||||||
let since_the_epoch = start
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("Time went backwards");
|
|
||||||
(since_the_epoch.as_millis() / 1000).try_into().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handler(params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
async fn handler(params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
|
||||||
match params.get("url") {
|
match params.get("url") {
|
||||||
Some(url) => Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
|
Some(url) => Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
|
||||||
|
@ -54,8 +45,8 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
|
||||||
|
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
|
|
||||||
let filter_start = now_timestamp_secs();
|
let filter_start = chrono::Utc::now().date_naive().and_hms_opt(0, 0, 0).unwrap().and_utc();
|
||||||
let filter_end = now_timestamp_secs() + (24 * 60 * 60) * days.unwrap_or(&String::from("1")).parse().unwrap_or(1) as i64;
|
let filter_end = filter_start + chrono::Duration::days(days.unwrap_or(&String::from("1")).parse().unwrap_or(1));
|
||||||
|
|
||||||
for event in calendar.components {
|
for event in calendar.components {
|
||||||
if let Some(event) = event.as_event() {
|
if let Some(event) = event.as_event() {
|
||||||
|
@ -85,9 +76,10 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
|
||||||
None => start + chrono::Duration::days(1),
|
None => start + chrono::Duration::days(1),
|
||||||
};
|
};
|
||||||
|
|
||||||
if start.timestamp() < filter_start || start.timestamp() > filter_end {
|
if start < filter_start || start > filter_end {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
entries.push(CustomCalendarEntry {
|
entries.push(CustomCalendarEntry {
|
||||||
title: event.get_summary().unwrap_or("").to_string(),
|
title: event.get_summary().unwrap_or("").to_string(),
|
||||||
description: event.get_description().unwrap_or("").to_string(),
|
description: event.get_description().unwrap_or("").to_string(),
|
||||||
|
|
Loading…
Reference in a new issue