123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = isDate;
- var _assertString = require('./util/assertString');
- var _assertString2 = _interopRequireDefault(_assertString);
- var _isISO = require('./isISO8601');
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function getTimezoneOffset(str) {
- var iso8601Parts = str.match(_isISO.iso8601);
- var timezone = void 0,
- sign = void 0,
- hours = void 0,
- minutes = void 0;
- if (!iso8601Parts) {
- str = str.toLowerCase();
- timezone = str.match(/(?:\s|gmt\s*)(-|\+)(\d{1,4})(\s|$)/);
- if (!timezone) {
- return str.indexOf('gmt') !== -1 ? 0 : null;
- }
- sign = timezone[1];
- var offset = timezone[2];
- if (offset.length === 3) {
- offset = '0' + offset;
- }
- if (offset.length <= 2) {
- hours = 0;
- minutes = parseInt(offset, 10);
- } else {
- hours = parseInt(offset.slice(0, 2), 10);
- minutes = parseInt(offset.slice(2, 4), 10);
- }
- } else {
- timezone = iso8601Parts[21];
- if (!timezone) {
- // if no hour/minute was provided, the date is GMT
- return !iso8601Parts[12] ? 0 : null;
- }
- if (timezone === 'z' || timezone === 'Z') {
- return 0;
- }
- sign = iso8601Parts[22];
- if (timezone.indexOf(':') !== -1) {
- hours = parseInt(iso8601Parts[23], 10);
- minutes = parseInt(iso8601Parts[24], 10);
- } else {
- hours = 0;
- minutes = parseInt(iso8601Parts[23], 10);
- }
- }
- return (hours * 60 + minutes) * (sign === '-' ? 1 : -1);
- }
- function isDate(str) {
- (0, _assertString2.default)(str);
- var normalizedDate = new Date(Date.parse(str));
- if (isNaN(normalizedDate)) {
- return false;
- }
- // normalizedDate is in the user's timezone. Apply the input
- // timezone offset to the date so that the year and day match
- // the input
- var timezoneOffset = getTimezoneOffset(str);
- if (timezoneOffset !== null) {
- var timezoneDifference = normalizedDate.getTimezoneOffset() - timezoneOffset;
- normalizedDate = new Date(normalizedDate.getTime() + 60000 * timezoneDifference);
- }
- var day = String(normalizedDate.getDate());
- var dayOrYear = void 0,
- dayOrYearMatches = void 0,
- year = void 0;
- // check for valid double digits that could be late days
- // check for all matches since a string like '12/23' is a valid date
- // ignore everything with nearby colons
- dayOrYearMatches = str.match(/(^|[^:\d])[23]\d([^T:\d]|$)/g);
- if (!dayOrYearMatches) {
- return true;
- }
- dayOrYear = dayOrYearMatches.map(function (digitString) {
- return digitString.match(/\d+/g)[0];
- }).join('/');
- year = String(normalizedDate.getFullYear()).slice(-2);
- if (dayOrYear === day || dayOrYear === year) {
- return true;
- } else if (dayOrYear === '' + day / year || dayOrYear === '' + year / day) {
- return true;
- }
- return false;
- }
- module.exports = exports['default'];
|