123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import moment from 'moment';
- import momentTimezone from 'moment-timezone';
- import ConfigStore from 'sentry/stores/configStore';
- interface Props extends React.HTMLAttributes<HTMLTimeElement> {
-
- date: moment.MomentInput | momentTimezone.MomentInput;
-
- dateOnly?: boolean;
-
- format?: string;
-
- seconds?: boolean;
-
- timeOnly?: boolean;
-
- timeZone?: boolean;
-
- utc?: boolean;
-
- year?: boolean;
- }
- function getDateFormat({year}: Pick<Props, 'year'>) {
-
- return year ? 'MMM D, YYYY' : 'MMM D';
- }
- function getTimeFormat({clock24Hours, seconds, timeZone}) {
- const substrings = [
- clock24Hours ? 'HH' : 'h',
- ':mm',
- seconds ? ':ss' : '',
- clock24Hours ? '' : ' A',
- timeZone ? ' z' : '',
- ];
- return substrings.join('');
- }
- function getFormat({
- dateOnly,
- timeOnly,
- year,
- seconds,
- timeZone,
- clock24Hours,
- }: Pick<Props, 'dateOnly' | 'timeOnly' | 'year' | 'seconds' | 'timeZone'> & {
- clock24Hours: boolean;
- }) {
- if (dateOnly) {
- return getDateFormat({year});
- }
- if (timeOnly) {
- return getTimeFormat({clock24Hours, seconds, timeZone});
- }
- const dateFormat = getDateFormat({year});
- const timeFormat = getTimeFormat({
- clock24Hours,
- seconds,
- timeZone,
- });
-
-
- return year ? `${dateFormat} ${timeFormat}` : `${dateFormat}, ${timeFormat}`;
- }
- function DateTime({
- format,
- date,
- utc,
- dateOnly,
- timeOnly,
- year,
- timeZone,
- seconds = false,
- ...props
- }: Props) {
- const user = ConfigStore.get('user');
- const options = user?.options;
- const formatString =
- format ??
- getFormat({
- dateOnly,
- timeOnly,
-
-
- year: year ?? moment().year() !== moment(date).year(),
-
-
- timeZone: timeZone ?? utc,
- seconds,
- ...options,
- });
- return (
- <time {...props}>
- {utc
- ? moment.utc(date as moment.MomentInput).format(formatString)
- : momentTimezone.tz(date, options?.timezone ?? '').format(formatString)}
- </time>
- );
- }
- export default DateTime;
|