Files
jrshikoku/lib/timeUtils.ts
harukin-expo-dev-env 20c18713e2 refactor: replace Date with dayjs for consistent date handling across components
- Updated FelicaHistoryPage to use dayjs for formatting scannedAt timestamps.
- Refactored ResearchToolsSettings to utilize dayjs for date formatting.
- Modified VoicepeakDebugLogSection to replace Date with dayjs for createdAt timestamps.
- Changed settings component to use dayjs for scannedAt timestamps.
- Updated ExGridSimpleView and ExGridView to use dayjs for time parsing and formatting.
- Refactored ExGridSimpleViewItem and ExGridViewItem to utilize dayjs for time formatting.
- Adjusted ListView and ListViewItem to use dayjs for time handling.
- Updated EachData to use dayjs for train time calculations.
- Refactored LED_vidion to remove redundant getServiceMinute function.
- Replaced Date with dayjs in trainRecorder for exportedAt timestamps.
- Updated appLifecycleCrashSentinel to use dayjs for timestamp handling.
- Refactored operationInfoSchedule to use dayjs for fetchedAt timestamp.
- Updated trainTimeFiltering to utilize dayjs for time comparisons.
- Refactored useWebViewRemount to handle timeouts with dayjs.
- Updated voicepeak and voicepeakDebugLog to use dayjs for timestamp handling.
- Introduced timeUtils module for consistent time parsing and formatting using dayjs.
2026-08-16 09:23:54 +00:00

75 lines
2.1 KiB
TypeScript

import dayjs, { type Dayjs } from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);
const CLOCK_DATE = "2000-01-01";
const CLOCK_TIME_FORMATS = [
"YYYY-MM-DD H:mm",
"YYYY-MM-DD HH:mm",
];
/** Parse a timetable clock value strictly as H:mm or HH:mm. */
export const parseClockTime = (time: string): Dayjs | null => {
const value = time?.trim();
if (!value) return null;
const parsed = dayjs(`${CLOCK_DATE} ${value}`, CLOCK_TIME_FORMATS, true);
return parsed.isValid() ? parsed : null;
};
/** Normalize a timetable clock value to HH:mm, preserving invalid input. */
export const normalizeTime = (time: string) =>
parseClockTime(time)?.format("HH:mm") ?? time;
/** Set a clock value on a base date without applying service-day rollover. */
export const setClockTime = (base: Dayjs, time: string, delayMinutes = 0) => {
const parsed = parseClockTime(time);
if (!parsed) return null;
return base
.clone()
.hour(parsed.hour())
.minute(parsed.minute())
.second(0)
.millisecond(0)
.add(delayMinutes, "minute");
};
/** Set a timetable value on a base date, treating 00:00-03:59 as service-day late night. */
export const setServiceTime = (base: Dayjs, time: string, delayMinutes = 0) => {
const parsed = parseClockTime(time);
if (!parsed) return null;
return base
.clone()
.hour(parsed.hour() < 4 ? parsed.hour() + 24 : parsed.hour())
.minute(parsed.minute())
.second(0)
.millisecond(0)
.add(delayMinutes, "minute");
};
export const getServiceTimeDifference = (
base: Dayjs,
time: string,
delayMinutes = 0
) => {
const parsed = parseClockTime(time);
const target = setServiceTime(base, time, delayMinutes);
if (!parsed || !target) return null;
const adjustedTarget =
base.hour() < 4 && parsed.hour() < 4
? target.subtract(1, "day")
: target;
return adjustedTarget.diff(base, "minute");
};
export const getServiceMinute = (time: string) => {
const parsed = parseClockTime(time);
if (!parsed) return null;
return (parsed.hour() < 4 ? parsed.hour() + 24 : parsed.hour()) * 60 + parsed.minute();
};