Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Use non-US dates #1274

Merged
merged 2 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion static_src/components/app_activity/timestamp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
49 changes: 23 additions & 26 deletions static_src/test/unit/util/format_date.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
});
30 changes: 19 additions & 11 deletions static_src/util/format_date.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
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
* formatDateTime returns a formatted date/time string.
*
* @returns {String} A "MM/DD/YYYY H:mma z" formatted string
* i.e. 03/21/2016 10:39am PDT
* @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"
*/
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;