react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / lib / js-yaml / type / timestamp.js
81149 views'use strict';12var Type = require('../type');34var YAML_TIMESTAMP_REGEXP = new RegExp(5'^([0-9][0-9][0-9][0-9])' + // [1] year6'-([0-9][0-9]?)' + // [2] month7'-([0-9][0-9]?)' + // [3] day8'(?:(?:[Tt]|[ \\t]+)' + // ...9'([0-9][0-9]?)' + // [4] hour10':([0-9][0-9])' + // [5] minute11':([0-9][0-9])' + // [6] second12'(?:\\.([0-9]*))?' + // [7] fraction13'(?:[ \\t]*(Z|([-+])([0-9][0-9]?)' + // [8] tz [9] tz_sign [10] tz_hour14'(?::([0-9][0-9]))?))?)?$'); // [11] tz_minute1516function resolveYamlTimestamp(data) {17if (null === data) {18return false;19}2021var match, year, month, day, hour, minute, second, fraction = 0,22delta = null, tz_hour, tz_minute, date;2324match = YAML_TIMESTAMP_REGEXP.exec(data);2526if (null === match) {27return false;28}2930return true;31}3233function constructYamlTimestamp(data) {34var match, year, month, day, hour, minute, second, fraction = 0,35delta = null, tz_hour, tz_minute, date;3637match = YAML_TIMESTAMP_REGEXP.exec(data);3839if (null === match) {40throw new Error('Date resolve error');41}4243// match: [1] year [2] month [3] day4445year = +(match[1]);46month = +(match[2]) - 1; // JS month starts with 047day = +(match[3]);4849if (!match[4]) { // no hour50return new Date(Date.UTC(year, month, day));51}5253// match: [4] hour [5] minute [6] second [7] fraction5455hour = +(match[4]);56minute = +(match[5]);57second = +(match[6]);5859if (match[7]) {60fraction = match[7].slice(0, 3);61while (fraction.length < 3) { // milli-seconds62fraction += '0';63}64fraction = +fraction;65}6667// match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute6869if (match[9]) {70tz_hour = +(match[10]);71tz_minute = +(match[11] || 0);72delta = (tz_hour * 60 + tz_minute) * 60000; // delta in mili-seconds73if ('-' === match[9]) {74delta = -delta;75}76}7778date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));7980if (delta) {81date.setTime(date.getTime() - delta);82}8384return date;85}8687function representYamlTimestamp(object /*, style*/) {88return object.toISOString();89}9091module.exports = new Type('tag:yaml.org,2002:timestamp', {92kind: 'scalar',93resolve: resolveYamlTimestamp,94construct: constructYamlTimestamp,95instanceOf: Date,96represent: representYamlTimestamp97});9899100