Fix timezones

This commit is contained in:
jude 2024-03-01 17:54:05 +00:00
parent e83b643d86
commit 72af0532fa

View File

@ -17,7 +17,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
const [timezone] = useTimezone(); const [timezone] = useTimezone();
const [time, setTime] = useState( const [time, setTime] = useState(
defaultValue ? DateTime.fromISO(defaultValue).setZone(timezone) : null, defaultValue ? DateTime.fromISO(defaultValue, { zone: "UTC" }) : null,
); );
const updateTime = useCallback( const updateTime = useCallback(
@ -28,7 +28,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
let newTime = time; let newTime = time;
if (newTime === null) { if (newTime === null) {
newTime = DateTime.now().setZone(timezone); newTime = DateTime.now().setZone("UTC");
} }
setTime(newTime.set(upd)); setTime(newTime.set(upd));
}, },
@ -36,7 +36,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
); );
useEffect(() => { useEffect(() => {
onInput(time?.toFormat("yyyy-LL-dd'T'HH:mm:ss")); onInput(time?.setZone("UTC").toFormat("yyyy-LL-dd'T'HH:mm:ss"));
}, [time]); }, [time]);
const flash = useFlash(); const flash = useFlash();
@ -48,7 +48,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
onPaste={(ev) => { onPaste={(ev) => {
ev.preventDefault(); ev.preventDefault();
const pasteValue = ev.clipboardData.getData("text/plain"); const pasteValue = ev.clipboardData.getData("text/plain");
let dt = DateTime.fromISO(pasteValue); let dt = DateTime.fromISO(pasteValue, { zone: timezone });
if (dt.isValid) { if (dt.isValid) {
setTime(dt); setTime(dt);
@ -84,7 +84,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="YYYY" placeholder="YYYY"
value={ value={
time time
? time.year.toLocaleString("en-US", { ? time.setZone(timezone).year.toLocaleString("en-US", {
minimumIntegerDigits: 4, minimumIntegerDigits: 4,
useGrouping: false, useGrouping: false,
}) })
@ -115,7 +115,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="MM" placeholder="MM"
value={ value={
time time
? time.month.toLocaleString("en-US", { ? time.setZone(timezone).month.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -145,7 +145,9 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="DD" placeholder="DD"
value={ value={
time time
? time.day.toLocaleString("en-US", { minimumIntegerDigits: 2 }) ? time
.setZone(timezone)
.day.toLocaleString("en-US", { minimumIntegerDigits: 2 })
: "" : ""
} }
onBlur={(ev) => { onBlur={(ev) => {
@ -172,7 +174,9 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="hh" placeholder="hh"
value={ value={
time time
? time.hour.toLocaleString("en-US", { minimumIntegerDigits: 2 }) ? time
.setZone(timezone)
.hour.toLocaleString("en-US", { minimumIntegerDigits: 2 })
: "" : ""
} }
onBlur={(ev) => { onBlur={(ev) => {
@ -200,7 +204,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="mm" placeholder="mm"
value={ value={
time time
? time.minute.toLocaleString("en-US", { ? time.setZone(timezone).minute.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -230,7 +234,7 @@ export const TimeInput = ({ defaultValue, onInput }) => {
placeholder="ss" placeholder="ss"
value={ value={
time time
? time.second.toLocaleString("en-US", { ? time.setZone(timezone).second.toLocaleString("en-US", {
minimumIntegerDigits: 2, minimumIntegerDigits: 2,
}) })
: "" : ""
@ -279,8 +283,12 @@ export const TimeInput = ({ defaultValue, onInput }) => {
ref={ref} ref={ref}
onInput={(ev) => { onInput={(ev) => {
ev.currentTarget.value === "" ev.currentTarget.value === ""
? setTime(null) ? updateTime(null)
: setTime(DateTime.fromISO(ev.currentTarget.value)); : setTime(
DateTime.fromISO(ev.currentTarget.value, { zone: timezone }).setZone(
"UTC",
),
);
}} }}
></input> ></input>
</> </>