From dd5518f331162931a0abf45a861a63b7dc5550c7 Mon Sep 17 00:00:00 2001 From: Jonathan Ingram Date: Tue, 31 Oct 2017 14:05:35 +1100 Subject: [PATCH 1/2] Use non-US dates --- .../components/app_activity/timestamp.jsx | 2 +- static_src/test/unit/util/format_date.spec.js | 49 +++++++++---------- static_src/util/format_date.js | 27 +++++----- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/static_src/components/app_activity/timestamp.jsx b/static_src/components/app_activity/timestamp.jsx index f71b7c9a..b4457543 100644 --- a/static_src/components/app_activity/timestamp.jsx +++ b/static_src/components/app_activity/timestamp.jsx @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment-timezone'; -const propTypes = { timestamp: PropTypes.string }; +const propTypes = { timestamp: PropTypes.string.isRequired }; const formatTimestamp = timestamp => moment(timestamp).tz(moment.tz.guess()).format('MMM DD YYYY HH:mm:ss z'); diff --git a/static_src/test/unit/util/format_date.spec.js b/static_src/test/unit/util/format_date.spec.js index 692313d2..fb9fd6e7 100644 --- a/static_src/test/unit/util/format_date.spec.js +++ b/static_src/test/unit/util/format_date.spec.js @@ -1,34 +1,31 @@ - import '../../global_setup.js'; import formatDateTime from '../../../util/format_date'; -describe('format_date util', function () { - describe('when used with an invalid datetime', function () { - it('should throw an exception', function () { - function format () { - return formatDateTime('invaliddate'); - } - expect(format).toThrow(); - }); - }); - - describe('when used with a valid datetime', function () { - describe('but without a timezone', function () { - it('should return a formatted datetime in UTC', function () { - const formatted = formatDateTime('2015-07-14T04:02:30Z'); - const expected = '07/14/2015 04:02am UTC'; - expect(formatted).toEqual(expected); - }); +describe('formatDateTime', () => { + for (const val of [null, undefined, '', 'invaliddate']) { + it(`should throw an exception when given an invalid value › ${val}`, () => { + expect(() => formatDateTime(val)).toThrow(); }); + } - describe('and with a valid timezone', function () { - it('should return a formatted datetime in that timezone', function () { - const tz = 'America/Los_Angeles'; - const formatted = formatDateTime('2015-07-14T04:02:30Z', tz); - const expected = '07/13/2015 09:02pm PDT'; - expect(formatted).toEqual(expected); - }); + for (const { val, tz, output } of [ + { + val: '2015-07-14T04:02:30Z', + output: 'Jul 14 2015 04:02am UTC' + }, + { + val: '1988-10-01T18:58:30Z', + output: 'Oct 01 1988 06:58pm UTC' + }, + { + val: '2015-07-14T04:02:30Z', + tz: 'America/Los_Angeles', + output: 'Jul 13 2015 09:02pm PDT' + } + ]) { + it(`should return a formatted datetime when given a valid value › ${val}`, () => { + expect(formatDateTime(val, tz)).toEqual(output); }); - }); + } }); diff --git a/static_src/util/format_date.js b/static_src/util/format_date.js index c6d182c5..160f965a 100644 --- a/static_src/util/format_date.js +++ b/static_src/util/format_date.js @@ -1,18 +1,17 @@ import moment from 'moment-timezone'; -const invalidMsg = 'Invalid datetimes cannot be formatted.'; +const makeInvalidDateTimeValueError = str => + new Error(`could not format invalid date/time value: ${str}`); -/** - * Returns a formated datetime string - * @params {String} datetime as a string - * @params {String} timezone abbreviation - defaults to UTC - * - * @returns {String} A "MM/DD/YYYY H:mma z" formatted string - * i.e. 03/21/2016 10:39am PDT -*/ -export default function formatDateTime(dateString, timezone = 'Etc/UTC') { - const d = moment(dateString); +const formatDateTime = (str, timezone = 'Etc/UTC') => { + if (!str) { + throw makeInvalidDateTimeValueError(str); + } + const d = moment(str); + if (!d.isValid()) { + throw makeInvalidDateTimeValueError(str); + } + return d.tz(timezone).format('MMM DD YYYY hh:mma z'); +}; - if (!d.isValid()) throw new Error(invalidMsg); - return d.tz(timezone).format('MM/DD/YYYY hh:mma z'); -} +export default formatDateTime; From d47c0cfcc2152fab9fa62241eb61b74ea54e93ba Mon Sep 17 00:00:00 2001 From: Jonathan Ingram Date: Wed, 8 Nov 2017 08:03:19 +1100 Subject: [PATCH 2/2] Reinstate doc comment for formatDateTime --- static_src/util/format_date.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/static_src/util/format_date.js b/static_src/util/format_date.js index 160f965a..11bac84a 100644 --- a/static_src/util/format_date.js +++ b/static_src/util/format_date.js @@ -3,6 +3,15 @@ import moment from 'moment-timezone'; const makeInvalidDateTimeValueError = str => new Error(`could not format invalid date/time value: ${str}`); +/** + * formatDateTime returns a formatted date/time string. + * + * @params {String} date/time as a string + * @params {String} timezone abbreviation - defaults to "Etc/UTC" + * + * @returns {String} A "MMM DD YYYY hh:mma z" formatted string + * e.g. "Jul 13 2015 09:02pm PDT" +*/ const formatDateTime = (str, timezone = 'Etc/UTC') => { if (!str) { throw makeInvalidDateTimeValueError(str);