Skip to content

Commit

Permalink
Merge pull request #4338 from traPtitech/show_full_date_of_message
Browse files Browse the repository at this point in the history
traQ上のメッセージの投稿・編集日時に年月日を適宜省略しつつ付け足す
  • Loading branch information
kitsne241 committed Jul 22, 2024
2 parents a3e2391 + 3480347 commit 43f1d5d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
27 changes: 22 additions & 5 deletions src/lib/basic/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,29 @@ export const getDateRepresentationWithoutSameDate = (
}

export const getDisplayDate = (createdAt: string, updatedAt: string) => {
const createdDate = new Date(createdAt)
if (createdAt === updatedAt) {
return getTimeString(createdDate)
const displayDate = new Date(updatedAt)
const today = new Date()
const timeString = getTimeString(displayDate)
const yesterday = new Date(today.getTime() - 1000 * 60 * 60 * 24)

if (
displayDate.getFullYear() === today.getFullYear() &&
displayDate.getMonth() === today.getMonth() &&
displayDate.getDate() === today.getDate()
) {
return '今日' + ' ' + timeString
}
if (
displayDate.getFullYear() === yesterday.getFullYear() &&
displayDate.getMonth() === yesterday.getMonth() &&
displayDate.getDate() === yesterday.getDate()
) {
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
}
}

Expand Down
35 changes: 25 additions & 10 deletions tests/unit/lib/basic/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,33 @@ describe('getDateRepresentationWithoutSameDate', () => {
})

describe('getDisplayDate', () => {
const dateISO = '2001-04-04T05:20:34'

it('should get time string when not modified', () => {
expect(getDisplayDate(dateISO, dateISO)).toBe('05:20')
beforeEach(() => {
vi.useFakeTimers()
})
it('should get time string when same date', () => {
const dateISO2 = '2001-04-04T08:25:34'
expect(getDisplayDate(dateISO, dateISO2)).toBe('08:25')
afterEach(() => {
vi.useRealTimers()
})
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')

const createdDateISO = '2010-04-01T12:34:56'
const updatedDateISO = '2010-05-02T14:28:57'

it('should say 今日 when updated today', () => {
vi.setSystemTime('2010-05-02T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('今日 14:28')
})
it('should say 昨日 when updated yesterday', () => {
vi.setSystemTime('2010-05-03T15:00:00')
expect(getDisplayDate(createdDateISO, updatedDateISO)).toBe('昨日 14:28')
})
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'
)
})
})

Expand Down

0 comments on commit 43f1d5d

Please sign in to comment.