Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Moment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<script>
import moment from '@nextcloud/moment'
import { shortRelativeDatetime } from '../util/shortRelativeDatetime.js'
import { longRelativeDatetime } from '../util/shortRelativeDatetime.js'

export default {
name: 'Moment',
Expand All @@ -30,7 +30,7 @@ export default {
},

formatted() {
return shortRelativeDatetime(new Date(this.timestamp * 1000))
return longRelativeDatetime(new Date(this.timestamp * 1000))
},
},
}
Expand Down
26 changes: 26 additions & 0 deletions src/tests/unit/util/shortRelativeDatetime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ describe('shortRelativeDatetime', () => {
expect(formatted).toBe('9:27')
})

it('shortens yesterdays time', () => {
const ref = new Date()
ref.setFullYear(2020, 1, 13)
ref.setHours(13, 14)
const d = new Date()
d.setFullYear(2020, 1, 12)
d.setHours(9, 27)

const formatted = shortDatetime(ref, d)

expect(formatted).toBe('9:27')
})

it('shortens yesterdays time with label', () => {
const ref = new Date()
ref.setFullYear(2020, 1, 13)
ref.setHours(13, 14)
const d = new Date()
d.setFullYear(2020, 1, 12)
d.setHours(9, 27)

const formatted = shortDatetime(ref, d, true)

expect(formatted).toBe('Yesterday 9:27')
})

it('shortens this weeks day', () => {
const ref = new Date()
ref.setFullYear(2020, 1, 13)
Expand Down
19 changes: 14 additions & 5 deletions src/util/shortRelativeDatetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@

import moment from '@nextcloud/moment'
import curry from 'lodash/fp/curry.js'
import { translate as t } from '@nextcloud/l10n'

Check failure on line 8 in src/util/shortRelativeDatetime.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected "@nextcloud/l10n" to come before "lodash/fp/curry.js"

export const shortDatetime = curry((ref, date) => {
export const shortDatetime = curry((ref, date, withLabel = false) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The withLabel flag has only an effect when the date is yesterday.

I think this should be done in the component itself, or if required in more places a own function.

The purpose of the shortDatetime function should stay with shorting a date ;)

const momentDate = moment(date)
const startOfToday = new Date(ref.getFullYear(), ref.getMonth(), ref.getDate())
const startOfYesterday = new Date(startOfToday)
startOfYesterday.setDate(startOfYesterday.getDate() - 1)
// Within the same day?
if (ref.getFullYear() === date.getFullYear()
&& ref.getMonth() === date.getMonth()
&& ref.getDate() === date.getDate()) {
if (date >= startOfToday) {
return momentDate.format('H:mm')
}
// Yesterday?
if (date >= startOfYesterday) {
return withLabel
? t('mail', 'Yesterday') + ' ' + momentDate.format('H:mm')
: momentDate.format('H:mm')
}
// Within the previous week?
if (date.getTime() > (ref.getTime() - 30 * 60 * 24 * 7 * 1000)) {
return momentDate.format('dd')
Expand All @@ -30,4 +38,5 @@
return moment(date * 1000).format('lll')
}

export const shortRelativeDatetime = shortDatetime(new Date())
export const shortRelativeDatetime = (date) => shortDatetime(new Date(), date, false)
export const longRelativeDatetime = (date) => shortDatetime(new Date(), date, true)
Loading