Skip to content

Commit

Permalink
feat: add millisecond support (#104)
Browse files Browse the repository at this point in the history
* feat: add millisecond support

* fix: incorrect milliseconds when number is round
  • Loading branch information
gkarapeev authored Dec 18, 2020
1 parent 5908096 commit b5ee6b7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/cldr/default-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ const defaultData = {
short: "sec.",
narrow: "sec."
},
millisecond: {
wide: "millisecond",
short: "ms",
narrow: "ms"
},
zone: {
wide: "time zone"
}
Expand Down
1 change: 1 addition & 0 deletions src/dates/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DATE_FIELD_MAP = {
'K': HOUR,
'm': 'minute',
's': 'second',
'S': 'millisecond',
'a': 'dayperiod',
'x': ZONE,
'X': ZONE,
Expand Down
2 changes: 1 addition & 1 deletion src/dates/format-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ formatters.S = function(date, formatLength) {
const milliseconds = date.getMilliseconds();
let result;
if (milliseconds !== 0) {
result = String(date.getMilliseconds() / 1000).split(".")[1].substr(0, formatLength);
result = pad(String(milliseconds / 1000).split(".")[1].substr(0, formatLength), formatLength, true);
} else {
result = pad(EMPTY, formatLength);
}
Expand Down
12 changes: 9 additions & 3 deletions test/cldr.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,22 @@ describe('dateFieldName', () => {
expect(dateFieldName({ type: 'second', nameType: 'short' })).toEqual("sec.");
});

it('should return placeholder for millisecond type', () => {
expect(dateFieldName({ type: 'millisecond', nameType: 'wide' })).toEqual("millisecond");
expect(dateFieldName({ type: 'millisecond', nameType: 'narrow' })).toEqual("ms");
expect(dateFieldName({ type: 'millisecond', nameType: 'short' })).toEqual("ms");
});

it('should return placeholder for zone type', () => {
expect(dateFieldName({ type: 'zone', nameType: 'wide' })).toEqual("time zone");
expect(dateFieldName({ type: 'zone', nameType: 'narrow' })).toEqual("time zone");
expect(dateFieldName({ type: 'zone', nameType: 'short' })).toEqual("time zone");
});

it('should return undefined for missing fieldName type', () => {
expect(dateFieldName({ type: 'millisecond', nameType: 'wide' })).toEqual(undefined);
expect(dateFieldName({ type: 'millisecond', nameType: 'narrow' })).toEqual(undefined);
expect(dateFieldName({ type: 'millisecond', nameType: 'short' })).toEqual(undefined);
expect(dateFieldName({ type: 'turbosecond', nameType: 'wide' })).toEqual(undefined);
expect(dateFieldName({ type: 'turbosecond', nameType: 'narrow' })).toEqual(undefined);
expect(dateFieldName({ type: 'turbosecond', nameType: 'short' })).toEqual(undefined);
});

it('should return wide placeholder by default', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ describe('date formatting', () => {
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 100), "hh:mm:S")).toEqual('01:01:1');
});

it('supports SSS with round numbers', () => {
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 100), "hh:mm:SSS")).toEqual('01:01:100');
});

it('supports S less than 100', () => {
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 99), "hh:mm:S")).toEqual('01:01:0');
});
Expand Down

0 comments on commit b5ee6b7

Please sign in to comment.