Skip to content

Commit f938e38

Browse files
committed
feat(timestamp): add UTCTimestamp and createTimestamp utility functions
1 parent 8976cc1 commit f938e38

File tree

4 files changed

+165
-61
lines changed

4 files changed

+165
-61
lines changed

packages/timestamp/package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/timestamp/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@
6262
"@testyard/stats": "^1.4.1",
6363
"@tsconfig/node16": "^16.1.1",
6464
"@types/jest": "^29.5.4",
65+
"@types/luxon": "^3.3.2",
6566
"jest": "^29.6.4",
6667
"jest-extended": "^4.0.1",
68+
"luxon": "^3.4.3",
69+
"moment": "^2.29.4",
6770
"rollup": "^3.28.1",
6871
"semantic-release": "^19.0.5",
6972
"semantic-release-monorepo": "^7.0.5",

packages/timestamp/src/index.ts

Lines changed: 86 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,105 @@
1+
import DateAdapter from './utils/DateAdapter'
2+
13
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'
1110

1211
// LOG -> ISO 8601 -> '2017-09-14T03:20:34.091-04:00'
1312

14-
type Preset = 'LOG' | 'LOG-SHORT' | 'FILE' | 'FILE-SHORT'
13+
// -----------------------------------------------------------------------------
1514

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
2019
}
2120

21+
// -----------------------------------------------------------------------------
22+
2223
function timestamp (): string
23-
function timestamp (format: string): string
24+
2425
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
2632

2733
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)
5035

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')
5738
}
5839

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+
}
6852

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+
}
7556
}
7657

77-
return timestamp()
58+
return getPreset(dateAdapter, 'LOG')
7859
}
7960

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+
// })()

packages/timestamp/test/index.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { DateTime } from 'luxon'
2+
13
import timestamp from '../src'
24

5+
const localDate = DateTime.now()
6+
37
describe('@extended/timestamp', () => {
48
test('default timestamp (YYYY-MM-DDTHH:mm:ss:ms+##:##)', () => {
59
expect(timestamp()).toMatch(
@@ -38,4 +42,49 @@ describe('@extended/timestamp', () => {
3842
)
3943
})
4044
})
45+
46+
describe('formatting', () => {
47+
describe('single patterns', () => {
48+
test('year (YYYY)', () => {
49+
expect(timestamp('YYYY')).toBe(localDate.toFormat('yyyy'))
50+
})
51+
52+
test('month (MM)', () => {
53+
expect(timestamp('MM')).toBe(localDate.toFormat('MM'))
54+
})
55+
56+
test('day (DD)', () => {
57+
expect(timestamp('DD')).toBe(localDate.toFormat('dd'))
58+
})
59+
60+
test('hours (HH)', () => {
61+
expect(timestamp('HH')).toBe(localDate.toFormat('HH'))
62+
})
63+
64+
test('minutes (mm)', () => {
65+
expect(timestamp('mm')).toBe(localDate.toFormat('mm'))
66+
})
67+
68+
test('seconds (ss)', () => {
69+
expect(timestamp('ss')).toBe(localDate.toFormat('ss'))
70+
})
71+
72+
test('milliseconds (ms)', () => {
73+
expect(timestamp('ms')).toMatch(/^\d{3}$/)
74+
})
75+
})
76+
77+
describe('multiple patterns', () => {
78+
test('year-month-day (YYYY-MM-DD)', () => {
79+
expect(timestamp('YYYY-MM-DD')).toBe(localDate.toFormat('yyyy-MM-dd'))
80+
})
81+
})
82+
})
83+
84+
describe('UTC timestamp', () => {
85+
test('x', () => {
86+
console.log(timestamp.UTCTimestamp())
87+
// expect(timestamp.UTCTimestamp())
88+
})
89+
})
4190
})

0 commit comments

Comments
 (0)