@bbc/psammead-timestamp-container

中文文档
5.1.2发布于 2022/03/30Apache-2.0ESM + CJS近 30 天下载 5187

React Timestamp container for relative and absolute times, using moment-timezone

原始使用文档
文档目录12 节
  1. Description
  2. Exports
  3. When to use this component
  4. Installation
  5. Props
  6. Usage
  7. Utilities
  8. Accessibility notes
  9. Roadmap
  10. Contributing
  11. Code of Conduct
  12. License

psammead-timestamp-container - Known Vulnerabilities Dependency Status peerDependencies Status npm version Storybook GitHub license PRs Welcome

Description

psammead-timestamp-container is a React container that offers relative and absolute times, with timezone support - using moment-timezone. Relative times are currently hard-coded to English (e.g. 3 minutes ago). psammead-timestamp-container returns a single time element.

Exports

/utilities - exports functions for dealing with timestamps.

When to use this component

psammead-timestamp-container is intended to be used when a single time DOM element that has the possibility of having varying time formats and timezones is required.

Installation

npm install @bbc/psammead-timestamp-container

Props

ArgumentTypeRequiredDefaultExample
timestampnumber or stringYesN/A1530947227000, '2012-03-08T12:17:24.000Z'
dateTimeFormatstringYesN/AYYYY-MM-DD
isRelativebooleanNofalsetrue
formatstringNonullD MMMM YYYY, HH:mm z
timezonestringNo'Europe/London''Europe/London'
paddingbooleanNotruefalse
prefixstringNonullUpdated
suffixstringNonullThis is a suffix
scriptobjectYesN/A{ canon: { groupA: { fontSize: '28', lineHeight: '32',}, groupB: { fontSize: '32', lineHeight: '36', }, groupD: { fontSize: '44', lineHeight: '48', }, }, trafalgar: { groupA: { fontSize: '20', lineHeight: '24', }, groupB: { fontSize: '24', lineHeight: '28', }, groupD: { fontSize: '32', lineHeight: '36', }, }, }
localestringnonullen
servicestringyesnullnews
altCalendarobjectnonullSee the jalaali calendar in psammead-calendars as an example

locale should be a momentjs locale.

Usage

import { latin } from '@bbc/gel-foundations/scripts';

const WrappingContainer = () => (
  <Timestamp
    timestamp={1530947227000}
    dateTimeFormat="YYYY-MM-DD"
    isRelative={false}
    format="D MMMM YYYY"
    timezone="Europe/London"
    prefix="Updated"
    suffix="."
    script={latin}
    locale="en"
  />
);

Utilities

formatDuration

import { formatDuration } from '@bbc/psammead-timestamp-container/utilities';

const localisedDuration = formatDuration({ duration: 'PTM30', locale: 'my' });
const customFormatDuration = formatDuration({
  duration: 'PTM30',
  format: 'mm,ss',
});
ArgumentTypeRequiredDefaultExample
durationstringYesN/APTH1M29S30 Duration string as specified by ISO 8601 standard.
formatstringNomm:ss, or h:mm:ss if in hoursmm,ss
localestringNoen-gbfr

localisedMoment

import { localisedMoment } from '@bbc/psammead-timestamp-container/utilities';

const localisedMoment = localisedMoment({ locale, timestamp });
ArgumentTypeRequiredDefaultExample
timestampnumber or stringYesN/A1539969006000 (Unix timestamp), '2012-03-08T12:17:24.000Z' (ISO8601 string)
localestringYesN/Afr

formatUnixTimestamp

import { localiseTimestamp } from '@bbc/psammead-timestamp-container/utilities';

const formattedTime = formatUnixTimestamp({
  format,
  isRelative,
  locale,
  timestamp,
  timezone,
});
ArgumentTypeRequiredDefaultExample
formatstringNo'LL, LT z''D MMMM YYYY z' a moment format string
isRelativebooleanYesN/Atrue When true, formats in relative time
localestringYesN/Afr Moment locale
timestampnumber or stringYesN/A1539969006000 (Unix timestamp), '2012-03-08T12:17:24.000Z' (ISO8601 string)
timezonestringYesN/A'GMT', 'Asia/Seoul' see (moment-timezone documentation)[https://momentjs.com/timezone/]

Accessibility notes

The usage of the semantic time element within psammead-timestamp component can result in strange behaviours when using assistive technology such as VoiceOver for iOS. Specifically, it results in a "text splitting" behaviour, where any text in the prefix is read out, but the value inside the semantic time element require an additional swipe before being read out.

However, as we heavily encourage the use of this semantic element, and want to ensure it's usage is in line with the W3C examples, we feel this compromise is necessary.

Roadmap

Currently, this package only supports relative time in English. The plan is to make this package more suitable for use on World Service sites by including logic to render relative time in multiple languages.

Contributing

Psammead is completely open source. We are grateful for any contributions, whether they be new components, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Psammead respository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Psammead is Apache 2.0 licensed.

版本与发布记录

@bbc/psammead-timestamp-container 共发布 143 个版本,首次发布于 2019/05/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.1.2latest2022/03/304 年前
5.1.12022/03/294 年前
5.1.02022/03/104 年前
5.0.402022/01/064 年前
5.0.392021/10/065 年前
5.0.382021/09/305 年前

其他发布通道:alpha → 5.0.7。

关于 @bbc/psammead-timestamp-container 的常见问题

@bbc/psammead-timestamp-container 是什么?

React Timestamp container for relative and absolute times, using moment-timezone

如何安装 @bbc/psammead-timestamp-container?

在项目目录运行 npm install @bbc/psammead-timestamp-container,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.9.0)、moment(2.24.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bbc/psammead-timestamp-container 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bbc__psammead-timestamp-container 是否存在。

@bbc/psammead-timestamp-container 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbc/psammead-timestamp-container 可以商用吗?

@bbc/psammead-timestamp-container 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/psammead-timestamp-container 的下载量有多少?

根据 npm 官方下载统计,@bbc/psammead-timestamp-container 在 2026/10/08 – 2026/11/06 这 30 天内被下载 5,187 次,日均约 173 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbc/psammead-timestamp-container 还在维护吗?

最新版本 5.1.2 发布于 2022/03/30(4 年前);至今共发布 143 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/05/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。