1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
ISO 8601
http://www.w3.org/TR/NOTE-datetime
- YYYY-MM-DD
- YYYY-MM-DDThh:mmTZD
- YYYY-MM-DDThh:mm:ssTZD
- YYYY-MM-DDThh:mm:ss.sTZD
- TZD = (Z or +hh:mm or -hh:mm)
*/
var moment = require('moment');
var Parser = require('../parser').Parser;
var ParsedResult = require('../../result').ParsedResult;
var PATTERN = new RegExp('(\\W|^)'
+ '([0-9]{4})\\-([0-9]{1,2})\\-([0-9]{1,2})'
+ '(?:T' //..
+ '([0-9]{1,2}):([0-9]{1,2})' // hh:mm
+ '(?::([0-9]{1,2})(?:\\.(\\d{1,4}))?)?' // :ss.s
+ '(?:Z|([+-]\\d{2}):?(\\d{2})?)?' // TZD (Z or ±hh:mm or ±hhmm or ±hh)
+ ')?' //..
+ '(?=\\W|$)', 'i');
var YEAR_NUMBER_GROUP = 2;
var MONTH_NUMBER_GROUP = 3;
var DATE_NUMBER_GROUP = 4;
var HOUR_NUMBER_GROUP = 5;
var MINUTE_NUMBER_GROUP = 6;
var SECOND_NUMBER_GROUP = 7;
var MILLISECOND_NUMBER_GROUP = 8;
var TZD_HOUR_OFFSET_GROUP = 9;
var TZD_MINUTE_OFFSET_GROUP = 10;
exports.Parser = function ENISOFormatParser(){
Parser.apply(this, arguments);
this.pattern = function() { return PATTERN; }
this.extract = function(text, ref, match, opt){
var text = match[0].substr(match[1].length);
var index = match.index + match[1].length;
var result = new ParsedResult({
text: text,
index: index,
ref: ref,
})
result.start.assign('year', parseInt(match[YEAR_NUMBER_GROUP]));
result.start.assign('month', parseInt(match[MONTH_NUMBER_GROUP]));
result.start.assign('day', parseInt(match[DATE_NUMBER_GROUP]));
if (moment(result.start.get('month')) > 12 || moment(result.start.get('month')) < 1 ||
moment(result.start.get('day')) > 31 || moment(result.start.get('day')) < 1) {
return null;
}
if (match[HOUR_NUMBER_GROUP] != null) {
result.start.assign('hour',
parseInt(match[HOUR_NUMBER_GROUP]));
result.start.assign('minute',
parseInt(match[MINUTE_NUMBER_GROUP]));
if (match[SECOND_NUMBER_GROUP] != null) {
result.start.assign('second',
parseInt(match[SECOND_NUMBER_GROUP]));
}
if (match[MILLISECOND_NUMBER_GROUP] != null) {
result.start.assign('millisecond',
parseInt(match[MILLISECOND_NUMBER_GROUP]));
}
if (match[TZD_HOUR_OFFSET_GROUP] == null) {
result.start.assign('timezoneOffset', 0);
} else {
var minuteOffset = 0;
var hourOffset = parseInt(match[TZD_HOUR_OFFSET_GROUP]);
if (match[TZD_MINUTE_OFFSET_GROUP] != null)
minuteOffset = parseInt(match[TZD_MINUTE_OFFSET_GROUP]);
var offset = hourOffset * 60;
if (offset < 0) {
offset -= minuteOffset;
} else {
offset += minuteOffset;
}
result.start.assign('timezoneOffset', offset);
}
}
result.tags['ENISOFormatParser'] = true;
return result;
};
}
|