timestamp.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const codegen_1 = require("./codegen");
  4. const DATE_TIME = /^(\d\d\d\d)-(\d\d)-(\d\d)(?:t|\s)(\d\d):(\d\d):(\d\d)(?:\.\d+)?(?:z|([+-]\d\d)(?::?(\d\d))?)$/i;
  5. const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  6. function validTimestamp(str) {
  7. // http://tools.ietf.org/html/rfc3339#section-5.6
  8. const matches = DATE_TIME.exec(str);
  9. if (!matches)
  10. return false;
  11. const y = +matches[1];
  12. const m = +matches[2];
  13. const d = +matches[3];
  14. const hr = +matches[4];
  15. const min = +matches[5];
  16. const sec = +matches[6];
  17. const tzH = +(matches[7] || 0);
  18. const tzM = +(matches[8] || 0);
  19. return (m >= 1 &&
  20. m <= 12 &&
  21. d >= 1 &&
  22. (d <= DAYS[m] ||
  23. // leap year: https://tools.ietf.org/html/rfc3339#appendix-C
  24. (m === 2 && d === 29 && (y % 100 === 0 ? y % 400 === 0 : y % 4 === 0))) &&
  25. ((hr <= 23 && min <= 59 && sec <= 59) ||
  26. // leap second
  27. (hr - tzH === 23 && min - tzM === 59 && sec === 60)));
  28. }
  29. exports.default = validTimestamp;
  30. validTimestamp.code = codegen_1._ `require("ajv/dist/compile/timestamp").default`;
  31. //# sourceMappingURL=timestamp.js.map