refactor: date time formatting

This commit is contained in:
Max Hohlfeld 2025-05-13 11:07:27 +02:00
parent 55291d1cb0
commit c9b075216a
9 changed files with 76 additions and 66 deletions

View File

@ -1,18 +1,19 @@
use crate::{
filters,
models::{find_free_date_time_slots, Assignment, Function, Vehicle},
utils::{
event_planning_template::generate_vehicles_assigned_and_available, ApplicationError,
TemplateResponse,
},
};
use actix_web::{web, HttpResponse, Responder}; use actix_web::{web, HttpResponse, Responder};
use askama::Template; use askama::Template;
use chrono::{NaiveDate, Utc}; use chrono::{NaiveDate, Utc};
use serde::Deserialize; use serde::Deserialize;
use sqlx::PgPool; use sqlx::PgPool;
use crate::models::{Area, Availability, Event, Role, User}; use crate::filters;
use crate::models::{
find_free_date_time_slots, Area, Assignment, Availability, Event, Function, Role, User, Vehicle,
};
use crate::utils::{
event_planning_template::generate_vehicles_assigned_and_available,
ApplicationError,
DateTimeFormat::{DayMonthYear, DayMonthYearHourMinute, HourMinute},
TemplateResponse,
};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct CalendarQuery { pub struct CalendarQuery {

View File

@ -1,9 +1,10 @@
use crate::filters;
use askama::Template; use askama::Template;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use serde::Deserialize; use serde::Deserialize;
use crate::filters;
use crate::models::{Availability, AvailabilityChangeset, Role, User}; use crate::models::{Availability, AvailabilityChangeset, Role, User};
use crate::utils::DateTimeFormat::{DayMonth, DayMonthYear, DayMonthYearHourMinute};
pub mod delete; pub mod delete;
pub mod get_new; pub mod get_new;
@ -23,7 +24,7 @@ struct NewOrEditAvailabilityTemplate<'a> {
end: Option<NaiveTime>, end: Option<NaiveTime>,
comment: Option<&'a str>, comment: Option<&'a str>,
slot_suggestions: Vec<(NaiveDateTime, NaiveDateTime)>, slot_suggestions: Vec<(NaiveDateTime, NaiveDateTime)>,
datetomorrow: NaiveDate datetomorrow: NaiveDate,
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View File

@ -1,10 +1,11 @@
use crate::filters;
use askama::Template; use askama::Template;
use chrono::{Days, NaiveDateTime}; use chrono::{Days, NaiveDateTime};
use chrono::{NaiveDate, NaiveTime}; use chrono::{NaiveDate, NaiveTime};
use serde::Deserialize; use serde::Deserialize;
use crate::filters;
use crate::models::{Location, Role, User}; use crate::models::{Location, Role, User};
use crate::utils::DateTimeFormat::{DayMonthYear, HourMinute, YearMonthDayTHourMinute};
pub mod delete; pub mod delete;
pub mod get_edit; pub mod get_edit;

View File

@ -3,7 +3,7 @@ use std::fmt::Display;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use maud::html; use maud::html;
use crate::models::UserFunction; use crate::{models::UserFunction, utils::DateTimeFormat};
pub fn show_area_query(a: &Option<i32>, first: bool) -> askama::Result<String> { pub fn show_area_query(a: &Option<i32>, first: bool) -> askama::Result<String> {
let char = if first { '?' } else { '&' }; let char = if first { '?' } else { '&' };
@ -73,38 +73,14 @@ pub fn show_tree(f: &UserFunction) -> askama::Result<String> {
Ok(html.into_string()) Ok(html.into_string())
} }
pub fn dt_f(v: &NaiveDateTime) -> askama::Result<String> { pub fn fmt_date(v: &NaiveDate, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format("%Y-%m-%dT%H:%M").to_string()) Ok(v.format(format.into()).to_string())
} }
pub fn dt_ff(v: &NaiveDateTime) -> askama::Result<String> { pub fn fmt_datetime(v: &NaiveDateTime, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format("%d.%m.%Y %H:%M").to_string()) Ok(v.format(format.into()).to_string())
} }
pub fn date_d(v: &NaiveDate) -> askama::Result<String> { pub fn fmt_time(v: &NaiveTime, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format("%d.%m.%Y").to_string()) Ok(v.format(format.into()).to_string())
}
pub fn date_c(v: &NaiveDate) -> askama::Result<String> {
Ok(v.format("%d.%m").to_string())
}
pub fn date_c_and_time(v: &NaiveDateTime) -> askama::Result<String> {
Ok(v.format("%d.%m. %H:%M").to_string())
}
pub fn time(v: &NaiveTime) -> askama::Result<String> {
Ok(v.format("%H:%M").to_string())
}
pub fn time_opt(v: &Option<NaiveTime>, default: &str) -> askama::Result<String> {
if let Some(t) = v {
return time(t);
}
Ok(default.to_string())
}
pub fn dt_t(v: &NaiveDateTime) -> askama::Result<String> {
Ok(v.format("%R").to_string())
} }

View File

@ -0,0 +1,21 @@
pub enum DateTimeFormat {
DayMonth,
DayMonthHourMinute,
DayMonthYear,
DayMonthYearHourMinute,
YearMonthDayTHourMinute,
HourMinute
}
impl From<DateTimeFormat> for &'static str {
fn from(value: DateTimeFormat) -> Self {
match value {
DateTimeFormat::DayMonth => "%d.%m.",
DateTimeFormat::DayMonthHourMinute => "%d.%m. %H:%M",
DateTimeFormat::DayMonthYear => "%d.%m.%Y",
DateTimeFormat::DayMonthYearHourMinute => "%d.%m.%Y %H:%M",
DateTimeFormat::YearMonthDayTHourMinute => "%Y-%m-%dT%H:%M",
DateTimeFormat::HourMinute => "%H:%M" // equivalent to %R,
}
}
}

View File

@ -1,5 +1,6 @@
mod application_error; mod application_error;
pub mod auth; pub mod auth;
mod date_time_format;
pub mod event_planning_template; pub mod event_planning_template;
pub mod manage_commands; pub mod manage_commands;
pub mod password_change; pub mod password_change;
@ -10,10 +11,11 @@ pub mod token_generation;
pub mod test_helper; pub mod test_helper;
pub use application_error::ApplicationError; pub use application_error::ApplicationError;
use chrono::{NaiveDate, Utc}; pub use date_time_format::DateTimeFormat;
pub use template_response_trait::TemplateResponse; pub use template_response_trait::TemplateResponse;
use chrono::{NaiveDate, Utc};
pub fn get_return_url_for_date(date: &NaiveDate) -> String { pub fn get_return_url_for_date(date: &NaiveDate) -> String {
let today = Utc::now().date_naive(); let today = Utc::now().date_naive();
if date == &today { if date == &today {

View File

@ -5,7 +5,8 @@
<div class="container"> <div class="container">
{% let is_edit = id.is_some() %} {% let is_edit = id.is_some() %}
<form method="post" action="/availability/{% if is_edit %}edit/{{ id.unwrap() }}{% else %}new{% endif %}"> <form method="post" action="/availability/{% if is_edit %}edit/{{ id.unwrap() }}{% else %}new{% endif %}">
<h1 class="title">{% if is_edit %}Bearbeite{% else %}Neue{% endif %} Vefügbarkeit für den {{ date|date_d }}</h1> <h1 class="title">{% if is_edit %}Bearbeite{% else %}Neue{% endif %} Vefügbarkeit für den {{
date|fmt_date(DayMonthYear) }}</h1>
<input type="hidden" name="startdate" value="{{ date }}"> <input type="hidden" name="startdate" value="{{ date }}">
<input type="hidden" name="enddate" value="{{ date }}" id="enddate"> <input type="hidden" name="enddate" value="{{ date }}" id="enddate">
@ -22,7 +23,8 @@
<p class="help">noch mögliche Zeiträume:</p> <p class="help">noch mögliche Zeiträume:</p>
<div class="tags help"> <div class="tags help">
{% for (s, e) in slot_suggestions %} {% for (s, e) in slot_suggestions %}
<span class="tag">{{ *s|date_c_and_time }} - {{ *e|date_c_and_time }}</span> <span class="tag">{{ s|fmt_datetime(DayMonthYearHourMinute) }} -
{{ e|fmt_datetime(DayMonthYearHourMinute) }}</span>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
@ -32,7 +34,7 @@
_='on change put the value of me into #et _='on change put the value of me into #et
then if (value of the previous <input/>) is greater than (value of me) then if (value of the previous <input/>) is greater than (value of me)
then set the value of #enddate to "{{ datetomorrow }}" then set the value of #enddate to "{{ datetomorrow }}"
then put "{{ datetomorrow|date_c }}" into #ed then put "{{ datetomorrow|fmt_date(DayMonth) }}" into #ed
end' /> end' />
</div> </div>
</div> </div>
@ -48,12 +50,14 @@
<div class="control"> <div class="control">
<label class="radio"> <label class="radio">
<input type="radio" name="isovernight" {{ is_overnight|invert|ref|cond_show("checked")|safe }} <input type="radio" name="isovernight" {{ is_overnight|invert|ref|cond_show("checked")|safe }}
_='on click set the value of #enddate to "{{ date }}" then put "{{ date|date_c }}" into #ed'> _='on click set the value of #enddate to "{{ date }}"
then put "{{ date|fmt_date(DayMonth) }}" into #ed'>
am selben Tag am selben Tag
</label> </label>
<label class="radio ml-3"> <label class="radio ml-3">
<input type="radio" name="isovernight" {{ is_overnight|cond_show("checked")|safe }} <input type="radio" name="isovernight" {{ is_overnight|cond_show("checked")|safe }}
_='on click set the value of #enddate to "{{ datetomorrow }}" then put "{{ datetomorrow|date_c }}" into #ed'> _='on click set the value of #enddate to "{{ datetomorrow }}"
then put "{{ datetomorrow|fmt_date(DayMonth) }}" into #ed'>
am Tag darauf am Tag darauf
</label> </label>
</div> </div>
@ -75,9 +79,11 @@
<svg class="icon is-small"> <svg class="icon is-small">
<use href="/static/feather-sprite.svg#info" /> <use href="/static/feather-sprite.svg#info" />
</svg> </svg>
verfügbar von {{ date|date_c }} <span id="st">{{ start|time_opt("10:00")|safe }}</span> Uhr {% let start_time = start.unwrap_or(NaiveTime::from_hms_opt(10, 0, 0).unwrap()) %}
bis <span id="ed">{{ enddate.as_ref().unwrap_or(date)|date_c }}</span> {% let end_time = start.unwrap_or(NaiveTime::from_hms_opt(20, 0, 0).unwrap()) %}
<span id="et">{{ end|time_opt("20:00")|safe }}</span> Uhr verfügbar von {{ date|fmt_date(DayMonth) }} <span id="st">{{ start_time|fmt_time(DayMonth) }}</span> Uhr
bis <span id="ed">{{ enddate.as_ref().unwrap_or(date)|fmt_date(DayMonth) }}</span>
<span id="et">{{ end_time|fmt_time(DayMonth) }}</span> Uhr
</p> </p>
</div> </div>
</div> </div>

View File

@ -7,7 +7,7 @@
{% if let Some(name) = name %} {% if let Some(name) = name %}
<h1 class="title">Event '{{ name }}' bearbeiten</h1> <h1 class="title">Event '{{ name }}' bearbeiten</h1>
{% else %} {% else %}
<h1 class="title">Neues Event anlegen für den {{ date|date_d }}</h1> <h1 class="title">Neues Event anlegen für den {{ date|fmt_date(DayMonthYear) }}</h1>
{% endif %} {% endif %}
<input type="hidden" name="date" value="{{ date }}"> <input type="hidden" name="date" value="{{ date }}">
@ -74,12 +74,12 @@
<div class="field-body"> <div class="field-body">
<div class="field"> <div class="field">
<input class="input" type="time" name="start" required <input class="input" type="time" name="start" required
value="{{ start|time }}" {{ canceled|cond_show("disabled") }} /> value="{{ start|fmt_time(HourMinute) }}" {{ canceled|cond_show("disabled") }} />
</div> </div>
<div class="field"> <div class="field">
{% let tomorrow = date.checked_add_days(Days::new(1)).unwrap() %} {% let tomorrow = date.checked_add_days(Days::new(1)).unwrap() %}
<input class="input" type="datetime-local" name="end" required <input class="input" type="datetime-local" name="end" required
value="{{ end|dt_f }}" {{ canceled|cond_show("disabled") }} value="{{ end|fmt_datetime(YearMonthDayTHourMinute) }}" {{ canceled|cond_show("disabled") }}
min="{{ date }}T00:00" max="{{ tomorrow }}T23:59" /> min="{{ date }}T00:00" max="{{ tomorrow }}T23:59" />
</div> </div>
</div> </div>

View File

@ -53,7 +53,7 @@
<div class="level"> <div class="level">
<div class="level-left"> <div class="level-left">
<h3 class="title is-3"> <h3 class="title is-3">
Events am {{ date|date_d }} Events am {{ date|fmt_date(DayMonthYear) }}
</h3> </h3>
</div> </div>
{% if user.role == Role::Admin || user.role == Role::AreaManager && (selected_area.is_none() || {% if user.role == Role::Admin || user.role == Role::AreaManager && (selected_area.is_none() ||
@ -106,7 +106,8 @@
{% endif %} {% endif %}
<div class="cell"> <div class="cell">
<p><b>Uhrzeit:</b> {{ event.start|dt_t }} Uhr - {{ event.end|dt_ff }} Uhr</p> <p><b>Uhrzeit:</b> {{ event.start|fmt_datetime(HourMinute) }} Uhr -
{{ event.end|fmt_datetime(DayMonthYearHourMinute) }} Uhr</p>
</div> </div>
<div class="cell"> <div class="cell">
@ -180,7 +181,7 @@
<div class="level"> <div class="level">
<div class="level-left"> <div class="level-left">
<h3 class="title is-3"> <h3 class="title is-3">
Verfügbarkeiten am {{ date|date_d }} Verfügbarkeiten am {{ date|fmt_date(DayMonthYear) }}
</h3> </h3>
</div> </div>
{% if selected_area.is_none() || selected_area.unwrap() == user.area_id %} {% if selected_area.is_none() || selected_area.unwrap() == user.area_id %}
@ -222,7 +223,8 @@
{{ u.function|show_tree|safe }} {{ u.function|show_tree|safe }}
</td> </td>
<td> <td>
{{ availability.start|dt_t }} Uhr bis {{ availability.end|dt_ff }} Uhr {{ availability.start|fmt_datetime(HourMinute) }} Uhr bis
{{ availability.end|fmt_datetime(DayMonthYearHourMinute) }} Uhr
</td> </td>
<td> <td>
{{ availability.comment.as_deref().unwrap_or("") }} {{ availability.comment.as_deref().unwrap_or("") }}