forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatChartTimes.ts
More file actions
53 lines (44 loc) · 1.85 KB
/
Copy pathformatChartTimes.ts
File metadata and controls
53 lines (44 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { NumberValue } from 'd3'
const createTimeFormatter = (timestamp: NumberValue, locale: string, options: Intl.DateTimeFormatOptions) =>
new Date(timestamp.valueOf() * 1000).toLocaleTimeString(locale, options)
export const hourFormatter = (locale: string) => (timestamp: NumberValue) =>
createTimeFormatter(timestamp, locale, {
hour: 'numeric',
minute: 'numeric',
hour12: true,
})
export const dayHourFormatter = (locale: string) => (timestamp: NumberValue) =>
createTimeFormatter(timestamp, locale, {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true,
})
const createDateFormatter = (timestamp: NumberValue, locale: string, options: Intl.DateTimeFormatOptions) =>
new Date(timestamp.valueOf() * 1000).toLocaleDateString(locale, options)
export const monthDayFormatter = (locale: string) => (timestamp: NumberValue) =>
createDateFormatter(timestamp, locale, {
month: 'long',
day: 'numeric',
})
export const monthYearDayFormatter = (locale: string) => (timestamp: NumberValue) =>
createDateFormatter(timestamp, locale, {
month: 'short',
year: 'numeric',
day: 'numeric',
})
export const monthTickFormatter = (locale: string) => (timestamp: NumberValue) => {
let date = new Date(timestamp.valueOf() * 1000)
// when a tick maps to a date not on the first of the month, modify the tick to the closest
// first of month date. For example, Dec 31 becomes Jan 1, and Dec 5 becomes Dec 1.
if (date.getDate() !== 1) {
date =
date.getDate() >= 15
? new Date(date.getFullYear(), date.getMonth() + 1, 1)
: new Date(date.getFullYear(), date.getMonth(), 1)
}
return date.toLocaleDateString(locale, { month: 'long' })
}
export const weekFormatter = (locale: string) => (timestamp: NumberValue) =>
createDateFormatter(timestamp, locale, { weekday: 'long' })