Skip to content

Commit 01af6ef

Browse files
authored
fix(date-formatter): changed logic for calculating relative formats (#UIM-853) (#806)
1 parent 93ceb5d commit 01af6ef

File tree

7 files changed

+92
-54
lines changed

7 files changed

+92
-54
lines changed

packages/cdk/datetime/date-adapter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ export abstract class DateAdapter<D> {
274274

275275
abstract diffNow(date: D, unit): number;
276276

277+
abstract daysFromToday(date: D): number;
278+
277279
/**
278280
* Attempts to deserialize a value to a valid date object. This is different from parsing in that
279281
* deserialize should only accept non-ambiguous, locale-independent formats (e.g. a ISO 8601

packages/mosaic-examples/mosaic/date-formatter/relative-date-formatter/relative-date-formatter-example.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ export class RelativeDateFormatterExample {
8282
const now = this.adapter.today();
8383

8484
relativeShort.beforeYesterdayNotCurrentYear = this.dateFormatter.relativeShortDate(
85-
now.minus({ years: 1, days: 2 })
85+
now.minus({ years: 1 })
8686
);
8787
relativeShort.beforeYesterdayCurrentYear = this.dateFormatter.relativeShortDate(now.minus({ days: 2 }));
8888
relativeShort.yesterday = this.dateFormatter.relativeShortDate(now.minus({ days: 1 }));
8989

9090
relativeShort.today = this.dateFormatter.relativeShortDate(now.minus({ hours: 1 }));
9191

92-
relativeShort.tomorrow = this.dateFormatter.relativeShortDate(now.plus({ days: 1, hours: 1 }));
93-
relativeShort.afterTomorrowCurrentYear = this.dateFormatter.relativeShortDate(now.plus({ days: 2, hours: 1 }));
92+
relativeShort.tomorrow = this.dateFormatter.relativeShortDate(now.plus({ days: 1 }));
93+
relativeShort.afterTomorrowCurrentYear = this.dateFormatter.relativeShortDate(now.plus({ days: 2 }));
9494
relativeShort.afterTomorrowNotCurrentYear = this.dateFormatter.relativeShortDate(
95-
now.plus({ years: 1, days: 2 })
95+
now.plus({ years: 1 })
9696
);
9797
}
9898

@@ -104,17 +104,17 @@ export class RelativeDateFormatterExample {
104104
const now = this.adapter.today();
105105

106106
relativeLong.beforeYesterdayNotCurrentYear = this.dateFormatter.relativeLongDate(
107-
now.minus({ years: 1, days: 2 })
107+
now.minus({ years: 1 })
108108
);
109109
relativeLong.beforeYesterdayCurrentYear = this.dateFormatter.relativeLongDate(now.minus({ days: 2 }));
110110
relativeLong.yesterday = this.dateFormatter.relativeLongDate(now.minus({ days: 1 }));
111111

112112
relativeLong.today = this.dateFormatter.relativeLongDate(now.minus({ hours: 1 }));
113113

114-
relativeLong.tomorrow = this.dateFormatter.relativeLongDate(now.plus({ days: 1, hours: 1 }));
115-
relativeLong.afterTomorrowCurrentYear = this.dateFormatter.relativeLongDate(now.plus({ days: 2, hours: 1 }));
114+
relativeLong.tomorrow = this.dateFormatter.relativeLongDate(now.plus({ days: 1 }));
115+
relativeLong.afterTomorrowCurrentYear = this.dateFormatter.relativeLongDate(now.plus({ days: 2 }));
116116
relativeLong.afterTomorrowNotCurrentYear = this.dateFormatter.relativeLongDate(
117-
now.plus({ years: 1, days: 2 })
117+
now.plus({ years: 1 })
118118
);
119119
}
120120
}

packages/mosaic-luxon-adapter/adapter/date-adapter.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
DateAdapter,
77
MC_DATE_LOCALE
88
} from '@ptsecurity/cdk/datetime';
9-
import { DateTime, DateTimeOptions, DurationUnit, Info, LocaleOptions } from 'luxon';
9+
import { DateTime, DateTimeOptions, DurationUnit, Info, Interval, LocaleOptions } from 'luxon';
1010

1111
import { enUS } from './locales/en-US';
1212
import { ruRU } from './locales/ru-RU';
@@ -289,6 +289,18 @@ export class LuxonDateAdapter extends DateAdapter<DateTime> {
289289
return date.diffNow(unit)[unit];
290290
}
291291

292+
daysFromToday(date: DateTime): number {
293+
const today = this.today();
294+
295+
if (this.hasSame(date, today, 'days')) {
296+
return 0;
297+
} else if (date < today) {
298+
return Math.round(Interval.fromDateTimes(date, today).length('days')) * -1;
299+
} else {
300+
return Math.round(Interval.fromDateTimes(today, date).length('days'));
301+
}
302+
}
303+
292304
private reconfigure(date: DateTime): DateTime {
293305
return date
294306
.reconfigure(this.localeOptions)

packages/mosaic-moment-adapter/adapter/moment-date-adapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
310310
return date.diff(this.today(), unit);
311311
}
312312

313+
daysFromToday(date: Moment): number {
314+
return this.diffNow(date, 'days');
315+
}
316+
313317
/** Creates a Moment instance while respecting the current UTC settings. */
314318
private createMoment(...args: any[]): Moment {
315319
return this.options?.useUtc ? moment.utc(...args) : moment(...args);

packages/mosaic/core/formatters/date/formatter.spec.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ describe('Date formatter', () => {
1717

1818
const mockAdapterAndFormatterForRelativeTests = () => {
1919
// @ts-ignore
20-
adapter.diffNow = (date: DateTime, unit: DurationUnit): number => {
21-
return date.diff(currentDate, unit)[unit];
22-
};
20+
adapter.today = (): DateTime => currentDate;
2321

2422
// @ts-ignore
2523
formatter.hasSame = (startDate: DateTime, endDate: DateTime, unit: DurationUnit): string => {
@@ -42,7 +40,8 @@ describe('Date formatter', () => {
4240
adapter = d;
4341
formatter = f;
4442

45-
currentDate = adapter.createDateTime(2000, 10, 10, 10, 0, 0, 0);
43+
44+
currentDate = adapter.createDateTime(adapter.today().year, 5, 15, 0, 0, 0, 0);
4645
}));
4746

4847
const YEAR = 'yyyy';
@@ -74,7 +73,7 @@ describe('Date formatter', () => {
7473
describe('relative formats', () => {
7574
describe('Relative short (relativeShortDate method)', () => {
7675
it('before yesterday (other year)', () => {
77-
const date = adapter.createDate(2015).minus({ hours: 49 });
76+
const date = adapter.createDate(2015).minus({ days: 3 });
7877
expect(formatter.relativeShortDate(date))
7978
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH} ${YEAR}`));
8079
});
@@ -103,7 +102,7 @@ describe('Date formatter', () => {
103102
});
104103

105104
it('today', () => {
106-
const date = adapter.today().minus({ hours: 1 });
105+
const date = adapter.today();
107106

108107
expect(formatter.relativeShortDate(date))
109108
.toBe(`Сегодня, ${date.toFormat(TIME)}`);
@@ -112,7 +111,7 @@ describe('Date formatter', () => {
112111
it('tomorrow', () => {
113112
mockAdapterAndFormatterForRelativeTests();
114113

115-
const date = currentDate.plus({ days: 1, hours: 1 });
114+
const date = currentDate.plus({ days: 1 });
116115

117116
expect(formatter.relativeShortDate(date))
118117
.toBe(`Завтра, ${date.toFormat(TIME)}`);
@@ -133,15 +132,15 @@ describe('Date formatter', () => {
133132
});
134133

135134
it('after tomorrow (other year)', () => {
136-
const date = currentDate.plus({ hours: 49 });
135+
const date = currentDate.plus({ years: 1 });
137136
expect(formatter.relativeShortDate(date))
138137
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH} ${YEAR}`));
139138
});
140139
});
141140

142141
describe('Relative long (relativeLongDate method)', () => {
143142
it('before yesterday (other year)', () => {
144-
const date = currentDate.minus({ hours: 49 });
143+
const date = currentDate.minus({ years: 1 });
145144
expect(formatter.relativeLongDate(date))
146145
.toBe(adapter.format(date, `${DAY_MONTH} ${YEAR}`));
147146
});
@@ -170,7 +169,7 @@ describe('Date formatter', () => {
170169
});
171170

172171
it('today', () => {
173-
const date = adapter.today().minus({ hours: 1 });
172+
const date = adapter.today();
174173

175174
expect(formatter.relativeLongDate(date))
176175
.toBe(`Сегодня, ${date.toFormat(TIME)}`);
@@ -179,7 +178,7 @@ describe('Date formatter', () => {
179178
it('tomorrow', () => {
180179
mockAdapterAndFormatterForRelativeTests();
181180

182-
const date = currentDate.plus({ days: 1, hours: 1 });
181+
const date = currentDate.plus({ days: 1 });
183182

184183
expect(formatter.relativeLongDate(date))
185184
.toBe(`Завтра, ${date.toFormat(TIME)}`);
@@ -200,7 +199,7 @@ describe('Date formatter', () => {
200199
});
201200

202201
it('after tomorrow (other year)', () => {
203-
const date = currentDate.plus({ hours: 49 });
202+
const date = currentDate.plus({ years: 1 });
204203
expect(formatter.relativeLongDate(date))
205204
.toBe(adapter.format(date, `${DAY_MONTH} ${YEAR}`));
206205
});
@@ -358,7 +357,7 @@ describe('Date formatter', () => {
358357

359358
it('rangeShortDateTime (same day)', () => {
360359
const startDate = adapter.today();
361-
const endDate = startDate.plus({ minutes: 10 });
360+
const endDate = startDate.plus({ minutes: 1 });
362361

363362
const startString = adapter.format(startDate, `${TIME}`);
364363
const endString = adapter.format(endDate, `${TIME}, ${DAY_SHORT_MONTH}`);
@@ -369,7 +368,7 @@ describe('Date formatter', () => {
369368

370369
it('rangeShortDateTime (same day, other year)', () => {
371370
const startDate = adapter.today().minus({ years: 1 });
372-
const endDate = startDate.plus({ minutes: 10 });
371+
const endDate = startDate.plus({ minutes: 1 });
373372

374373
const startString = adapter.format(startDate, `${TIME}`);
375374
const endString = adapter.format(endDate, `${TIME}, ${DAY_SHORT_MONTH} ${YEAR}`);
@@ -482,7 +481,7 @@ describe('Date formatter', () => {
482481

483482
it('rangeLongDateTime (same day)', () => {
484483
const startDate = adapter.today();
485-
const endDate = startDate.plus({ minutes: 10 });
484+
const endDate = startDate.plus({ minutes: 1 });
486485

487486
const startString = adapter.format(
488487
startDate, `${DAY_MONTH}, ${FROM.toLocaleLowerCase()}${NBSP}${TIME}`
@@ -495,7 +494,7 @@ describe('Date formatter', () => {
495494

496495
it('rangeLongDateTime (same day, other year)', () => {
497496
const startDate = adapter.today().minus({ years: 1 });
498-
const endDate = startDate.plus({ minutes: 10 });
497+
const endDate = startDate.plus({ minutes: 1 });
499498

500499
const startString = adapter.format(
501500
startDate, `${DAY_MONTH} ${YEAR}, ${FROM.toLocaleLowerCase()}${NBSP}${TIME}`
@@ -559,7 +558,7 @@ describe('Date formatter', () => {
559558

560559
it('rangeMiddleDateTime (same day)', () => {
561560
const startDate = adapter.today();
562-
const endDate = startDate.plus({ minutes: 10 });
561+
const endDate = startDate.plus({ minutes: 1 });
563562

564563
const startString = adapter.format(startDate, `${TIME}`);
565564
const endString = adapter.format(endDate, `${TIME}, ${DAY_MONTH}`);
@@ -570,7 +569,7 @@ describe('Date formatter', () => {
570569

571570
it('rangeMiddleDateTime (same day, other year)', () => {
572571
const startDate = adapter.today().minus({ years: 1 });
573-
const endDate = startDate.plus({ minutes: 10 });
572+
const endDate = startDate.plus({ minutes: 1 });
574573

575574
const startString = adapter.format(startDate, `${TIME}`);
576575
const endString = adapter.format(endDate, `${TIME}, ${DAY_MONTH} ${YEAR}`);
@@ -812,7 +811,7 @@ describe('Date formatter', () => {
812811
describe('relative formats', () => {
813812
describe('Relative short (relativeShortDate method)', () => {
814813
it('before yesterday (other year)', () => {
815-
const date = adapter.createDate(2015).minus({ hours: 49 });
814+
const date = adapter.createDate(2015).minus({ days: 3 });
816815
expect(formatter.relativeShortDate(date))
817816
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${YEAR}`));
818817
});
@@ -842,7 +841,7 @@ describe('Date formatter', () => {
842841
});
843842

844843
it('today', () => {
845-
const date = adapter.today().minus({ hours: 1 });
844+
const date = adapter.today();
846845

847846
expect(formatter.relativeShortDate(date))
848847
.toBe(`Today, ${date.toFormat(TIME)}`);
@@ -851,7 +850,7 @@ describe('Date formatter', () => {
851850
it('tomorrow', () => {
852851
mockAdapterAndFormatterForRelativeTests();
853852

854-
const date = currentDate.plus({ days: 1, hours: 1 });
853+
const date = currentDate.plus({ days: 1 });
855854

856855
expect(formatter.relativeShortDate(date))
857856
.toBe(`Tomorrow, ${date.toFormat(TIME)}`);
@@ -872,7 +871,7 @@ describe('Date formatter', () => {
872871
});
873872

874873
it('after tomorrow (other year)', () => {
875-
const date = adapter.createDate(2015).plus({ hours: 49 });
874+
const date = adapter.createDate(2015).plus({ days: 3 });
876875
expect(formatter.relativeShortDate(date))
877876
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${YEAR}`));
878877
});
@@ -881,7 +880,7 @@ describe('Date formatter', () => {
881880

882881
describe('Relative long (relativeLongDate method)', () => {
883882
it('before yesterday (other year)', () => {
884-
const date = adapter.createDate(2015).minus({ hours: 49 });
883+
const date = adapter.createDate(2015).minus({ days: 3 });
885884
expect(formatter.relativeLongDate(date))
886885
.toBe(adapter.format(date, `${DAY_MONTH}, ${YEAR}`));
887886
});
@@ -910,7 +909,7 @@ describe('Date formatter', () => {
910909
});
911910

912911
it('today', () => {
913-
const date = adapter.today().minus({ hours: 1 });
912+
const date = adapter.today();
914913

915914
expect(formatter.relativeLongDate(date))
916915
.toBe(`Today, ${date.toFormat(TIME)}`);
@@ -919,7 +918,7 @@ describe('Date formatter', () => {
919918
it('tomorrow', () => {
920919
mockAdapterAndFormatterForRelativeTests();
921920

922-
const date = currentDate.plus({ days: 1, hours: 1 });
921+
const date = currentDate.plus({ days: 1 });
923922

924923
expect(formatter.relativeLongDate(date))
925924
.toBe(`Tomorrow, ${date.toFormat(TIME)}`);
@@ -940,7 +939,7 @@ describe('Date formatter', () => {
940939
});
941940

942941
it('after tomorrow (other year)', () => {
943-
const date = adapter.createDate(2015).plus({ hours: 49 });
942+
const date = adapter.createDate(2015).plus({ days: 3 });
944943
expect(formatter.relativeLongDate(date))
945944
.toBe(adapter.format(date, `${DAY_MONTH}, ${YEAR}`));
946945
});
@@ -1098,7 +1097,7 @@ describe('Date formatter', () => {
10981097

10991098
it('rangeShortDateTime (same day)', () => {
11001099
const startDate = adapter.today();
1101-
const endDate = startDate.plus({ minutes: 10 });
1100+
const endDate = startDate.plus({ minutes: 1 });
11021101

11031102
const startString = adapter.format(startDate, `${TIME}`);
11041103
const endString = adapter.format(endDate, `${TIME}, ${DAY_SHORT_MONTH}`);
@@ -1109,7 +1108,7 @@ describe('Date formatter', () => {
11091108

11101109
it('rangeShortDateTime (same day, other year)', () => {
11111110
const startDate = adapter.today().minus({ years: 1 });
1112-
const endDate = startDate.plus({ minutes: 10 });
1111+
const endDate = startDate.plus({ minutes: 1 });
11131112

11141113
const startString = adapter.format(startDate, `${TIME}`);
11151114
const endString = adapter.format(endDate, `${TIME}, ${DAY_SHORT_MONTH}, ${YEAR}`);
@@ -1222,7 +1221,7 @@ describe('Date formatter', () => {
12221221

12231222
it('rangeLongDateTime (same day)', () => {
12241223
const startDate = adapter.today();
1225-
const endDate = startDate.plus({ minutes: 10 });
1224+
const endDate = startDate.plus({ minutes: 1 });
12261225

12271226
const startString = adapter.format(startDate, `${DAY_MONTH}, 'from'${NBSP}${TIME}`);
12281227
const endString = adapter.format(endDate, `'to'${NBSP}${TIME}`);
@@ -1233,7 +1232,7 @@ describe('Date formatter', () => {
12331232

12341233
it('rangeLongDateTime (same day, other year)', () => {
12351234
const startDate = adapter.today().minus({ years: 1 });
1236-
const endDate = startDate.plus({ minutes: 10 });
1235+
const endDate = startDate.plus({ minutes: 1 });
12371236

12381237
const startString = adapter.format(startDate, `${DAY_MONTH}, ${YEAR}, 'from'${NBSP}${TIME}`);
12391238
const endString = adapter.format(endDate, `'to'${NBSP}${TIME}`);
@@ -1295,7 +1294,7 @@ describe('Date formatter', () => {
12951294

12961295
it('rangeMiddleDateTime (same day)', () => {
12971296
const startDate = adapter.today();
1298-
const endDate = startDate.plus({ minutes: 10 });
1297+
const endDate = startDate.plus({ minutes: 1 });
12991298

13001299
const startString = adapter.format(startDate, `${TIME}`);
13011300
const endString = adapter.format(endDate, `${TIME}, ${DAY_MONTH}`);
@@ -1306,7 +1305,7 @@ describe('Date formatter', () => {
13061305

13071306
it('rangeMiddleDateTime (same day, other year)', () => {
13081307
const startDate = adapter.today().minus({ years: 1 });
1309-
const endDate = startDate.plus({ minutes: 10 });
1308+
const endDate = startDate.plus({ minutes: 1 });
13101309

13111310
const startString = adapter.format(startDate, `${TIME}`);
13121311
const endString = adapter.format(endDate, `${TIME}, ${DAY_MONTH}, ${YEAR}`);

0 commit comments

Comments
 (0)