macos-web/tests/cases/10-datetime.test.ts
2026-07-20 14:30:44 +08:00

46 lines
1.3 KiB
TypeScript

/**
* 10-datetime: 日期格式化、时区
*/
import { describe, it, expect } from 'vitest'
import { localDateKey, fmtDateCN, fmtTime, tzValid } from '../../src/utils'
describe('10-datetime — 日期时间工具', () => {
it('localDateKey 返回 YYYY-MM-DD 格式', () => {
const d = new Date(2024, 0, 15) // Jan 15, 2024
expect(localDateKey(d)).toBe('2024-01-15')
})
it('localDateKey() 无参数返回今天', () => {
const key = localDateKey()
expect(key).toMatch(/^\d{4}-\d{2}-\d{2}$/)
})
it('fmtDateCN 返回中文日期', () => {
const d = new Date(2024, 6, 17) // Jul 17, 2024 (Wednesday)
const result = fmtDateCN(d)
expect(result).toContain('7月')
expect(result).toContain('17日')
})
it('fmtTime 24小时制', () => {
const d = new Date(2024, 0, 1, 14, 30)
const result = fmtTime(d, true)
expect(result).toBe('14:30')
})
it('fmtTime 12小时制', () => {
const d = new Date(2024, 0, 1, 14, 30)
const result = fmtTime(d, false)
expect(result).toContain('下午')
expect(result).toContain('2:30')
})
it('tzValid 验证时区', () => {
expect(tzValid('Asia/Shanghai')).toBe(true)
expect(tzValid('America/New_York')).toBe(true)
expect(tzValid('')).toBe(false)
expect(tzValid('local')).toBe(false)
expect(tzValid('Invalid/Zone')).toBe(false)
})
})