This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1274 from govau/ji-non-us-dates
Use non-US dates
- Loading branch information
Showing
3 changed files
with
43 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |