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 askama::Template;
use chrono::{NaiveDate, Utc};
use serde::Deserialize;
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)]
pub struct CalendarQuery {

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use std::fmt::Display;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
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> {
let char = if first { '?' } else { '&' };
@ -73,38 +73,14 @@ pub fn show_tree(f: &UserFunction) -> askama::Result<String> {
Ok(html.into_string())
}
pub fn dt_f(v: &NaiveDateTime) -> askama::Result<String> {
Ok(v.format("%Y-%m-%dT%H:%M").to_string())
pub fn fmt_date(v: &NaiveDate, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format(format.into()).to_string())
}
pub fn dt_ff(v: &NaiveDateTime) -> askama::Result<String> {
Ok(v.format("%d.%m.%Y %H:%M").to_string())
pub fn fmt_datetime(v: &NaiveDateTime, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format(format.into()).to_string())
}
pub fn date_d(v: &NaiveDate) -> askama::Result<String> {
Ok(v.format("%d.%m.%Y").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())
pub fn fmt_time(v: &NaiveTime, format: DateTimeFormat) -> askama::Result<String> {
Ok(v.format(format.into()).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;
pub mod auth;
mod date_time_format;
pub mod event_planning_template;
pub mod manage_commands;
pub mod password_change;
@ -10,10 +11,11 @@ pub mod token_generation;
pub mod test_helper;
pub use application_error::ApplicationError;
use chrono::{NaiveDate, Utc};
pub use date_time_format::DateTimeFormat;
pub use template_response_trait::TemplateResponse;
use chrono::{NaiveDate, Utc};
pub fn get_return_url_for_date(date: &NaiveDate) -> String {
let today = Utc::now().date_naive();
if date == &today {

View File

@ -5,7 +5,8 @@
<div class="container">
{% let is_edit = id.is_some() %}
<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="enddate" value="{{ date }}" id="enddate">
@ -22,7 +23,8 @@
<p class="help">noch mögliche Zeiträume:</p>
<div class="tags help">
{% 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 %}
</div>
{% endif %}
@ -32,7 +34,7 @@
_='on change put the value of me into #et
then if (value of the previous <input/>) is greater than (value of me)
then set the value of #enddate to "{{ datetomorrow }}"
then put "{{ datetomorrow|date_c }}" into #ed
then put "{{ datetomorrow|fmt_date(DayMonth) }}" into #ed
end' />
</div>
</div>
@ -48,12 +50,14 @@
<div class="control">
<label class="radio">
<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
</label>
<label class="radio ml-3">
<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
</label>
</div>
@ -75,9 +79,11 @@
<svg class="icon is-small">
<use href="/static/feather-sprite.svg#info" />
</svg>
verfügbar von {{ date|date_c }} <span id="st">{{ start|time_opt("10:00")|safe }}</span> Uhr
bis <span id="ed">{{ enddate.as_ref().unwrap_or(date)|date_c }}</span>
<span id="et">{{ end|time_opt("20:00")|safe }}</span> Uhr
{% let start_time = start.unwrap_or(NaiveTime::from_hms_opt(10, 0, 0).unwrap()) %}
{% let end_time = start.unwrap_or(NaiveTime::from_hms_opt(20, 0, 0).unwrap()) %}
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>
</div>
</div>

View File

@ -7,7 +7,7 @@
{% if let Some(name) = name %}
<h1 class="title">Event '{{ name }}' bearbeiten</h1>
{% 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 %}
<input type="hidden" name="date" value="{{ date }}">
@ -74,12 +74,12 @@
<div class="field-body">
<div class="field">
<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 class="field">
{% let tomorrow = date.checked_add_days(Days::new(1)).unwrap() %}
<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" />
</div>
</div>

View File

@ -7,7 +7,7 @@
<div class="level">
<div class="level-left">
<a hx-boost="true" class="button is-link level-item"
href="/?date={{ date.pred() }}{{ selected_area|show_area_query(false) }}">
href="/?date={{ date.pred() }}{{ selected_area|show_area_query(false) }}">
<svg class="icon">
<use href="/static/feather-sprite.svg#arrow-left" />
</svg>
@ -16,7 +16,7 @@
<div class="control level-item is-flex-grow-0">
<input class="input" type="date" name="date" value="{{ date }}" hx-target="closest body"
hx-get="/{{ selected_area|show_area_query(true) }}" hx-push-url="true">
hx-get="/{{ selected_area|show_area_query(true) }}" hx-push-url="true">
{% if user.role == Role::Admin %}
<div class="select ml-2">
@ -36,7 +36,7 @@
<div class="level-right">
<a hx-boost="true" class="button is-link level-item"
href="/?date={{ date.succ() }}{{ selected_area|show_area_query(false) }}">
href="/?date={{ date.succ() }}{{ selected_area|show_area_query(false) }}">
<svg class="icon">
<use href="/static/feather-sprite.svg#arrow-right" />
</svg>
@ -53,7 +53,7 @@
<div class="level">
<div class="level-left">
<h3 class="title is-3">
Events am {{ date|date_d }}
Events am {{ date|fmt_date(DayMonthYear) }}
</h3>
</div>
{% if user.role == Role::Admin || user.role == Role::AreaManager && (selected_area.is_none() ||
@ -106,7 +106,8 @@
{% endif %}
<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 class="cell">
@ -180,14 +181,14 @@
<div class="level">
<div class="level-left">
<h3 class="title is-3">
Verfügbarkeiten am {{ date|date_d }}
Verfügbarkeiten am {{ date|fmt_date(DayMonthYear) }}
</h3>
</div>
{% if selected_area.is_none() || selected_area.unwrap() == user.area_id %}
<div class="level-right">
{% let btn_disabled = !user_can_create_availability %}
<button class="button is-link is-light" hx-get="/availability/new?date={{ date }}" {{
btn_disabled|cond_show("disabled") }} hx-target="closest body">
btn_disabled|cond_show("disabled") }} hx-target="closest body">
<svg class="icon">
<use href="/static/feather-sprite.svg#plus-circle" />
</svg>
@ -222,7 +223,8 @@
{{ u.function|show_tree|safe }}
</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>
{{ availability.comment.as_deref().unwrap_or("") }}
@ -231,14 +233,14 @@
{% if availability.user_id == user.id %}
<div class="buttons is-right">
<a class="button is-primary is-light" hx-boost="true"
href="/availability/edit/{{ availability.id }}">
href="/availability/edit/{{ availability.id }}">
<svg class="icon">
<use href="/static/feather-sprite.svg#edit" />
</svg>
<span>Bearbeiten</span>
</a>
<button class="button is-danger is-light" hx-delete="/availability/delete/{{ availability.id }}"
hx-target="closest tr" hx-swap="delete" hx-trigger="confirmed">
hx-target="closest tr" hx-swap="delete" hx-trigger="confirmed">
<svg class="icon">
<use href="/static/feather-sprite.svg#x-circle" />
</svg>