[mod] helpers:date formatting

This commit is contained in:
Fenris Wolf 2024-10-03 11:07:39 +02:00
parent d7aaeb153a
commit 801eddb661

View file

@ -75,6 +75,42 @@ namespace _zeitbild.frontend_web.helpers
} }
/**
* @todo timezone
*/
export function date_format(
date : lib_plankton.pit.type_date
) : string
{
return lib_plankton.string.coin(
"{{year}}-{{month}}-{{day}}",
{
"year": date.year.toFixed(0).padStart(4, "0"),
"month": date.month.toFixed(0).padStart(2, "0"),
"day": date.day.toFixed(0).padStart(2, "0"),
}
);
}
/**
* @todo timezone
*/
export function time_format(
time : lib_plankton.pit.type_time
) : string
{
return lib_plankton.string.coin(
"{{hour}}:{{minute}}",
{
"hour": time.hour.toFixed(0).padStart(2, "0"),
"minute": time.minute.toFixed(0).padStart(2, "0"),
"second": time.second.toFixed(0).padStart(2, "0"),
}
);
}
/** /**
* @todo timezone * @todo timezone
*/ */
@ -89,14 +125,7 @@ namespace _zeitbild.frontend_web.helpers
return lib_plankton.string.coin( return lib_plankton.string.coin(
"{{date}}{{macro_time}}", "{{date}}{{macro_time}}",
{ {
"date": lib_plankton.string.coin( "date": date_format(datetime.date),
"{{year}}-{{month}}-{{day}}",
{
"year": datetime.date.year.toFixed(0).padStart(4, "0"),
"month": datetime.date.month.toFixed(0).padStart(2, "0"),
"day": datetime.date.day.toFixed(0).padStart(2, "0"),
}
),
"macro_time": ( "macro_time": (
(datetime.time === null) (datetime.time === null)
? ?
@ -105,14 +134,7 @@ namespace _zeitbild.frontend_web.helpers
lib_plankton.string.coin( lib_plankton.string.coin(
",{{time}}", ",{{time}}",
{ {
"time": lib_plankton.string.coin( "time": time_format(datetime.time),
"{{hour}}:{{minute}}",
{
"hour": datetime.time.hour.toFixed(0).padStart(2, "0"),
"minute": datetime.time.minute.toFixed(0).padStart(2, "0"),
"second": datetime.time.second.toFixed(0).padStart(2, "0"),
}
),
} }
) )
), ),
@ -129,24 +151,28 @@ namespace _zeitbild.frontend_web.helpers
to : (null | lib_plankton.pit.type_datetime) to : (null | lib_plankton.pit.type_datetime)
) : string ) : string
{ {
return lib_plankton.string.coin( let result : string = "";
"{{from}}{{macro_to}}", result += datetime_format(from);
{ if (to === null) {
"from": datetime_format(from), // do nothing
"macro_to": ( }
(to === null) else {
? result += " - ";
"" if (
: (from.date.year === to.date.year)
lib_plankton.string.coin( &&
" - {{to}}", (from.date.month === to.date.month)
{ &&
"to": datetime_format(to), (from.date.day === to.date.day)
} ) {
) // only time
), result += time_format(to.time);
} }
); else {
result += datetime_format(to);
}
}
return result;
} }
/** /**