|
| 1 | +import DateAdapter from './utils/DateAdapter' |
| 2 | + |
1 | 3 | import { |
2 | | - getYear, |
3 | | - getMonth, |
4 | | - getDay, |
5 | | - getHour, |
6 | | - getMinutes, |
7 | | - getSeconds, |
8 | | - getMilliseconds, |
9 | | - getOffset |
10 | | -} from './utils' |
| 4 | + type Preset, |
| 5 | + isPreset, |
| 6 | + getPreset |
| 7 | +} from './utils/preset' |
| 8 | + |
| 9 | +import format from './utils/format' |
11 | 10 |
|
12 | 11 | // LOG -> ISO 8601 -> '2017-09-14T03:20:34.091-04:00' |
13 | 12 |
|
14 | | -type Preset = 'LOG' | 'LOG-SHORT' | 'FILE' | 'FILE-SHORT' |
| 13 | +// ----------------------------------------------------------------------------- |
15 | 14 |
|
16 | | -interface Options { |
17 | | - preset?: Preset |
18 | | - format?: string |
19 | | - from?: string |
| 15 | +interface Options<T extends string> { |
| 16 | + format?: T |
| 17 | + UTC?: boolean |
| 18 | + from?: string | Date |
20 | 19 | } |
21 | 20 |
|
| 21 | +// ----------------------------------------------------------------------------- |
| 22 | + |
22 | 23 | function timestamp (): string |
23 | | -function timestamp (format: string): string |
| 24 | + |
24 | 25 | function timestamp (preset: Preset): string |
25 | | -function timestamp (options: Options): string |
| 26 | + |
| 27 | +function timestamp (format: string): string |
| 28 | + |
| 29 | +function timestamp (options: Options<Preset>): string |
| 30 | + |
| 31 | +function timestamp (options: Options<string>): string |
26 | 32 |
|
27 | 33 | function timestamp (arg1?: any): string { |
28 | | - const date = new Date() |
29 | | - // // var offset = localDate.getTimezoneOffset(); |
30 | | - // // var utc = new Date(localDate.getTime() - offset * 60000); |
31 | | - |
32 | | - // // console.log(offset) |
33 | | - // // console.log(utc) |
34 | | - |
35 | | - // console.log(new Date().toString()) |
36 | | - // console.log(new Date().toISOString()) |
37 | | - |
38 | | - // console.log(new Date().getHours()) |
39 | | - // console.log(new Date().getUTCHours()) |
40 | | - |
41 | | - if (typeof arg1 === 'undefined' || arg1 === 'LOG') { |
42 | | - return ( |
43 | | - getYear(date) + '-' + getMonth(date) + '-' + getDay(date) + |
44 | | - 'T' + |
45 | | - getHour(date) + ':' + getMinutes(date) + ':' + getSeconds(date) + |
46 | | - '.' + getMilliseconds(date) + |
47 | | - getOffset(date) |
48 | | - ) |
49 | | - } |
| 34 | + const dateAdapter = new DateAdapter(new Date(), false) |
50 | 35 |
|
51 | | - if (arg1 === 'LOG-SHORT') { |
52 | | - return ( |
53 | | - getYear(date) + '-' + getMonth(date) + '-' + getDay(date) + |
54 | | - 'T' + |
55 | | - getHour(date) + ':' + getMinutes(date) + ':' + getSeconds(date) |
56 | | - ) |
| 36 | + if (typeof arg1 === 'undefined') { |
| 37 | + return getPreset(dateAdapter, 'LOG') |
57 | 38 | } |
58 | 39 |
|
59 | | - if (arg1 === 'FILE') { |
60 | | - return ( |
61 | | - getYear(date) + '-' + getMonth(date) + '-' + getDay(date) + |
62 | | - 'T' + |
63 | | - getHour(date) + '-' + getMinutes(date) + '-' + getSeconds(date) + |
64 | | - '-' + getMilliseconds(date) + |
65 | | - getOffset(date, '-') |
66 | | - ) |
67 | | - } |
| 40 | + if (typeof arg1 === 'string') { |
| 41 | + return isPreset(arg1) |
| 42 | + ? getPreset(dateAdapter, arg1 as Preset) |
| 43 | + : format(arg1, dateAdapter) |
| 44 | + } else if (typeof arg1 === 'object' && arg1 !== null) { |
| 45 | + if (typeof arg1.UTC === 'boolean') { |
| 46 | + dateAdapter.UTC = arg1.UTC |
| 47 | + } |
| 48 | + |
| 49 | + if (isPreset(arg1.preset)) { |
| 50 | + return getPreset(dateAdapter, arg1 as Preset) |
| 51 | + } |
68 | 52 |
|
69 | | - if (arg1 === 'FILE-SHORT') { |
70 | | - return ( |
71 | | - getYear(date) + '-' + getMonth(date) + '-' + getDay(date) + |
72 | | - 'T' + |
73 | | - getHour(date) + '-' + getMinutes(date) + '-' + getSeconds(date) |
74 | | - ) |
| 53 | + if (typeof arg1.format === 'string') { |
| 54 | + return format(arg1.format, dateAdapter) |
| 55 | + } |
75 | 56 | } |
76 | 57 |
|
77 | | - return timestamp() |
| 58 | + return getPreset(dateAdapter, 'LOG') |
78 | 59 | } |
79 | 60 |
|
80 | | -export default timestamp |
| 61 | +// ----------------------------------------------------------------------------- |
| 62 | + |
| 63 | +function UTCTimestamp (): string |
| 64 | + |
| 65 | +function UTCTimestamp (preset: Preset): string |
| 66 | + |
| 67 | +function UTCTimestamp (format: string): string |
| 68 | + |
| 69 | +function UTCTimestamp (options: Options<Preset>): string |
| 70 | + |
| 71 | +function UTCTimestamp (options: Options<string>): string |
| 72 | + |
| 73 | +function UTCTimestamp (arg1?: any): string { |
| 74 | + return timestamp({ UTC: true }) |
| 75 | +} |
| 76 | + |
| 77 | +// ----------------------------------------------------------------------------- |
| 78 | + |
| 79 | +function createTimestamp (preset: Preset): typeof timestamp |
| 80 | + |
| 81 | +function createTimestamp (format: string): typeof timestamp |
| 82 | + |
| 83 | +function createTimestamp (options: Options<Preset>): typeof timestamp |
| 84 | + |
| 85 | +function createTimestamp (options: Options<string>): typeof timestamp |
| 86 | + |
| 87 | +function createTimestamp (arg1?: any): typeof timestamp { |
| 88 | + return () => timestamp() |
| 89 | +} |
| 90 | + |
| 91 | +// ----------------------------------------------------------------------------- |
| 92 | + |
| 93 | +const extendedTimestamp: typeof timestamp & { |
| 94 | + createTimestamp: typeof createTimestamp |
| 95 | + UTCTimestamp: typeof UTCTimestamp |
| 96 | +} = timestamp as any |
| 97 | + |
| 98 | +extendedTimestamp.createTimestamp = createTimestamp |
| 99 | +extendedTimestamp.UTCTimestamp = UTCTimestamp |
| 100 | + |
| 101 | +export default extendedTimestamp |
| 102 | + |
| 103 | +// ;(() => { |
| 104 | +// timestamp('-YYYY-MM-') |
| 105 | +// })() |
0 commit comments