From 5b5d135bec375ae2d16f5135d2f9e432e93a701f Mon Sep 17 00:00:00 2001 From: goodmice Date: Wed, 1 Jun 2022 14:05:54 +0500 Subject: [PATCH] =?UTF-8?q?TimeOnly=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BD=D0=B0=20TimeDto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Table/TimePickerWrapper.tsx | 14 +++++++------- src/components/Table/sorters.ts | 8 ++++---- src/utils/datetime.ts | 14 ++++++-------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/components/Table/TimePickerWrapper.tsx b/src/components/Table/TimePickerWrapper.tsx index 6ebe6a9..6bcc9a3 100644 --- a/src/components/Table/TimePickerWrapper.tsx +++ b/src/components/Table/TimePickerWrapper.tsx @@ -2,25 +2,25 @@ import { Moment } from 'moment' import { TimePicker, TimePickerProps } from 'antd' import { memo, useCallback, useMemo } from 'react' -import { defaultTimeFormat, momentToTimeOnly, timeOnlyToMoment } from '@utils/datetime' -import { TimeOnly } from '@api' +import { defaultTimeFormat, momentToTime, timeToMoment } from '@utils/datetime' +import { TimeDto } from '@api' export type TimePickerWrapperProps = Omit, 'onChange'> & { - value?: TimeOnly, - onChange?: (date: TimeOnly | null) => any + value?: TimeDto, + onChange?: (date: TimeDto | null) => any isUTC?: boolean } export const TimePickerWrapper = memo(({ value, onChange, isUTC, ...other }) => { - const time = useMemo(() => value ? timeOnlyToMoment(value, isUTC) : null, [value, isUTC]) + const time = useMemo(() => value ? timeToMoment(value, isUTC) : null, [value, isUTC]) - const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTimeOnly(time) : null), [onChange]) + const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTime(time) : null), [onChange]) return ( (key: keyof DataType) => (a: return date.getTime() - new Date(bdate).getTime() } -export const makeTimeSorter = (key: keyof DataType) => (a: DataType, b: DataType) => { +export const makeTimeSorter = (key: keyof DataType) => (a: DataType, b: DataType) => { const elma = a[key] const elmb = b[key] @@ -34,5 +34,5 @@ export const makeTimeSorter = (key: keyof DataType) => (a if (!elma) return 1 if (!elmb) return -1 - return timeOnlyToMoment(elma).diff(timeOnlyToMoment(elmb)) + return timeToMoment(elma).diff(timeToMoment(elmb)) } diff --git a/src/utils/datetime.ts b/src/utils/datetime.ts index cb92700..f4edc4e 100755 --- a/src/utils/datetime.ts +++ b/src/utils/datetime.ts @@ -1,6 +1,6 @@ import moment, { Moment } from 'moment' -import { SimpleTimezoneDto, TimeOnly } from '@api' +import { SimpleTimezoneDto, TimeDto } from '@api' export type RawDate = number | string | Date @@ -20,7 +20,7 @@ export function isRawDate(value: unknown): value is RawDate { return !isNaN(Date.parse(String(value))) } -export function isTimeOnly(value: unknown): value is TimeOnly { +export function isTime(value: unknown): value is TimeDto { if (!value || typeof value !== 'object') return false const keys = Object.keys(value) @@ -34,8 +34,8 @@ export const formatDate = (date: unknown, utc: boolean = false, format: string = } export const formatTime = (time: unknown, utc: boolean = false, format: string = defaultTimeFormat) => { - if(!isTimeOnly(time)) return - const out = timeOnlyToMoment(time, utc, format) + if(!isTime(time)) return + const out = timeToMoment(time, utc, format) return out.format(format) } @@ -92,15 +92,13 @@ export const findTimezoneId = (value: SimpleTimezoneDto): TimezoneId => (isTimezoneId(value.timezoneId) && value.timezoneId) || (Object.keys(rawTimezones) as TimezoneId[]).find(id => rawTimezones[id] === value.hours) as TimezoneId -export const timeOnlyToMoment = (time?: TimeOnly | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => { +export const timeToMoment = (time?: TimeDto | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => { const input = `${time?.hour ?? 0}:${time?.minute ?? 0}:${time?.second ?? 0}` return isUtc ? moment.utc(input, format).local() : moment(input, format) } -export const momentToTimeOnly = (time?: Moment | null): TimeOnly => ({ +export const momentToTime = (time?: Moment | null): TimeDto => ({ hour: time?.hour() ?? 0, minute: time?.minute() ?? 0, second: time?.second() ?? 0, - millisecond: 0, - ticks: 0, })