Skip to content

Commit

Permalink
fix(datetime): format seconds token
Browse files Browse the repository at this point in the history
Closes #6951
  • Loading branch information
adamdbradley committed Jun 28, 2016
1 parent 3cd31c3 commit 4fff262
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/datetime/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ionicBootstrap} from '../../../../../src';
})
class E2EPage {
wwwInvented = '1989';
time = '13:47';
time = '13:47:00';
netscapeReleased = '1994-12-15T13:47:20.789';
operaReleased = '1995-04-15';
firefoxReleased = '2002-09-23T15:03:46.789';
Expand Down
5 changes: 5 additions & 0 deletions src/components/datetime/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@
{{convertedDate}}
</p>

<ion-item>
<ion-label>HH:mm:ss</ion-label>
<ion-datetime displayFormat="HH:mm:ss" [(ngModel)]="time"></ion-datetime>
</ion-item>

</ion-content>
12 changes: 11 additions & 1 deletion src/util/datetime-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function renderTextFormat(format: string, value: any, date: DateTimeData,

if (format === FORMAT_YY || format === FORMAT_MM ||
format === FORMAT_DD || format === FORMAT_HH ||
format === FORMAT_mm) {
format === FORMAT_mm || format === FORMAT_ss) {
return twoDigit(value);
}

Expand Down Expand Up @@ -136,6 +136,12 @@ export function dateValueRange(format: string, min: DateTimeData, max: DateTimeD
opts.push(i);
}

} else if (format === FORMAT_ss || format === FORMAT_s) {
// seconds
for (i = 0; i < 60; i++) {
opts.push(i);
}

} else if (format === FORMAT_A || format === FORMAT_a) {
// AM/PM
opts.push('am', 'pm');
Expand Down Expand Up @@ -432,6 +438,8 @@ const FORMAT_hh = 'hh';
const FORMAT_h = 'h';
const FORMAT_mm = 'mm';
const FORMAT_m = 'm';
const FORMAT_ss = 'ss';
const FORMAT_s = 's';
const FORMAT_A = 'A';
const FORMAT_a = 'a';

Expand All @@ -447,11 +455,13 @@ const FORMAT_KEYS = [
{ f: FORMAT_HH, k: 'hour' },
{ f: FORMAT_hh, k: 'hour' },
{ f: FORMAT_mm, k: 'minute' },
{ f: FORMAT_ss, k: 'second' },
{ f: FORMAT_M, k: 'month' },
{ f: FORMAT_D, k: 'day' },
{ f: FORMAT_H, k: 'hour' },
{ f: FORMAT_h, k: 'hour' },
{ f: FORMAT_m, k: 'minute' },
{ f: FORMAT_s, k: 'second' },
{ f: FORMAT_A, k: 'ampm' },
{ f: FORMAT_a, k: 'ampm' },
];
Expand Down
45 changes: 26 additions & 19 deletions src/util/test/datetime-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('convertDataToISO', () => {
expect(str).toEqual('13:47:20.789');
});

it('should convert DateTimeData to HH:mm:SS string', () => {
it('should convert DateTimeData to HH:mm:ss string', () => {
var data: datetime.DateTimeData = {
year: null,
month: null,
Expand Down Expand Up @@ -201,6 +201,11 @@ describe('convertFormatToKey', () => {
expect(datetime.convertFormatToKey('m')).toEqual('minute');
});

it('should convert second formats to their DateParse key', () => {
expect(datetime.convertFormatToKey('ss')).toEqual('second');
expect(datetime.convertFormatToKey('s')).toEqual('second');
});

it('should convert am/pm formats to their DateParse key', () => {
expect(datetime.convertFormatToKey('A')).toEqual('ampm');
expect(datetime.convertFormatToKey('a')).toEqual('ampm');
Expand Down Expand Up @@ -317,24 +322,26 @@ describe('parseTemplate', () => {
expect(formats[1]).toEqual('mm');
});

it('should get formats from template "m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY"', () => {
var formats = datetime.parseTemplate('m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY');
expect(formats[0]).toEqual('m');
expect(formats[1]).toEqual('mm');
expect(formats[2]).toEqual('h');
expect(formats[3]).toEqual('hh');
expect(formats[4]).toEqual('H');
expect(formats[5]).toEqual('HH');
expect(formats[6]).toEqual('D');
expect(formats[7]).toEqual('DD');
expect(formats[8]).toEqual('DDD');
expect(formats[9]).toEqual('DDDD');
expect(formats[10]).toEqual('M');
expect(formats[11]).toEqual('MM');
expect(formats[12]).toEqual('MMM');
expect(formats[13]).toEqual('MMMM');
expect(formats[14]).toEqual('YY');
expect(formats[15]).toEqual('YYYY');
it('should get formats from template "s ss m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY"', () => {
var formats = datetime.parseTemplate('s ss m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY');
expect(formats[0]).toEqual('s');
expect(formats[1]).toEqual('ss');
expect(formats[2]).toEqual('m');
expect(formats[3]).toEqual('mm');
expect(formats[4]).toEqual('h');
expect(formats[5]).toEqual('hh');
expect(formats[6]).toEqual('H');
expect(formats[7]).toEqual('HH');
expect(formats[8]).toEqual('D');
expect(formats[9]).toEqual('DD');
expect(formats[10]).toEqual('DDD');
expect(formats[11]).toEqual('DDDD');
expect(formats[12]).toEqual('M');
expect(formats[13]).toEqual('MM');
expect(formats[14]).toEqual('MMM');
expect(formats[15]).toEqual('MMMM');
expect(formats[16]).toEqual('YY');
expect(formats[17]).toEqual('YYYY');
});

it('should get formats from template YYMMMMDDHHmm', () => {
Expand Down

0 comments on commit 4fff262

Please sign in to comment.