From c9ae32b308538824c9bb564cf7557f076116c018 Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Mon, 1 Jul 2024 16:20:30 +0900 Subject: [PATCH 1/8] =?UTF-8?q?traQ=E4=B8=8A=E3=81=AE=E3=83=A1=E3=83=83?= =?UTF-8?q?=E3=82=BB=E3=83=BC=E3=82=B8=E3=81=AE=E6=8A=95=E7=A8=BF=E3=83=BB?= =?UTF-8?q?=E7=B7=A8=E9=9B=86=E6=97=A5=E6=99=82=E3=81=AB=E5=B9=B4=E6=9C=88?= =?UTF-8?q?=E6=97=A5=E3=82=92=E9=81=A9=E5=AE=9C=E7=9C=81=E7=95=A5=E3=81=97?= =?UTF-8?q?=E3=81=A4=E3=81=A4=E4=BB=98=E3=81=91=E8=B6=B3=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/basic/date.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib/basic/date.ts b/src/lib/basic/date.ts index 20dfc2170..c1452a56c 100644 --- a/src/lib/basic/date.ts +++ b/src/lib/basic/date.ts @@ -42,12 +42,25 @@ export const getDateRepresentationWithoutSameDate = ( } export const getDisplayDate = (createdAt: string, updatedAt: string) => { - const createdDate = new Date(createdAt) + let displayDate = new Date(createdAt) if (createdAt === updatedAt) { - return getTimeString(createdDate) + displayDate = new Date(updatedAt) + } + const today = new Date() + const timeString = getTimeString(displayDate) + const yesterday = new Date(today) + yesterday.setDate(today.getDate() - 1) + + if (getFullDayString(displayDate) === getFullDayString(today)) { + return '今日' + ' ' + timeString + } + if (getFullDayString(displayDate) === getFullDayString(yesterday)) { + return '昨日' + ' ' + timeString + } + if (displayDate.getFullYear() === today.getFullYear()) { + return getDayString(displayDate) + ' ' + timeString } else { - const updatedDate = new Date(updatedAt) - return getDateRepresentationWithoutSameDate(updatedDate, createdDate) + return getFullDayString(displayDate) + ' ' + timeString } } From 8e949db25d8eca56a0340efcf0b89a9583da587d Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Mon, 1 Jul 2024 17:10:18 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE?= =?UTF-8?q?=E3=83=9F=E3=82=B9=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/basic/date.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/basic/date.ts b/src/lib/basic/date.ts index c1452a56c..088d6bbe9 100644 --- a/src/lib/basic/date.ts +++ b/src/lib/basic/date.ts @@ -43,7 +43,7 @@ export const getDateRepresentationWithoutSameDate = ( export const getDisplayDate = (createdAt: string, updatedAt: string) => { let displayDate = new Date(createdAt) - if (createdAt === updatedAt) { + if (createdAt !== updatedAt) { displayDate = new Date(updatedAt) } const today = new Date() From 6c40343ba31cf460375fe9ca8ec568a97491b2f7 Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Thu, 4 Jul 2024 22:16:46 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B1?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=82=92=E6=96=B0=E3=81=97=E3=81=84=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=81=95=E3=81=9B=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/lib/basic/date.spec.ts | 40 +++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/unit/lib/basic/date.spec.ts b/tests/unit/lib/basic/date.spec.ts index 5f76167c8..38b820dc6 100644 --- a/tests/unit/lib/basic/date.spec.ts +++ b/tests/unit/lib/basic/date.spec.ts @@ -94,18 +94,38 @@ describe('getDateRepresentationWithoutSameDate', () => { }) describe('getDisplayDate', () => { - const dateISO = '2001-04-04T05:20:34' + const today = new Date() + const year = today.getFullYear().toString() + const month = (today.getMonth() + 1).toString().padStart(2, '0') + const day = today.getDate().toString().padStart(2, '0') - it('should get time string when not modified', () => { - expect(getDisplayDate(dateISO, dateISO)).toBe('05:20') - }) - it('should get time string when same date', () => { - const dateISO2 = '2001-04-04T08:25:34' - expect(getDisplayDate(dateISO, dateISO2)).toBe('08:25') + const createdDateISO = '2014-04-01T10:20:30' + const updatedDate = today + updatedDate.setHours(12) + updatedDate.setMinutes(34) + + it('should say 今日 when updated today', () => { + const updatedDateISO = updatedDate.toISOString() + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 12:34') + }) + it('should say 昨日 when updated yesterday', () => { + updatedDate.setDate(today.getDate() - 1) + const updatedDateISO = updatedDate.toISOString() + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 12:34') + }) + it('should get date string when updated in the same year', () => { + updatedDate.setDate(today.getDate() - 2) + updatedDate.setFullYear(today.getFullYear()) + const updatedDateISO = updatedDate.toISOString() + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( + month + '/' + day + ' 12:34' + ) }) - it('should get date string when not same date', () => { - const dateISO2 = '2001-06-04T08:25:34' - expect(getDisplayDate(dateISO, dateISO2)).toBe('06/04 08:25') + it('should get FULL date string when updated before last year', () => { + const updatedDateISO = '2014-05-01T10:20:30' + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( + '2014/05/01 10:20' + ) }) }) From 0fb8434e4219f642f0dace90aac420297b594404 Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Thu, 4 Jul 2024 22:22:38 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B1?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=81=AE=E6=97=A5=E4=BB=98=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=81=AE=E3=83=9F=E3=82=B9=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/lib/basic/date.spec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/unit/lib/basic/date.spec.ts b/tests/unit/lib/basic/date.spec.ts index 38b820dc6..52b184acd 100644 --- a/tests/unit/lib/basic/date.spec.ts +++ b/tests/unit/lib/basic/date.spec.ts @@ -95,9 +95,6 @@ describe('getDateRepresentationWithoutSameDate', () => { describe('getDisplayDate', () => { const today = new Date() - const year = today.getFullYear().toString() - const month = (today.getMonth() + 1).toString().padStart(2, '0') - const day = today.getDate().toString().padStart(2, '0') const createdDateISO = '2014-04-01T10:20:30' const updatedDate = today @@ -116,6 +113,8 @@ describe('getDisplayDate', () => { it('should get date string when updated in the same year', () => { updatedDate.setDate(today.getDate() - 2) updatedDate.setFullYear(today.getFullYear()) + const month = (updatedDate.getMonth() + 1).toString().padStart(2, '0') + const day = updatedDate.getDate().toString().padStart(2, '0') const updatedDateISO = updatedDate.toISOString() expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( month + '/' + day + ' 12:34' From 17046612c23d63904910a82b5c76f2f6807e8ac2 Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Fri, 5 Jul 2024 07:35:17 +0900 Subject: [PATCH 5/8] =?UTF-8?q?faketimers=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=81=A6test=E3=82=92=E6=9B=B8=E3=81=8D=E7=9B=B4=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/lib/basic/date.spec.ts | 40 ++++++++++++------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/tests/unit/lib/basic/date.spec.ts b/tests/unit/lib/basic/date.spec.ts index 52b184acd..8ff301c19 100644 --- a/tests/unit/lib/basic/date.spec.ts +++ b/tests/unit/lib/basic/date.spec.ts @@ -94,37 +94,27 @@ describe('getDateRepresentationWithoutSameDate', () => { }) describe('getDisplayDate', () => { - const today = new Date() + beforeEach(() => {vi.useFakeTimers()}) + afterEach(() => {vi.useRealTimers()}) - const createdDateISO = '2014-04-01T10:20:30' - const updatedDate = today - updatedDate.setHours(12) - updatedDate.setMinutes(34) + const createdDateISO = '2010-04-01T12:34:56' + const updatedDateISO = '2010-05-02T14:28:57' it('should say 今日 when updated today', () => { - const updatedDateISO = updatedDate.toISOString() - expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 12:34') + vi.setSystemTime('2010-05-02T15:00:00') + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 14:28') }) it('should say 昨日 when updated yesterday', () => { - updatedDate.setDate(today.getDate() - 1) - const updatedDateISO = updatedDate.toISOString() - expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 12:34') - }) - it('should get date string when updated in the same year', () => { - updatedDate.setDate(today.getDate() - 2) - updatedDate.setFullYear(today.getFullYear()) - const month = (updatedDate.getMonth() + 1).toString().padStart(2, '0') - const day = updatedDate.getDate().toString().padStart(2, '0') - const updatedDateISO = updatedDate.toISOString() - expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( - month + '/' + day + ' 12:34' - ) + vi.setSystemTime('2010-05-03T15:00:00') + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 14:28') }) - it('should get FULL date string when updated before last year', () => { - const updatedDateISO = '2014-05-01T10:20:30' - expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( - '2014/05/01 10:20' - ) + it('should get MM/DD when updated in the same year', () => { + vi.setSystemTime('2010-07-07T15:00:00') + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('05/02 14:28') + }) + it('should get YYYY/MM/DD when updated before last year', () => { + vi.setSystemTime('2015-10-10T15:00:00') + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('2010/05/02 14:28') }) }) From a35b1b0ca219fd96ae996ad48ae4544e4c23bc45 Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Fri, 5 Jul 2024 07:37:59 +0900 Subject: [PATCH 6/8] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/lib/basic/date.spec.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/unit/lib/basic/date.spec.ts b/tests/unit/lib/basic/date.spec.ts index 8ff301c19..4ffb7240e 100644 --- a/tests/unit/lib/basic/date.spec.ts +++ b/tests/unit/lib/basic/date.spec.ts @@ -94,8 +94,12 @@ describe('getDateRepresentationWithoutSameDate', () => { }) describe('getDisplayDate', () => { - beforeEach(() => {vi.useFakeTimers()}) - afterEach(() => {vi.useRealTimers()}) + beforeEach(() => { + vi.useFakeTimers() + }) + afterEach(() => { + vi.useRealTimers() + }) const createdDateISO = '2010-04-01T12:34:56' const updatedDateISO = '2010-05-02T14:28:57' @@ -114,7 +118,9 @@ describe('getDisplayDate', () => { }) it('should get YYYY/MM/DD when updated before last year', () => { vi.setSystemTime('2015-10-10T15:00:00') - expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('2010/05/02 14:28') + expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe( + '2010/05/02 14:28' + ) }) }) From 292f68ae5d283da045ed485008a9dd686be1190a Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Sun, 7 Jul 2024 17:34:26 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=E5=B8=B8=E3=81=ABupdatedAt=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=83=BByesterday=E3=81=AE=E5=86=8D=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9=E3=81=AE=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/basic/date.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib/basic/date.ts b/src/lib/basic/date.ts index 088d6bbe9..f8558cb87 100644 --- a/src/lib/basic/date.ts +++ b/src/lib/basic/date.ts @@ -42,14 +42,10 @@ export const getDateRepresentationWithoutSameDate = ( } export const getDisplayDate = (createdAt: string, updatedAt: string) => { - let displayDate = new Date(createdAt) - if (createdAt !== updatedAt) { - displayDate = new Date(updatedAt) - } + const displayDate = new Date(updatedAt) const today = new Date() const timeString = getTimeString(displayDate) - const yesterday = new Date(today) - yesterday.setDate(today.getDate() - 1) + const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24) if (getFullDayString(displayDate) === getFullDayString(today)) { return '今日' + ' ' + timeString From 348034738269ef1994ba43ed989163b747bc0a9b Mon Sep 17 00:00:00 2001 From: kitsne241 Date: Sun, 7 Jul 2024 18:07:07 +0900 Subject: [PATCH 8/8] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=AE=E5=A0=B4=E5=90=88=E5=88=86=E3=81=91?= =?UTF-8?q?=E3=81=ABgetFullDayString=E3=82=92=E4=B8=8D=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/basic/date.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/basic/date.ts b/src/lib/basic/date.ts index f8558cb87..7d3fb3f31 100644 --- a/src/lib/basic/date.ts +++ b/src/lib/basic/date.ts @@ -47,10 +47,18 @@ export const getDisplayDate = (createdAt: string, updatedAt: string) => { const timeString = getTimeString(displayDate) const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24) - if (getFullDayString(displayDate) === getFullDayString(today)) { + if ( + displayDate.getFullYear() === today.getFullYear() && + displayDate.getMonth() === today.getMonth() && + displayDate.getDate() === today.getDate() + ) { return '今日' + ' ' + timeString } - if (getFullDayString(displayDate) === getFullDayString(yesterday)) { + if ( + displayDate.getFullYear() === yesterday.getFullYear() && + displayDate.getMonth() === yesterday.getMonth() && + displayDate.getDate() === yesterday.getDate() + ) { return '昨日' + ' ' + timeString } if (displayDate.getFullYear() === today.getFullYear()) {