패키지, 저장소 및 앱 카테고리 검색

패키지 상세v5.0.0

relative_time

현재 시간을 기준으로 DateTime 객체를 텍스트 형식으로 표현합니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
8.2천
좋아요
22
Pub 점수
160

패키지 설치

flutter pub add relative_time

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

relative_time 패키지는 DateTime 객체를 현재 시점과의 상대적인 시간을 표현하는 인간이 읽기 쉬운 문자열로 변환합니다. 예를 들어 원시 타임스탬프 대신 '2분 전' 또는 '3시간 후'와 같은 표시가 가능합니다. 여러 언어를 지원하며, Android, iOS, Web, macOS, Windows, Linux 등 주요 플랫폼에서 간편성과 성능을 고려해 설계되었습니다. 피드, 알림, 채팅 인터페이스 등에서 동적이고 사용자 친화적인 시간 표시가 필요한 앱에 적합합니다.

사용 사례

  • 게시물 게시 후 경과 시간 표시
  • 카운트다운 또는 알림 표시
  • 채팅 메시지 타임스탬프 향상
  • 실시간 활동 지표 업데이트
  • 소셜 미디어 앱의 사용자 경험(UX) 향상

주요 기능

  • 간단한 API, 최소 설정
  • 로컬라이제이션을 지원합니다
  • 크로스 플랫폼 호환성
  • 실시간 정확성을 위한 자동 업데이트
  • 경량이고 효율적

적합한 대상

  • 실시간 데이터 피드를 갖춘 앱
  • 소셜 네트워킹 애플리케이션
  • 작업 및 알림 도구
  • 채팅 및 메시징 플랫폼
  • 동적 시간 레이블을 필요로 하는 사용자 인터페이스 구성 요소

고려 사항

  • 실시간 업데이트를 위해서는 수동 새로 고침이 필요합니다
  • 상대적 시간 형식화만 가능
  • 사용자 정의 시간 단위에 대한 내장 지원 없음
  • 시스템 시계 정확도에 따라 다름
  • 시간대 차이를 처리하기 위해 추가 조치가 필요할 수 있습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

relative_time

A Flutter package to express a DateTime object relative to the current time in text form. For instance, it may transform DateTimes into 3 years ago, or in 42 minutes, or now.

Features

  • Available in more languages than you can name.
  • Provides localization delegate for easy internationalization.
  • Usable as class instance method call or extension method.
  • Can be expressed in different, customizable time units.
  • Supports numeric and natural representations.

Getting started

The package may optionally retrieve the appropriate localization from the BuildContext. The standard way to setup localization is to make use of a WidgetApp's (such as a MaterialApp) localizationsDelegates and supportedLocales. If you're already using these to localize your app, it should suffice to add RelativeTimeLocalizations.delegate to your list of localizationsDelegates, roughly as follows.

MaterialApp(
  localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
    AppLocalizations.delegate,
    RelativeTimeLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: AppLocalizations.supportedLocales,
);

If your app does not make use of (this form of) localization yet but you would like it to, reference the Flutter documentation on internationalization for more information on how to set this up.

Usage

The relative time may be accessed by passing the DateTime source object to format on an instance of RelativeTime with either a BuildContext (if configured for localization) or a specific Locale.

RelativeTime(context).format(time);
RelativeTime.locale(const Locale('en')).format(time);

Alternatively, the provided DateTime extension methods are a bit more concise.

time.relativeTime(context);
time.relativeTimeLocale(const Locale('en'));

You may specify a list of time units that will be considered. This can be any of the following: year, month, week, day, hour, minute and second. By default, all time units are potentially used. The following limits the relative time to being expressed in years and weeks. No judgement here. Because weeks are the most granular time unit specified, anything less than one week would be represented as this week.

RelativeTime(
  context, 
  timeUnits: const <TimeUnit>[
    TimeUnit.year,
    TimeUnit.week,
  ],
).format(time);

By default, results are described using natural language as much as possible. That is, one day in the future will be transformed into tomorrow. If you'd rather see this as in 1 day, set the numeric parameter to true.

RelativeTime(
  context,
  numeric: true,
).format(time);

Testing

The current time may be mocked with help from the clock package.

withClock(
  Clock.fixed(DateTime.fromMicrosecondsSinceEpoch(0)),
  () => RelativeTime(context).format(time),
);

Additional information

Translations have been extracted from the Unicode CLDR Project. The extraction tool for this purpose is available in the dev folder. Note that due to the automated nature of this process, PRs for language updates will not be considered.

카테고리

의존성

의존성 목록 2 개

소스 저장소

이 패키지를 사용하는 프로젝트