Store times as local time not UTC

This commit is contained in:
jude 2024-05-04 10:24:20 +01:00
parent 79da56c794
commit aecf2c15be

View File

@ -16,28 +16,28 @@ export const TimeInput = ({ defaultValue, onInput }) => {
const ref = useRef(null); const ref = useRef(null);
const [timezone] = useTimezone(); const [timezone] = useTimezone();
const [time, setTime] = useState( const [localTime, setLocalTime] = useState(
defaultValue ? DateTime.fromISO(defaultValue, { zone: "UTC" }) : null, defaultValue ? DateTime.fromISO(defaultValue, { zone: "UTC" }).setZone(timezone) : null,
); );
const updateTime = useCallback( const updateTime = useCallback(
(upd: TimeUpdate) => { (upd: TimeUpdate) => {
if (upd === null) { if (upd === null) {
setTime(null); setLocalTime(null);
} }
let newTime = time; let newTime = localTime;
if (newTime === null) { if (newTime === null) {
newTime = DateTime.now().setZone("UTC"); newTime = DateTime.now().setZone(timezone);
} }
setTime(newTime.setZone(timezone).set(upd).setZone("UTC")); setLocalTime(newTime.set(upd));
}, },
[time, timezone], [localTime, timezone],
); );
useEffect(() => { useEffect(() => {
onInput(time?.setZone("UTC").toFormat("yyyy-LL-dd'T'HH:mm:ss")); onInput(localTime?.setZone("UTC").toFormat("yyyy-LL-dd'T'HH:mm:ss"));
}, [time]); }, [localTime]);
const flash = useFlash(); const flash = useFlash();
@ -51,14 +51,14 @@ export const TimeInput = ({ defaultValue, onInput }) => {
let dt = DateTime.fromISO(pasteValue, { zone: timezone }); let dt = DateTime.fromISO(pasteValue, { zone: timezone });
if (dt.isValid) { if (dt.isValid) {
setTime(dt); setLocalTime(dt);
return; return;
} }
dt = DateTime.fromSQL(pasteValue); dt = DateTime.fromSQL(pasteValue);
if (dt.isValid) { if (dt.isValid) {
setTime(dt); setLocalTime(dt);
return; return;
} }
@ -83,8 +83,8 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={4} maxlength={4}
placeholder="YYYY" placeholder="YYYY"
value={ value={
time localTime
? time.setZone(timezone).year.toLocaleString("en-US", { ? localTime.year.toLocaleString("en-US", {
minimumIntegerDigits: 4, minimumIntegerDigits: 4,
useGrouping: false, useGrouping: false,
}) })
@ -114,8 +114,8 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={2} maxlength={2}
placeholder="MM" placeholder="MM"
value={ value={
time localTime
? time.setZone(timezone).month.toLocaleString("en-US", { ? localTime.month.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -144,10 +144,10 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={2} maxlength={2}
placeholder="DD" placeholder="DD"
value={ value={
time localTime
? time ? localTime.day.toLocaleString("en-US", {
.setZone(timezone) minimumIntegerDigits: 2,
.day.toLocaleString("en-US", { minimumIntegerDigits: 2 }) })
: "" : ""
} }
onBlur={(ev) => { onBlur={(ev) => {
@ -173,10 +173,10 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={2} maxlength={2}
placeholder="hh" placeholder="hh"
value={ value={
time localTime
? time ? localTime.hour.toLocaleString("en-US", {
.setZone(timezone) minimumIntegerDigits: 2,
.hour.toLocaleString("en-US", { minimumIntegerDigits: 2 }) })
: "" : ""
} }
onBlur={(ev) => { onBlur={(ev) => {
@ -203,8 +203,8 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={2} maxlength={2}
placeholder="mm" placeholder="mm"
value={ value={
time localTime
? time.setZone(timezone).minute.toLocaleString("en-US", { ? localTime.minute.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -233,8 +233,8 @@ export const TimeInput = ({ defaultValue, onInput }) => {
maxlength={2} maxlength={2}
placeholder="ss" placeholder="ss"
value={ value={
time localTime
? time.setZone(timezone).second.toLocaleString("en-US", { ? localTime.second.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -276,18 +276,16 @@ export const TimeInput = ({ defaultValue, onInput }) => {
type="datetime-local" type="datetime-local"
step="1" step="1"
value={ value={
time localTime
? time.toFormat("yyyy-LL-dd'T'HH:mm:ss") ? localTime.toFormat("yyyy-LL-dd'T'HH:mm:ss")
: DateTime.now().toFormat("yyyy-LL-dd'T'HH:mm:ss") : DateTime.now().setZone(timezone).toFormat("yyyy-LL-dd'T'HH:mm:ss")
} }
ref={ref} ref={ref}
onInput={(ev) => { onInput={(ev) => {
ev.currentTarget.value === "" ev.currentTarget.value === ""
? updateTime(null) ? updateTime(null)
: setTime( : setLocalTime(
DateTime.fromISO(ev.currentTarget.value, { zone: timezone }).setZone( DateTime.fromISO(ev.currentTarget.value, { zone: timezone }),
"UTC",
),
); );
}} }}
></input> ></input>