timestamp.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const DT_SEPARATOR = /t|\s/i
  2. const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
  3. const TIME = /^(\d\d):(\d\d):(\d\d)(?:\.\d+)?(?:z|([+-]\d\d)(?::?(\d\d))?)$/i
  4. const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  5. export default function validTimestamp(str: string, allowDate: boolean): boolean {
  6. // http://tools.ietf.org/html/rfc3339#section-5.6
  7. const dt: string[] = str.split(DT_SEPARATOR)
  8. return (
  9. (dt.length === 2 && validDate(dt[0]) && validTime(dt[1])) ||
  10. (allowDate && dt.length === 1 && validDate(dt[0]))
  11. )
  12. }
  13. function validDate(str: string): boolean {
  14. const matches: string[] | null = DATE.exec(str)
  15. if (!matches) return false
  16. const y: number = +matches[1]
  17. const m: number = +matches[2]
  18. const d: number = +matches[3]
  19. return (
  20. m >= 1 &&
  21. m <= 12 &&
  22. d >= 1 &&
  23. (d <= DAYS[m] ||
  24. // leap year: https://tools.ietf.org/html/rfc3339#appendix-C
  25. (m === 2 && d === 29 && (y % 100 === 0 ? y % 400 === 0 : y % 4 === 0)))
  26. )
  27. }
  28. function validTime(str: string): boolean {
  29. const matches: string[] | null = TIME.exec(str)
  30. if (!matches) return false
  31. const hr: number = +matches[1]
  32. const min: number = +matches[2]
  33. const sec: number = +matches[3]
  34. const tzH: number = +(matches[4] || 0)
  35. const tzM: number = +(matches[5] || 0)
  36. return (
  37. (hr <= 23 && min <= 59 && sec <= 59) ||
  38. // leap second
  39. (hr - tzH === 23 && min - tzM === 59 && sec === 60)
  40. )
  41. }
  42. validTimestamp.code = 'require("ajv/dist/runtime/timestamp").default'