|
|
@@ -86,4 +86,78 @@ describe('format.js', () => {
|
|
|
expect(format.formatScore(undefined)).toBe('--')
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ // --------------- parseDate ---------------
|
|
|
+
|
|
|
+ describe('parseDate', () => {
|
|
|
+ // 断言使用本地时间字段(getFullYear/getMonth/getDate/...),
|
|
|
+ // 避免 Node(V8)对纯日期字符串按 UTC 解析带来的时区差异。
|
|
|
+ const ymd = (d) => {
|
|
|
+ return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
|
|
|
+ }
|
|
|
+
|
|
|
+ test('parses MySQL DATETIME with space separator "yyyy-MM-dd HH:mm"', () => {
|
|
|
+ // iOS 原生 new Date() 无法解析空格分隔格式,parseDate 必须归一化成功
|
|
|
+ const d = format.parseDate('2026-08-22 14:00')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ expect(d.getHours()).toBe(14)
|
|
|
+ expect(d.getMinutes()).toBe(0)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses MySQL DATETIME with seconds', () => {
|
|
|
+ const d = format.parseDate('2026-08-22 14:00:30')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ expect(d.getSeconds()).toBe(30)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses Java "EEE MMM dd HH:mm:ss zzz yyyy" (CST) format', () => {
|
|
|
+ // 后端 Java 常见格式,iOS 原生无法解析
|
|
|
+ const d = format.parseDate('Sat Aug 22 14:00:00 CST 2026')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ expect(d.getHours()).toBe(14)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses slash-separated "yyyy/MM/dd"', () => {
|
|
|
+ const d = format.parseDate('2026/08/22')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses ISO 8601 "yyyy-MM-ddTHH:mm:ss"', () => {
|
|
|
+ const d = format.parseDate('2026-08-22T14:00:00')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses plain date "yyyy-MM-dd"', () => {
|
|
|
+ const d = format.parseDate('2026-08-22')
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(ymd(d)).toBe('2026-8-22')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('parses numeric timestamp', () => {
|
|
|
+ const d = format.parseDate(1780000000000)
|
|
|
+ expect(d).not.toBeNull()
|
|
|
+ expect(d.getTime()).toBe(1780000000000)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('returns same Date object when given a Date', () => {
|
|
|
+ const date = new Date()
|
|
|
+ expect(format.parseDate(date)).toBe(date)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('returns null for null/undefined/empty string', () => {
|
|
|
+ expect(format.parseDate(null)).toBeNull()
|
|
|
+ expect(format.parseDate(undefined)).toBeNull()
|
|
|
+ expect(format.parseDate('')).toBeNull()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('returns null for unparseable string', () => {
|
|
|
+ expect(format.parseDate('not-a-date')).toBeNull()
|
|
|
+ expect(format.parseDate('2026-13-45')).toBeNull()
|
|
|
+ })
|
|
|
+ })
|
|
|
})
|