diff --git a/scm-ui/ui-components/src/DateFromNow.js b/scm-ui/ui-components/src/DateFromNow.js index 5abe1bb1b3..1b7786a499 100644 --- a/scm-ui/ui-components/src/DateFromNow.js +++ b/scm-ui/ui-components/src/DateFromNow.js @@ -13,6 +13,15 @@ const supportedLocales = { type Props = { date?: string, + timeZone?: string, + + /** + * baseDate is the date from which the distance is calculated, + * default is the current time (new Date()). This property + * is required to keep snapshots tests green over the time on + * ci server. + */ + baseDate?: string, // context props i18n: any @@ -34,11 +43,23 @@ class DateFromNow extends React.Component { }; createOptions = () => { - const locale = this.getLocale(); - return { - locale, - addSuffix: true + const { timeZone } = this.props; + const options: Object = { + addSuffix: true, + locate: this.getLocale(), }; + if (timeZone) { + options.timeZone = timeZone; + } + return options; + }; + + getBaseDate = () => { + const { baseDate } = this.props; + if (baseDate) { + return parseISO(baseDate); + } + return new Date(); }; render() { @@ -46,7 +67,7 @@ class DateFromNow extends React.Component { if (date) { const isoDate = parseISO(date); const options = this.createOptions(); - const distance = formatDistance(isoDate, new Date(), options); + const distance = formatDistance(isoDate, this.getBaseDate(), options); const formatted = format(isoDate, "yyyy-MM-dd HH:mm:ss", options); return {distance}; } diff --git a/scm-ui/ui-components/src/DateFromNow.stories.js b/scm-ui/ui-components/src/DateFromNow.stories.js index 2d99ac7997..eee92c2681 100644 --- a/scm-ui/ui-components/src/DateFromNow.stories.js +++ b/scm-ui/ui-components/src/DateFromNow.stories.js @@ -2,13 +2,24 @@ import React from "react"; import DateFromNow from "./DateFromNow"; import { storiesOf } from "@storybook/react"; +const baseProps = { + timeZone: "Europe/Berlin", + baseDate: "2019-10-12T13:56:42+02:00" +}; + storiesOf("DateFromNow", module).add("Default", () => (

- +

- + +

+

+ +

+

+

));