Skip to content

Commit

Permalink
Added milliseconds to the localised formatting. This requires seconds…
Browse files Browse the repository at this point in the history
… to be present in the options in order to determine its position.
  • Loading branch information
michaelverschoof committed Jun 2, 2020
1 parent 5f6c8d9 commit c68835b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
22 changes: 20 additions & 2 deletions src/lib/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ function localisedFormat (timestamp : number, format ?: string, locale ?: string
const locales = getLocales(locale);
const options = getOptions(format?.split(','), timezone);

// TODO Milliseconds
// TODO: Remove this once fractionalDigits is more widely used
if (options.fractionalSecondDigits) {
delete options.fractionalSecondDigits;

if (!options.second) {
console.warn('Tried to add milliseconds to localised format without seconds present. This is currently not possible.')
return Formatter(locales, options).format(timestamp);
}

return Formatter(locales, options).formatToParts(timestamp).reduce(
(formatted : string, item : DateTimeFormatPart) => (
formatted += (item.value + (item.type === 'second' ? '.' + getMilliseconds(timestamp) : ''))
), ''
);
}

return Formatter(locales, options).format(timestamp);
}
Expand Down Expand Up @@ -69,11 +83,15 @@ function getLocales (locale ?: string) : string[] {
// TODO: Remove this once fractionalDigits is more widely used
function formatMilliseconds (timestamp : number, format : string) : string {
const regex = new RegExp('{(' + Object.keys(Millisecond.options).join('|') + ')}', 'g');
const milliseconds = timestamp.toString().slice(-3);
const milliseconds = getMilliseconds(timestamp);

return format.replace(regex, milliseconds);
}

function getMilliseconds (timestamp : number) : string {
return timestamp.toString().slice(-3);
}

export const Format = {
format,
localise : localisedFormat,
Expand Down
31 changes: 17 additions & 14 deletions test/format/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const NEW_YORK = 'America/New_York';
const LONDON = 'Europe/London';
const AMSTERDAM = 'Europe/Amsterdam';

const FORMAT_LOCALISED_LONG = 'weekday, month-number-short, day, year, hour, minute, second, timezone';
const FORMAT_LOCALISED_SHORT = 'WDD, M, DD, YY, HH, mm, ss, TZZ';
const FORMAT_LOCALISED_LONG = 'weekday, month-number-short, day, year, hour, minute, second, millisecond, timezone';
const FORMAT_LOCALISED_SHORT = 'WDD, M, DD, YY, HH, mm, ss, ms, TZZ';
const FORMAT_CUSTOMISED_LONG = '{weekday}, {month} {day}, {year} @ {hour}:{minute}:{second}.{millisecond} {AMPM} {timezone-short}';
const FORMAT_CUSTOMISED_SHORT = '{WDD}, {MMMM} {DD}, {YY} @ {HH}:{mm}:{ss}.{ms} {AMPM} {TZ}';

Expand All @@ -26,13 +26,13 @@ const RESULT_SHORT_US = '6/24/1986';
const RESULT_SHORT_GB = '24/06/1986';
const RESULT_SHORT_NL = '24-6-1986';

const RESULT_LOCALISED_US = 'Tuesday, 6/24/1986, 12:01:02 PM Coordinated Universal Time';
const RESULT_LOCALISED_US = 'Tuesday, 6/24/1986, 12:01:02.003 PM Coordinated Universal Time';
const RESULT_LOCALISED_GB = 'Tuesday, 24/06/1986, 12:01:02 Coordinated Universal Time';
const RESULT_LOCALISED_NL = 'dinsdag 24-6-1986 12:01:02 Gecoördineerde wereldtijd';
const RESULT_LOCALISED_NL = 'dinsdag 24-6-1986 12:01:02.003 Gecoördineerde wereldtijd';

const RESULT_LOCALISED_NEW_YORK = 'Tuesday, 6/24/1986, 8:01:02 AM Eastern Daylight Time';
const RESULT_LOCALISED_NEW_YORK = 'Tuesday, 6/24/1986, 8:01:02.003 AM Eastern Daylight Time';
const RESULT_LOCALISED_LONDON = 'Tuesday, 24/06/1986, 13:01:02 British Summer Time';
const RESULT_LOCALISED_AMSTERDAM = 'dinsdag 24-6-1986 14:01:02 Midden-Europese zomertijd';
const RESULT_LOCALISED_AMSTERDAM = 'dinsdag 24-6-1986 14:01:02.003 Midden-Europese zomertijd';

const RESULT_CUSTOMISED_US = 'Tuesday, June 24, 1986 @ 12:01:02.003 PM UTC';
const RESULT_CUSTOMISED_GB = 'Tuesday, June 24, 1986 @ 12:01:02 pm UTC';
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Localise', () => {
let result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, US);
expect(result).toBe(RESULT_LOCALISED_US);

result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, GB);
result = Format.localise(timestamp, FORMAT_LOCALISED_LONG.replace(', millisecond', ''), GB);
expect(result).toBe(RESULT_LOCALISED_GB);

result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, NL);
Expand All @@ -112,12 +112,15 @@ describe('Localise', () => {
result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, US);
expect(result).toBe(RESULT_LOCALISED_US);

result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, GB);
result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT.replace(', ms', ''), GB);
expect(result).toBe(RESULT_LOCALISED_GB);

result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, NL);
expect(result).toBe(RESULT_LOCALISED_NL);

result = Format.localise(timestamp, FORMAT_LOCALISED_LONG.replace(', second', ''), GB);
expect(result).toBe(RESULT_LOCALISED_GB.replace(':02', ''));

expect(() => Format.localise(timestamp, FORMAT_WRONG_LONG, US)).toThrowError(PrimeError);
expect(() => Format.localise(timestamp, FORMAT_WRONG_LONG, GB)).toThrowError(PrimeError);
expect(() => Format.localise(timestamp, FORMAT_WRONG_LONG, NL)).toThrowError(PrimeError);
Expand All @@ -130,7 +133,7 @@ describe('Localise', () => {
let result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, US, NEW_YORK);
expect(result).toBe(RESULT_LOCALISED_NEW_YORK);

result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, GB, LONDON);
result = Format.localise(timestamp, FORMAT_LOCALISED_LONG.replace(', millisecond', ''), GB, LONDON);
expect(result).toBe(RESULT_LOCALISED_LONDON);

result = Format.localise(timestamp, FORMAT_LOCALISED_LONG, NL, AMSTERDAM);
Expand All @@ -139,7 +142,7 @@ describe('Localise', () => {
result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, US, NEW_YORK);
expect(result).toBe(RESULT_LOCALISED_NEW_YORK);

result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, GB, LONDON);
result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT.replace(', ms', ''), GB, LONDON);
expect(result).toBe(RESULT_LOCALISED_LONDON);

result = Format.localise(timestamp, FORMAT_LOCALISED_SHORT, NL, AMSTERDAM);
Expand Down Expand Up @@ -308,7 +311,7 @@ describe('Format', () => {
let result = Format.format(timestamp, FORMAT_LOCALISED_LONG, US);
expect(result).toBe(RESULT_LOCALISED_US);

result = Format.format(timestamp, FORMAT_LOCALISED_LONG, GB);
result = Format.format(timestamp, FORMAT_LOCALISED_LONG.replace(', millisecond', ''), GB);
expect(result).toBe(RESULT_LOCALISED_GB);

result = Format.format(timestamp, FORMAT_LOCALISED_LONG, NL);
Expand All @@ -317,7 +320,7 @@ describe('Format', () => {
result = Format.format(timestamp, FORMAT_LOCALISED_SHORT, US);
expect(result).toBe(RESULT_LOCALISED_US);

result = Format.format(timestamp, FORMAT_LOCALISED_SHORT, GB);
result = Format.format(timestamp, FORMAT_LOCALISED_SHORT.replace(', ms', ''), GB);
expect(result).toBe(RESULT_LOCALISED_GB);

result = Format.format(timestamp, FORMAT_LOCALISED_SHORT, NL);
Expand Down Expand Up @@ -353,7 +356,7 @@ describe('Format', () => {
let result = Format.format(timestamp, FORMAT_LOCALISED_LONG, US, NEW_YORK);
expect(result).toBe(RESULT_LOCALISED_NEW_YORK);

result = Format.format(timestamp, FORMAT_LOCALISED_LONG, GB, LONDON);
result = Format.format(timestamp, FORMAT_LOCALISED_LONG.replace(', millisecond', ''), GB, LONDON);
expect(result).toBe(RESULT_LOCALISED_LONDON);

result = Format.format(timestamp, FORMAT_LOCALISED_LONG.replace('{AMPM}', ''), NL, AMSTERDAM);
Expand All @@ -362,7 +365,7 @@ describe('Format', () => {
result = Format.format(timestamp, FORMAT_LOCALISED_SHORT, US, NEW_YORK);
expect(result).toBe(RESULT_LOCALISED_NEW_YORK);

result = Format.format(timestamp, FORMAT_LOCALISED_SHORT, GB, LONDON);
result = Format.format(timestamp, FORMAT_LOCALISED_SHORT.replace(', ms', ''), GB, LONDON);
expect(result).toBe(RESULT_LOCALISED_LONDON);

result = Format.format(timestamp, FORMAT_LOCALISED_SHORT.replace('{AMPM}', ''), NL, AMSTERDAM);
Expand Down

0 comments on commit c68835b

Please sign in to comment.