add generic date pivot helper
This commit is contained in:
parent
46274fba4f
commit
26762efcea
|
@ -4,7 +4,7 @@ import { msatsToSats } from '../../lib/format'
|
|||
import { bioSchema, emailSchema, settingsSchema, ssValidate, userSchema } from '../../lib/validate'
|
||||
import { createMentions, getItem, SELECT, updateItem, filterClause } from './item'
|
||||
import serialize from './serial'
|
||||
import { dayPivot } from '../../lib/time'
|
||||
import { datePivot } from '../../lib/time'
|
||||
|
||||
export function within (table, within) {
|
||||
let interval = ' AND "' + table + '".created_at >= $1 - INTERVAL '
|
||||
|
@ -54,13 +54,13 @@ export function viewWithin (table, within) {
|
|||
export function withinDate (within) {
|
||||
switch (within) {
|
||||
case 'day':
|
||||
return dayPivot(new Date(), -1)
|
||||
return datePivot(new Date(), { days: -1 })
|
||||
case 'week':
|
||||
return dayPivot(new Date(), -7)
|
||||
return datePivot(new Date(), { days: -7 })
|
||||
case 'month':
|
||||
return dayPivot(new Date(), -30)
|
||||
return datePivot(new Date(), { days: -30 })
|
||||
case 'year':
|
||||
return dayPivot(new Date(), -365)
|
||||
return datePivot(new Date(), { days: -365 })
|
||||
default:
|
||||
return new Date(0)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Snl ({ ignorePreference }) {
|
|||
|
||||
useEffect(() => {
|
||||
const dismissed = window.localStorage.getItem('snl')
|
||||
if (!ignorePreference && dismissed && dismissed > new Date(dismissed) < dayPivot(new Date(), -6)) {
|
||||
if (!ignorePreference && dismissed && dismissed > new Date(dismissed) < datePivot(new Date(), { days: -6 })) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ export const defaultCommentSort = (pinned, bio, createdAt) => {
|
|||
// pins sort by recent
|
||||
if (pinned) return 'recent'
|
||||
// old items (that aren't bios) sort by top
|
||||
if (!bio && new Date(createdAt) < dayPivot(new Date(), -OLD_ITEM_DAYS)) return 'top'
|
||||
if (!bio && new Date(createdAt) < datePivot(new Date(), { days: -OLD_ITEM_DAYS })) return 'top'
|
||||
// everything else sorts by hot
|
||||
return 'hot'
|
||||
}
|
||||
|
|
13
lib/time.js
13
lib/time.js
|
@ -20,8 +20,17 @@ export function timeSince (timeStamp) {
|
|||
return 'now'
|
||||
}
|
||||
|
||||
export function dayPivot (date, days) {
|
||||
return new Date(date.getTime() + days * 24 * 60 * 60 * 1000)
|
||||
export function datePivot (date,
|
||||
{ years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) {
|
||||
return new Date(
|
||||
date.getFullYear() + years,
|
||||
date.getMonth() + months,
|
||||
date.getDate() + days,
|
||||
date.getHours() + hours,
|
||||
date.getMinutes() + minutes,
|
||||
date.getSeconds() + seconds,
|
||||
date.getMilliseconds() + milliseconds
|
||||
)
|
||||
}
|
||||
|
||||
export function timeLeft (timeStamp) {
|
||||
|
|
Loading…
Reference in New Issue