-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
298 lines (285 loc) · 15.3 KB
/
main.js
File metadata and controls
298 lines (285 loc) · 15.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//Copyright (c) 2015 Liam Bolling
//https://www.linkedin.com/in/liambolling
function dateParse(dateString){
//Create new date object based on current time.
startTime = new Date();
endTime = null;
currentDaySet = null;
currentMonthSet = null;
currentTimeSet = null;
haveCreatedSecondDateObject = false;
knownDictonary = new Array();
knownDictonary["jan"] = ["month", 0];
knownDictonary["january"] = ["month", 0];
knownDictonary["feb"] = ["month", 1];
knownDictonary["february"] = ["month", 1];
knownDictonary["mar"] = ["month", 2];
knownDictonary["march"] = ["month", 2];
knownDictonary["apr"] = ["month", 3];
knownDictonary["april"] = ["month", 3];
knownDictonary["may"] = ["month", 4];
knownDictonary["june"] = ["month", 5];
knownDictonary["jun"] = ["month", 5];
knownDictonary["july"] = ["month", 6];
knownDictonary["jul"] = ["month", 6];
knownDictonary["aug"] = ["month", 7];
knownDictonary["august"] = ["month", 7];
knownDictonary["sep"] = ["month", 8];
knownDictonary["september"] = ["month", 8];
knownDictonary["oct"] = ["month", 9];
knownDictonary["october"] = ["month", 9];
knownDictonary["nov"] = ["month", 10];
knownDictonary["november"] = ["month", 10];
knownDictonary["dec"] = ["month", 11];
knownDictonary["december"] = ["month", 11];
knownDictonary["noon"] = ["time", 12];
knownDictonary["midnight"] = ["time", 0];
knownDictonary["all"] = ["timeRange", 24];
knownMeridiemDictonary = new Array();
knownMeridiemDictonary["p"] = ["meridiem", 0];
knownMeridiemDictonary["a"] = ["meridiem", 1];
knownMeridiemDictonary["am"] = ["meridiem", 1];
knownMeridiemDictonary["pm"] = ["meridiem", 0];
function assign_to_date(type, value){
if(type == "day" && (currentDaySet == null && haveCreatedSecondDateObject == false)){
startTime.setMonth(startTime.getMonth(), value);
currentDaySet = true;
}else if(type == "day" && (currentDaySet != null && haveCreatedSecondDateObject == false)){
endTime = new Date(startTime.getTime());
endTime.setMonth(endTime.getMonth(), value);
haveCreatedSecondDateObject = true;
}else if(type == "day" && (currentDaySet != null && haveCreatedSecondDateObject == true)){
endTime.setMonth(endTime.getMonth(), value);
}
else if(type == "time" && (currentTimeSet == null && haveCreatedSecondDateObject == false)){
startTime.setHours(value[0]);
startTime.setMinutes(value[1]);
console.log("here_1");
currentTimeSet = true;
}else if(type == "time" && (currentTimeSet != null && haveCreatedSecondDateObject == false)){
endTime = new Date(startTime.getTime());
endTime.setHours(value[0]);
endTime.setMinutes(value[1]);
haveCreatedSecondDateObject = true;
console.log("here_2");
}else if(type == "time" && (currentTimeSet != null && haveCreatedSecondDateObject == true)){
endTime.setHours(value[0]);
endTime.setMinutes(value[1]);
console.log("here_3");
}
else if(type == "month" && (currentMonthSet == null && haveCreatedSecondDateObject == false)){
startTime.setMonth(value);
currentMonthSet = true;
}else if(type == "month" && (currentMonthSet != null && haveCreatedSecondDateObject == false)){
endTime = new Date(startTime.getTime());
endTime.setMonth(value);
haveCreatedSecondDateObject = true;
}else if(type == "month" && (currentMonthSet != null && haveCreatedSecondDateObject == true)){
endTime.setMonth(value);
}
}
//Removes all possible special charecters and replaces it with a space
dateString = dateString.replace(/[\. .\/,-]+/g, " ");
//Split the date by spaces
rawDateString = dateString.split(" ");
dateString = new Array();
for(x = 0; x < rawDateString.length; x++){
if(rawDateString[x] != ""){
dateString.push(rawDateString[x]);
}
}
console.log(dateString);
for(i = 0; i < dateString.length; i++){
if(isNaN(Number(dateString[i]))){
// If the array value is a string
tempLowercase = dateString[i].toLowerCase();
if(knownDictonary[tempLowercase] == null){
// Is the first charecter a number?
if(!isNaN(Number(dateString[i].charAt(0)))){
// Is the first charecter a 0? If so, remove it.
if(Number(dateString[i].charAt(0)) == 0){
// Remove the 0, and keep going.
console.log("Found 0");
dateString[i] = dateString[i].substring(1, dateString[i].length);
}
// Is the second charcter a number?
if(!isNaN(Number(dateString[i].charAt(1)))){
console.log(dateString[i].charAt(1));
// Is the third charecter implying that the day ends with a "th" or "st"?
if(dateString[i].charAt(2) == "n" || dateString[i].charAt(2) == "r" || dateString[i].charAt(2) == "s" || dateString[i].charAt(2) == "t"){
// Safe to assume that the first two charecters are digits, and that they are the day. EXAMPLE: 12
assign_to_date("day", Number(dateString[i].substring(0, 2)));
}
else if(dateString[i].charAt(2) == ":"){
// If last charecter in the string an int?
if(!isNaN(Number(dateString[i].charAt(dateString[i].length - 1)))){
// We now assume that it is a time. If there is an elemnt to the right of the current, and if it is in the known meridiems dictonary, then continue.
if(dateString[i+1] != null && knownMeridiemDictonary[dateString[i+1].toLowerCase()] != null){
// Split the time by a : to get the first hour and second the minute
tempTimeSplit = dateString[i].split(":");
console.log(tempTimeSplit);
if(knownMeridiemDictonary[dateString[i+1].toLowerCase()][1] == 0){
// Assuming that the time ends in a PM, we add 12 to the final hour mark. Assign minutes as usual.
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else{
// Assuming that the time ends in a AM, we don't add anything. Assign minutes as usual.
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}// End of If
// This is now assuming that we don't know the meridiem. Assign everything in an as is method.
}else{
tempTimeSplit = dateString[i].split(":");
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
timeHasBeenSet = true;
}
// We can now assume that the last charecter is a string
}else{
if(dateString[i].charAt(dateString[i].length - 2).toLowerCase() == "p"){
// Remove the last 2 charecters, then split the time by the ":" EXAMPLE: 10:00PM becomes ["10", "00"] (removing the "PM")
tempTimeSplit = dateString[i].substring(0, dateString[i].length-2).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 1).toLowerCase() == "p"){
// Remove the last 1 charecters, then split the time by the ":" EXAMPLE: 10:00P becomes ["10", "00"] (removing the "P")
tempTimeSplit = dateString[i].substring(0, dateString[i].length-1).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 2).toLowerCase() == "a"){
// Remove the last 2 charecters, then split the time by the ":" EXAMPLE: 10:00AM becomes ["10", "00"] (removing the "AM")
tempTimeSplit = dateString[i].substring(0, dateString[i].length-2).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 1).toLowerCase() == "a"){
// Remove the last 1 charecters, then split the time by the ":" EXAMPLE: 10:00A becomes ["10", "00"] (removing the "A")
tempTimeSplit = dateString[i].substring(0, dateString[i].length-1).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}else{
//Should never get here. Means there is a time, that has a string at the end which is not a meridiem.
console.log("Parsed down to a time but it doesn't quite make sense because it ends with an unknown string. Attempting to assume based on what info I was given...");
tempTimeSplit = dateString[i].split(":");
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1].substring(0,2))]);
}
}
}
else if(dateString[i].charAt(dateString[i].length - 2) == "p" || dateString[i].charAt(dateString[i].length - 1) == "p"){
assign_to_date("time", [Number(dateString[i].substring(0, 2)) + 12, 0]);
}else if(dateString[i].charAt(dateString[i].length - 2) == "a" || dateString[i].charAt(dateString[i].length - 1) == "a"){
assign_to_date("time", [Number(dateString[i].substring(0, 2)), 0]);
}else{
assign_to_date("day", Number(dateString[i].substring(0, 2)));
}
}
// Does the second charecter an English numeral?
else if(dateString[i].charAt(1) == "n" || dateString[i].charAt(1) == "r" || dateString[i].charAt(1) == "s" || dateString[i].charAt(1) == "t"){
assign_to_date("day", Number(dateString[i].charAt(0)));
}
// Does the second charecter a ":"?
else if(dateString[i].charAt(1) == ":"){
// If last charecter in the string an int?
console.log("Middel charecter is a :");
if(!isNaN(Number(dateString[i].charAt(dateString[i].length-1)))){
console.log("Last charecter is an int");
// We now assume that it is a time. If there is an elemnt to the right of the current, and if it is in the known meridiems dictonary, then continue.
if(dateString[i+1] != null && knownMeridiemDictonary[dateString[i+1].toLowerCase()] != null){
// Split the time by a : to get the first hour and second the minute
tempTimeSplit = dateString[i].split(":");
console.log(tempTimeSplit);
if(knownMeridiemDictonary[dateString[i+1].toLowerCase()][1] == 0){
// Assuming that the time ends in a PM, we add 12 to the final hour mark. Assign minutes as usual.
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else{
// Assuming that the time ends in a AM, we don't add anything. Assign minutes as usual.
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}// End of If
// This is now assuming that we don't know the meridiem. Assign everything in an as is method.
}else{
tempTimeSplit = dateString[i].split(":");
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}
// We can now assume that the last charecter is a string
}else{
if(dateString[i].charAt(dateString[i].length - 2).toLowerCase() == "p"){
// Remove the last 2 charecters, then split the time by the ":" EXAMPLE: 10:00PM becomes ["10", "00"] (removing the "PM")
tempTimeSplit = dateString[i].substring(0, dateString[i].length - 2).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 1).toLowerCase() == "p"){
// Remove the last 1 charecters, then split the time by the ":" EXAMPLE: 10:00P becomes ["10", "00"] (removing the "P")
tempTimeSplit = dateString[i].substring(0, dateString[i].length - 1).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]) + 12, Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 2).toLowerCase() == "a"){
// Remove the last 2 charecters, then split the time by the ":" EXAMPLE: 10:00AM becomes ["10", "00"] (removing the "AM")
tempTimeSplit = dateString[i].substring(0, dateString[i].length - 2).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}else if(dateString[i].charAt(dateString[i].length - 1).toLowerCase() == "a"){
// Remove the last 1 charecters, then split the time by the ":" EXAMPLE: 10:00A becomes ["10", "00"] (removing the "A")
tempTimeSplit = dateString[i].substring(0, dateString[i].length - 1).split(":");
console.log(tempTimeSplit);
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1])]);
}else{
//Should never get here. Means there is a time, that has a string at the end which is not a meridiem.
console.log("Parsed down to a time but it doesn't quite make sense because it ends with an unknown string.");
tempTimeSplit = dateString[i].split(":");
assign_to_date("time", [Number(tempTimeSplit[0]), Number(tempTimeSplit[1].substring(0,2))]);
}
}
}
else if(dateString[i].charAt(dateString[i].length - 2) == "p" || dateString[i].charAt(dateString[i].length - 1) == "p"){
assign_to_date("time", [Number(dateString[i].substring(0, 1)) + 12, 0]);
}else if(dateString[i].charAt(dateString[i].length - 2) == "a" || dateString[i].charAt(dateString[i].length - 1) == "a"){
assign_to_date("time", [Number(dateString[i].substring(0, 1)), 0]);
}else{
assign_to_date("day", Number(dateString[i].substring(0, 1)));
}
}
// Was found in the known dictonary. Check against if it is a month
}else if(knownDictonary[tempLowercase][0] == "month"){
assign_to_date("month", knownDictonary[tempLowercase][1]);
//resultTime.setMonth(knownDictonary[tempLowercase][1]);
}else if(knownDictonary[tempLowercase][0] == "time"){
if(knownDictonary[tempLowercase][1] == "0"){
//resultTime.setMonth(resultTime.getMonth(), resultTime.getDay());
assign_to_date("time", [23, 59]);
}
if(knownDictonary[tempLowercase][1] == "12"){
//resultTime.setMonth(resultTime.getMonth(), resultTime.getDay());
assign_to_date("time", [12, 0]);
}
}else if(knownDictonary[tempLowercase][0] == "timeRange"){
if(knownDictonary[tempLowercase][1] == "24"){
assign_to_date("time", [0, 0]);
assign_to_date("time", [23, 59]);
}
}
}else{
if((dateString[i+1] != null && dateString[i+1].toLowerCase() == "am") || (dateString[i+1] != null && dateString[i+1].toLowerCase() == "a")){
assign_to_date("time", [Number(dateString[i]), 0]);
}else if((dateString[i+1] != null && dateString[i+1].toLowerCase() == "pm") || (dateString[i+1] != null && dateString[i+1].toLowerCase() == "p")){
assign_to_date("time", [Number(dateString[i]) + 12, 0]);
}else if(dateString[i].length == 4){
// We can assume it is a year because of the 4 digits
resultTime.setFullYear(parseInt(dateString[i]));
}else{
if(dateString[i+1] != null && dateString[i+1].length == 4){
assign_to_date("day", Number(dateString[i]));
}else{
if(dateString[i+2] != null && dateString[i+2].length == 4){
assign_to_date("month", Number(dateString[i]) - 1);
}else{
if(dateString[i+1] != null && (dateString[i+1].length != 4 && !isNaN(Number(dateString[i+1])))){
assign_to_date("month", Number(dateString[i]) - 1);
}else{
assign_to_date("day", Number(dateString[i]));
}
}
}
}
}
}
if(endTime == null){
endTime = new Date(startTime.getTime());
}
return new Array(startTime, endTime);
}