parse-messy-time
Advanced tools
Comparing version 1.0.0 to 1.1.0
162
index.js
@@ -9,6 +9,9 @@ var months = [ | ||
var hmsre = RegExp( | ||
'(\\d+\\.?\\d*(?:[:h]\\d+\\.?\\d*(?:[:m]\\d+\\.\\d*s?)?)?)' | ||
); | ||
var tokre = RegExp( | ||
'\\s+|(\\d+(?:th|nd|rd|th))' | ||
+ '|(\\d+(?:[:h]\\d+(?:[:m]\\d+s?)?)?)([A-Za-z]+)' | ||
+ '|([A-Za-z]+)(\\d+(?:[:h]\\d+(?:[:m]\\d+s?)?)?)' | ||
+ '|' + hmsre.source + '([A-Za-z]+)' | ||
+ '|([A-Za-z]+)' + hmsre.source | ||
); | ||
@@ -37,9 +40,41 @@ | ||
} | ||
else if ((m = hmsre.exec(t)) && isunit(next)) { | ||
if (tokens[i-1] === 'in') { | ||
for (var j = i; j < tokens.length; j += 2) { | ||
if (tokens[j] === 'and') j --; | ||
else if ((m = hmsre.exec(tokens[j])) | ||
&& ishunit(tokens[j+1])) { | ||
addu(parseh(tokens[j]), nunit(tokens[j+1])); | ||
} | ||
else if ((m = /^(\d+\.?\d*)/.exec(tokens[j])) | ||
&& isdunit(tokens[j+1])) { | ||
daddu(Number(m[1]), nunit(tokens[j+1])); | ||
} | ||
else break; | ||
} | ||
i = j; | ||
} | ||
else { | ||
for (var j = i + 2; j < tokens.length; j++) { | ||
if (tokens[j] === 'ago') break; | ||
} | ||
if (j === tokens.length) continue; | ||
for (var k = i; k < j; k++) { | ||
if ((m = hmsre.exec(tokens[k])) && ishunit(tokens[k+1])) { | ||
subu(parseh(tokens[k]), nunit(tokens[k+1])); | ||
} | ||
else if ((m = /^(\d+\.?\d*)/.exec(tokens[k])) | ||
&& isdunit(tokens[k+1])) { | ||
dsubu(Number(m[1]), nunit(tokens[k+1])); | ||
} | ||
} | ||
i = j; | ||
} | ||
} | ||
else if (/\d+[:h]\d+/.test(t) || /^(am|pm)/.test(next)) { | ||
m = /(\d+)(?:[:h](\d+)(?:[:m](\d+s?\.?\d*))?)?/.exec(t); | ||
res.hours = Number(m[1]); | ||
if (/^pm/.test(next) && res.hours < 12) res.hours += 12; | ||
if (m[2]) res.minutes = Number(m[2]); | ||
if (m[3]) res.seconds = Number(m[3]); | ||
// time | ||
var hms = parseh(t, next); | ||
if (hms[0] !== null) res.hours = hms[0]; | ||
if (hms[1] !== null) res.minutes = hms[1]; | ||
if (hms[2] !== null) res.seconds = hms[2]; | ||
} | ||
@@ -96,2 +131,12 @@ else if ((m = /^(\d+)/.exec(t)) && monthish(next)) { | ||
} | ||
else if (/^yesterday/.test(t) && res.date === undefined) { | ||
var yst = new Date(now.valueOf() - 24*60*60*1000); | ||
res.date = yst.getDate(); | ||
if (res.month === undefined) { | ||
res.month = months[yst.getMonth()]; | ||
} | ||
if (res.year === undefined) { | ||
res.year = yst.getFullYear(); | ||
} | ||
} | ||
else if (t === 'next' && dayish(next) && res.date === undefined) { | ||
@@ -118,11 +163,17 @@ setFromDay(next, 7); | ||
} | ||
if (res.month) res.month = nmonth(res.month); | ||
if (res.month && typeof res.month !== 'number') { | ||
res.month = nmonth(res.month); | ||
} | ||
var out = new Date; | ||
var out = new Date(now); | ||
out.setHours(res.hours === undefined ? 0 : res.hours); | ||
out.setMinutes(res.minutes === undefined ? 0 : res.minutes); | ||
out.setSeconds(res.seconds === undefined ? 0 : res.seconds); | ||
if (res.date) out.setDate(res.date); | ||
if (res.date) out.setDate(res.date); | ||
if (res.month) out.setMonth(months.indexOf(res.month)); | ||
if (typeof res.month === 'number') { | ||
out.setMonth(res.month); | ||
} | ||
else if (res.month) out.setMonth(months.indexOf(res.month)); | ||
if (res.year) out.setYear(res.year); | ||
@@ -143,10 +194,72 @@ return out; | ||
} | ||
function opu (hms, u, op) { | ||
if (u == 'hours') { | ||
res.hours = op(now.getHours(), hms[0]); | ||
res.minutes = op(now.getMinutes(), hms[1] === null ? 0 : hms[1]); | ||
res.seconds = op(now.getSeconds(), hms[2] === null ? 0 : hms[2]); | ||
} | ||
else if (u == 'minutes') { | ||
if (res.hours === undefined) res.hours = now.getHours(); | ||
res.minutes = op(now.getMinutes(), hms[0] === null ? 0 : hms[0]); | ||
res.seconds = op(now.getSeconds(), hms[1] === null ? 0 : hms[1]); | ||
} | ||
else if (u == 'seconds') { | ||
if (res.hours === undefined) res.hours = now.getHours(); | ||
if (res.minutes === undefined) res.minutes = now.getMinutes(); | ||
res.seconds = op(now.getSeconds(), hms[0] === null ? 0 : hms[0]); | ||
} | ||
} | ||
function subu (hms, u) { opu(hms, u, sub) } | ||
function addu (hms, u) { opu(hms, u, add) } | ||
function dopu (n, u, op) { | ||
if (res.hours === undefined) res.hours = now.getHours(); | ||
if (res.minutes === undefined) res.minutes = now.getMinutes(); | ||
if (res.seconds === undefined) res.seconds = now.getSeconds(); | ||
if (u === 'days') { | ||
res.date = op(now.getDate(), n); | ||
} | ||
else if (u === 'weeks') { | ||
res.date = op(now.getDate(), n*7); | ||
} | ||
else if (u === 'months') { | ||
res.month = op(now.getMonth(), n); | ||
} | ||
else if (u === 'years') { | ||
res.year = op(now.getFullYear(), n); | ||
} | ||
} | ||
function dsubu (n, u) { dopu(n, u, sub) } | ||
function daddu (n, u) { dopu(n, u, add) } | ||
}; | ||
function add (a, b) { return a + b } | ||
function sub (a, b) { return a - b } | ||
function lc (s) { return String(s).toLowerCase() } | ||
function monthish (s) { | ||
return /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i.test(s); | ||
function ishunit (s) { | ||
var n = nunit(s); | ||
return n === 'hours' || n === 'minutes' || n === 'seconds'; | ||
} | ||
function isdunit (s) { | ||
var n = nunit(s); | ||
return n === 'days' || n === 'weeks' || n === 'months' || n === 'years'; | ||
} | ||
function isunit (s) { return Boolean(nunit(s)) } | ||
function nunit (s) { | ||
if (/^(ms|millisecs?|milliseconds?)$/.test(s)) return 'milliseconds'; | ||
if (/^(s|secs?|seconds?)$/.test(s)) return 'seconds'; | ||
if (/^(m|mins?|minutes?)$/.test(s)) return 'minutes'; | ||
if (/^(h|hrs?|hours?)$/.test(s)) return 'hours'; | ||
if (/^(d|days?)$/.test(s)) return 'days'; | ||
if (/^(w|wks?|weeks?)$/.test(s)) return 'weeks'; | ||
if (/^(mo|mnths?|months?)$/.test(s)) return 'months'; | ||
if (/^(y|yrs?|years?)$/.test(s)) return 'years'; | ||
} | ||
function monthish (s) { return Boolean(nmonth(s)) } | ||
function dayish (s) { | ||
@@ -180,1 +293,22 @@ return /^(mon|tue|wed|thu|fri|sat|sun)/i.test(s); | ||
} | ||
function parseh (s, next) { | ||
var m = /(\d+\.?\d*)(?:[:h](\d+\.?\d*)(?:[:m](\d+\.?\d*s?\.?\d*))?)?/.exec(s); | ||
var hms = [ Number(m[1]), null, null ]; | ||
if (/^pm/.test(next) && hms[0] < 12) hms[0] += 12; | ||
if (m[2]) hms[1] = Number(m[2]); | ||
if (m[3]) hms[2] = Number(m[3]); | ||
if (hms[0] > floorup(hms[0])) { | ||
hms[1] = floorup((hms[0] - floorup(hms[0])) * 60); | ||
hms[0] = floorup(hms[0]); | ||
} | ||
if (hms[1] > floorup(hms[1])) { | ||
hms[2] = floorup((hms[1] - floorup(hms[1])) * 60); | ||
hms[1] = floorup(hms[1]); | ||
} | ||
return hms; | ||
} | ||
function floorup (x) { | ||
return Math.floor(Math.round(x * 1e6) / 1e6); | ||
} |
{ | ||
"name": "parse-messy-time", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "parse messy human date and time strings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,2 +8,4 @@ var parse = require('../'); | ||
var tomorrow = new Date((new Date).valueOf() + 24*60*60*1000); | ||
// Tue Apr 14 2015 09:46:01 GMT-0700 (PDT) | ||
var optsd = { now: new Date(1429029961000) }; | ||
@@ -94,3 +96,78 @@ | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 12 minutes', optsd)), | ||
'2015-04-14 09:58:01', | ||
'in 12 minutes' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 2 hours', optsd)), | ||
'2015-04-14 11:46:01', | ||
'in 2 hours' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 31 hours', optsd)), | ||
'2015-04-15 16:46:01', | ||
'in 31 hours' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 20 hours 40 minutes', optsd)), | ||
'2015-04-15 06:26:01', | ||
'in 20 hours 40 minutes' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 20 hours and 40 minutes', optsd)), | ||
'2015-04-15 06:26:01', | ||
'in 20 hours and 40 minutes' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 20.2h', optsd)), | ||
'2015-04-15 05:58:01', | ||
'in 20.2h' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 5 weeks', optsd)), | ||
'2015-05-19 09:46:01', | ||
'in 5 weeks' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 2 years', optsd)), | ||
'2017-04-14 09:46:01', | ||
'in 2 years' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 2 years and 5 weeks', optsd)), | ||
'2017-05-19 09:46:01', | ||
'in 2 years and 5 weeks' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('in 1.5 weeks', optsd)), | ||
'2015-04-24 09:46:01', | ||
'in 1.5 weeks' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('2 days ago', optsd)), | ||
'2015-04-12 09:46:01', | ||
'2 days ago' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('2 days and 6 hours ago', optsd)), | ||
'2015-04-12 03:46:01', | ||
'2 days and 6 hours ago' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('1 month ago', optsd)), | ||
'2015-03-14 09:46:01', | ||
'1 month ago' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('yesterday', optsd)), | ||
'2015-04-13 00:00:00', | ||
'yesterday' | ||
); | ||
t.deepEqual( | ||
strftime('%F %T', parse('yesterday at 8am', optsd)), | ||
'2015-04-13 08:00:00', | ||
'yesterday' | ||
); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18860
458
65