3 var Type = require('../type');
5 var YAML_TIMESTAMP_REGEXP = new RegExp(
6 '^([0-9][0-9][0-9][0-9])' + // [1] year
7 '-([0-9][0-9]?)' + // [2] month
8 '-([0-9][0-9]?)' + // [3] day
9 '(?:(?:[Tt]|[ \\t]+)' + // ...
10 '([0-9][0-9]?)' + // [4] hour
11 ':([0-9][0-9])' + // [5] minute
12 ':([0-9][0-9])' + // [6] second
13 '(?:\\.([0-9]*))?' + // [7] fraction
14 '(?:[ \\t]*(Z|([-+])([0-9][0-9]?)' + // [8] tz [9] tz_sign [10] tz_hour
15 '(?::([0-9][0-9]))?))?)?$'); // [11] tz_minute
17 function resolveYamlTimestamp(data) {
22 var match, year, month, day, hour, minute, second, fraction = 0,
23 delta = null, tz_hour, tz_minute, date;
25 match = YAML_TIMESTAMP_REGEXP.exec(data);
34 function constructYamlTimestamp(data) {
35 var match, year, month, day, hour, minute, second, fraction = 0,
36 delta = null, tz_hour, tz_minute, date;
38 match = YAML_TIMESTAMP_REGEXP.exec(data);
41 throw new Error('Date resolve error');
44 // match: [1] year [2] month [3] day
47 month = +(match[2]) - 1; // JS month starts with 0
50 if (!match[4]) { // no hour
51 return new Date(Date.UTC(year, month, day));
54 // match: [4] hour [5] minute [6] second [7] fraction
61 fraction = match[7].slice(0, 3);
62 while (fraction.length < 3) { // milli-seconds
68 // match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute
71 tz_hour = +(match[10]);
72 tz_minute = +(match[11] || 0);
73 delta = (tz_hour * 60 + tz_minute) * 60000; // delta in mili-seconds
74 if ('-' === match[9]) {
79 date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));
82 date.setTime(date.getTime() - delta);
88 function representYamlTimestamp(object /*, style*/) {
89 return object.toISOString();
92 module.exports = new Type('tag:yaml.org,2002:timestamp', {
94 resolve: resolveYamlTimestamp,
95 construct: constructYamlTimestamp,
97 represent: representYamlTimestamp