Todae

Todae

formatTime과 parseTime

formatTime(time, { locale })은 시각을 로캘의 short 시각 스타일로 씁니다. 예: en-US에서는 9:30 PM, de에서는 21:30, ko에서는 오후 9:30. 초가 있으면 초까지 쓰는 medium 스타일로 씁니다. parseTime(text, { locale })은 입력한 텍스트를 Temporal.PlainTime으로 읽고, 시각이 아니면 null을 돌려줍니다. locale이 없으면 둘 다 런타임의 로캘을 씁니다. Field.TimeInput은 이 둘로 텍스트를 읽고 쓰며, timeOptions는 formatTime으로 라벨을 씁니다.

이 페이지의 내용
import { Code, DataList, Field, formatTime, Label, parseTime, Stack } from '@tounsoo/todae';
import { useId, useState } from 'react';

// What parseTime reads from the text, and how formatTime writes it back.
export default function TimeRoundTrip({ locale }: { locale: string }) {
  const id = useId();
  const [text, setText] = useState('2130');
  const value = parseTime(text, { locale });
  return (
    <Stack.Root gap="condensed">
      <Field.Group>
        <Label.Root htmlFor={id}>Opens at</Label.Root>
        <Field.Root id={id}>
          <Field.TextInput value={text} onChange={(event) => setText(event.target.value)} />
        </Field.Root>
      </Field.Group>
      <DataList.Root aria-label="Round trip">
        <DataList.Item>
          <DataList.Term>parseTime</DataList.Term>
          <DataList.Details>
            <Code.Root>{value === null ? 'null' : value.toString()}</Code.Root>
          </DataList.Details>
        </DataList.Item>
        <DataList.Item>
          <DataList.Term>formatTime</DataList.Term>
          <DataList.Details>
            <Code.Root>{value === null ? 'null' : formatTime(value, { locale })}</Code.Root>
          </DataList.Details>
        </DataList.Item>
      </DataList.Root>
    </Stack.Root>
  );
}
parseTime
21:30:00
formatTime
오후 9:30

parseTime이 읽는 것

9, 9:30, 21.30.15, 0930처럼 시, 시와 분, 또는 시와 분과 초를 읽고, 그 앞이나 뒤에 있는 로캘 자체의 하루 중 때를 나타내는 단어도 읽습니다. 예: en-US에서는 9:30 pm, ko에서는 오후 9:30. 대소문자는 상관없고, 숫자는 어느 문자 체계의 숫자여도 되며, 숫자 사이는 어떤 문장 부호, 기호, 공백으로 나누거나, fr-CA의 21 h 30처럼 로캘이 숫자 사이에 쓰는 단어로 나눠도 됩니다. 하루 중 때를 나타내는 단어가 없으면 시는 0부터 23까지입니다.

Temporal

Todae는 Temporal 폴리필을 싣지 않습니다. 둘 다 Temporal.PlainTime을 쓰므로, 브라우저에 Temporal이 없으면 앱이 폴리필을 불러옵니다(네이티브 플랫폼 참고).

접근성을 지키는 조합

Todae가 맡는 것: 넘긴 locale로 쓴 텍스트. 넘기지 않으면 런타임의 로캘입니다.

직접 제공할 것: 페이지와 맞는 locale, 그리고 쓸 때와 읽을 때 같은 locale. parseTime이 null을 돌려줄 때 입력 가까이에 둘 메시지.

주의할 점: locale 없이 서버에서 렌더링하면 텍스트는 서버의 로캘로 쓰입니다.

언제 쓰나요

formatTime

언제: 직접 만든 텍스트 입력처럼, 시각을 로캘에 맞는 텍스트로 쓸 때.

쓰지 않을 때: 시간대를 붙이는 것처럼 다른 형식의 시각 → Intl.DateTimeFormat.

parseTime

언제: 사람이 입력한 시각을 읽을 때.

쓰지 않을 때: 09:30처럼 형식이 정해진 텍스트 → Temporal.PlainTime.from.

Props

formatTime props
이름타입설명
time 필수Temporal.PlainTime

쓸 시각.

options.localestring

ko 같은 로캘. 지정하지 않으면 런타임의 로캘입니다.

parseTime props
이름타입설명
text 필수string

입력된 텍스트.

options.localestring

ko 같은 로캘. 지정하지 않으면 런타임의 로캘입니다.