timestamp.js 1.5 KB

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