forked from cinnyapp/cinny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/datetime' into evergreen
- Loading branch information
Showing
12 changed files
with
143 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
|
||
import { isInSameDay, DateTime } from '../../../util/time'; | ||
|
||
interface TimeProps { | ||
timestamp: number; | ||
fullTime?: boolean; | ||
} | ||
|
||
function Time({ timestamp, fullTime }: TimeProps) { | ||
const date = new Date(timestamp); | ||
|
||
const formattedFullTime = DateTime.full(date); | ||
let formattedDate = formattedFullTime; | ||
|
||
if (!fullTime) { | ||
const compareDate = new Date(); | ||
const isToday = isInSameDay(date, compareDate); | ||
compareDate.setDate(compareDate.getDate() - 1); | ||
const isYesterday = isInSameDay(date, compareDate); | ||
|
||
formattedDate = isToday || isYesterday ? DateTime.time(date) : DateTime.dateISO(date); | ||
if (isYesterday) { | ||
const yesterday = DateTime.relative(-1, 'day') ?? 'Yesterday'; | ||
formattedDate = `${yesterday}, ${formattedDate}`; | ||
} | ||
} | ||
|
||
return ( | ||
<time dateTime={date.toISOString()} title={formattedFullTime}> | ||
{formattedDate} | ||
</time> | ||
); | ||
} | ||
|
||
export default Time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import cons from '../client/state/cons'; | ||
import settings from '../client/state/settings'; | ||
|
||
function capitalize(string: string) { | ||
return string.charAt(0).toLocaleUpperCase() + string.slice(1); | ||
} | ||
|
||
const hourCycle = () => (settings.isTime12 ? 'h12' : 'h23'); | ||
|
||
let fullFormat: Intl.DateTimeFormat; | ||
let dateFormat: Intl.DateTimeFormat; | ||
let timeFormat: Intl.DateTimeFormat; | ||
let relativeFormat: Intl.RelativeTimeFormat | undefined; | ||
|
||
function initDateFormats() { | ||
fullFormat = new Intl.DateTimeFormat('en', { | ||
hourCycle: hourCycle(), | ||
dateStyle: 'full', | ||
timeStyle: 'short', | ||
}); | ||
|
||
dateFormat = new Intl.DateTimeFormat('en', { | ||
dateStyle: 'long', | ||
}); | ||
|
||
timeFormat = new Intl.DateTimeFormat('en', { | ||
hourCycle: hourCycle(), | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
}); | ||
|
||
relativeFormat = Intl.RelativeTimeFormat | ||
? new Intl.RelativeTimeFormat('en', { numeric: 'auto' }) | ||
: undefined; | ||
} | ||
initDateFormats(); | ||
|
||
export class DateTime { | ||
static full = (date: Date) => fullFormat.format(date); | ||
|
||
static date = (date: Date) => dateFormat.format(date); | ||
|
||
static time = (date: Date) => timeFormat.format(date); | ||
|
||
static relative = (value: number, unit: Intl.RelativeTimeFormatUnit) => | ||
relativeFormat ? capitalize(relativeFormat.format(value, unit)) : undefined; | ||
|
||
static dateISO = (date: Date) => date.toISOString().split('T')[0]; | ||
} | ||
|
||
export function diffMinutes(dt2: Date, dt1: Date): number { | ||
let diff = (dt2.getTime() - dt1.getTime()) / 1000; | ||
diff /= 60; | ||
return Math.abs(Math.round(diff)); | ||
} | ||
|
||
export function isInSameDay(dt2: Date, dt1: Date): boolean { | ||
return ( | ||
dt2.getFullYear() === dt1.getFullYear() && | ||
dt2.getMonth() === dt1.getMonth() && | ||
dt2.getDate() === dt1.getDate() | ||
); | ||
} | ||
|
||
settings.on(cons.events.settings.TIME12_TOGGLED, () => { | ||
initDateFormats(); | ||
}); |