Skip to content

Commit

Permalink
🚑 fix date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Platane committed Feb 24, 2024
1 parent de688e9 commit 6468bd5
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions packages/app/components/Lines/FlyingLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { styled } from "@linaria/react";
import React from "react";
import { DateTime } from "luxon";
import type { ILocation } from "@tzr/location-index";
import { css } from "@linaria/core";

type Props = { location: ILocation; t: number };
export const FlyingLabel = () => (
<Container>
<span />
<span style={{ fontSize: "0.72em" }} />
<span />
<span />
<span />
<span />
<span />
</Container>
);

/**
* format the date as string
* split into date / time / literal blocks
*/
const formatDateTime = (timezone: string, t: number) => {
const parts = DateTime.fromMillis(t, { zone: timezone }).toLocaleParts({
minute: "numeric",
Expand All @@ -20,34 +28,63 @@ const formatDateTime = (timezone: string, t: number) => {
day: "numeric",
});

const values = parts.map((x) => x.value);

const a = parts.findIndex(
({ type }) => type === "month" || type === "year" || type === "day"
);
const b =
parts.length -
parts
.reverse()
.findIndex(
({ type }) => type === "month" || type === "year" || type === "day"
);

return [
values.slice(0, a).join(""),
values.slice(a, b).join(""),
values.slice(b).join(""),
];
const getLooseType = (type: Intl.DateTimeFormatPart["type"]) =>
((type === "day" || type === "month" || type === "year") && "date") ||
((type === "hour" || type === "minute" || type === "dayPeriod") &&
"time") ||
"literal";

const looseParts: { type: "date" | "time" | "literal"; text: string }[] = [];

let i = 0;
while (i < parts.length) {
const type = getLooseType(parts[i].type);

let j = i;
for (; j < parts.length; j++) {
const t = getLooseType(parts[j].type);
if (t !== "literal" && t !== type) break;
}

j--;

if (type !== "literal")
while (parts[j] && getLooseType(parts[j].type) === "literal") j--;

let text = "";
for (let k = i; k <= j; k++) text += parts[k].value;

looseParts.push({ type, text });

i = j + 1;
}

return looseParts;
};

export const update = (domElement: Element, { location, t }: Props) => {
const parts = formatDateTime(location.timezone, t);

parts.forEach((text, i) => {
(domElement.children[i] as HTMLElement).innerText = text;
});
for (let i = 0; i < domElement.children.length; i++) {
const el = domElement.children[i] as HTMLElement;
el.innerText = parts[i]?.text ?? "";

el.classList.remove(valueClassName);
el.classList.remove(literalClassName);

if (parts[i]?.type === "literal") el.classList.add(literalClassName);
else el.classList.add(valueClassName);
}
};

const valueClassName = css`
white-space: pre;
`;
const literalClassName = css`
font-size: 0.72em;
white-space: pre;
`;

const Container = styled.div`
padding-left: 4px;
white-space: nowrap;
Expand Down

1 comment on commit 6468bd5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.