Skip to content

Commit b5ee6b7

Browse files
authored
feat: add millisecond support (#104)
* feat: add millisecond support * fix: incorrect milliseconds when number is round
1 parent 5908096 commit b5ee6b7

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

src/cldr/default-data.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ const defaultData = {
535535
short: "sec.",
536536
narrow: "sec."
537537
},
538+
millisecond: {
539+
wide: "millisecond",
540+
short: "ms",
541+
narrow: "ms"
542+
},
538543
zone: {
539544
wide: "time zone"
540545
}

src/dates/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const DATE_FIELD_MAP = {
2121
'K': HOUR,
2222
'm': 'minute',
2323
's': 'second',
24+
'S': 'millisecond',
2425
'a': 'dayperiod',
2526
'x': ZONE,
2627
'X': ZONE,

src/dates/format-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ formatters.S = function(date, formatLength) {
123123
const milliseconds = date.getMilliseconds();
124124
let result;
125125
if (milliseconds !== 0) {
126-
result = String(date.getMilliseconds() / 1000).split(".")[1].substr(0, formatLength);
126+
result = pad(String(milliseconds / 1000).split(".")[1].substr(0, formatLength), formatLength, true);
127127
} else {
128128
result = pad(EMPTY, formatLength);
129129
}

test/cldr.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,22 @@ describe('dateFieldName', () => {
569569
expect(dateFieldName({ type: 'second', nameType: 'short' })).toEqual("sec.");
570570
});
571571

572+
it('should return placeholder for millisecond type', () => {
573+
expect(dateFieldName({ type: 'millisecond', nameType: 'wide' })).toEqual("millisecond");
574+
expect(dateFieldName({ type: 'millisecond', nameType: 'narrow' })).toEqual("ms");
575+
expect(dateFieldName({ type: 'millisecond', nameType: 'short' })).toEqual("ms");
576+
});
577+
572578
it('should return placeholder for zone type', () => {
573579
expect(dateFieldName({ type: 'zone', nameType: 'wide' })).toEqual("time zone");
574580
expect(dateFieldName({ type: 'zone', nameType: 'narrow' })).toEqual("time zone");
575581
expect(dateFieldName({ type: 'zone', nameType: 'short' })).toEqual("time zone");
576582
});
577583

578584
it('should return undefined for missing fieldName type', () => {
579-
expect(dateFieldName({ type: 'millisecond', nameType: 'wide' })).toEqual(undefined);
580-
expect(dateFieldName({ type: 'millisecond', nameType: 'narrow' })).toEqual(undefined);
581-
expect(dateFieldName({ type: 'millisecond', nameType: 'short' })).toEqual(undefined);
585+
expect(dateFieldName({ type: 'turbosecond', nameType: 'wide' })).toEqual(undefined);
586+
expect(dateFieldName({ type: 'turbosecond', nameType: 'narrow' })).toEqual(undefined);
587+
expect(dateFieldName({ type: 'turbosecond', nameType: 'short' })).toEqual(undefined);
582588
});
583589

584590
it('should return wide placeholder by default', () => {

test/dates.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ describe('date formatting', () => {
236236
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 100), "hh:mm:S")).toEqual('01:01:1');
237237
});
238238

239+
it('supports SSS with round numbers', () => {
240+
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 100), "hh:mm:SSS")).toEqual('01:01:100');
241+
});
242+
239243
it('supports S less than 100', () => {
240244
expect(formatDate(date(2000, 1, 1, 1, 1, 1, 99), "hh:mm:S")).toEqual('01:01:0');
241245
});

0 commit comments

Comments
 (0)