commit
message
parsing '(Dayname) Month DD YYYY' values for DATEVALUE
author
Ben Vogt <[email protected]>
date
2017-03-18 22:29:27
stats
2 file(s) changed,
46 insertions(+),
0 deletions(-)
files
src/RawFormulas/Date.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index 9631be1..c4b666f 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -274,6 +274,30 @@ var DATEVALUE = function (...values) : number {
6 }
7 }
8
9+ // Check (Dayname) Month DD YYYY
10+ if (m === undefined) {
11+ // For reference: https://regex101.com/r/xPcm7v/11
12+ var matches = dateString.match(/^\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?,?\s*(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec),?\s*(0?[0-9]|1[0-9]|2[0-9]|3[0-1]),?\s*([0-9]{4}|[1-9][0-9]{2}|[0-9]{2})\s*$/i);
13+ if (matches && matches.length === 5) {
14+ var years = parseInt(matches[4]);
15+ var monthName = matches[2];
16+ var days = parseInt(matches[3]) - 1; // Days are zero indexed.
17+ var actualYear = years;
18+ if (years >= 0 && years < 30) {
19+ actualYear = Y2K_YEAR + years;
20+ } else if (years >= 30 && years < 100) {
21+ actualYear = FIRST_YEAR + years;
22+ }
23+ var tmpMoment = moment.utc([actualYear]).month(monthName);
24+ // If we're specifying more days than there are in this month
25+ if (days > tmpMoment.daysInMonth() - 1) {
26+ throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
27+ }
28+ m = tmpMoment.add({"days": days});
29+ }
30+
31+ }
32+
33 // If we've not been able to parse the date by now, then we cannot parse it at all.
34 if (m === undefined || !m.isValid()) {
35 throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
36diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
37index 967b77b..584348d 100644
38--- a/tests/FormulasTest.ts
39+++ b/tests/FormulasTest.ts
40@@ -1046,7 +1046,27 @@ assertEquals(DATEVALUE("1992/1/13 12:666669:00am"), 34078); // overload minutes
41 assertEquals(DATEVALUE("1992/1/13 12:666669:00pm"), 34079); // overload minutes
42 assertEquals(DATEVALUE("1992/1/13 12:100000000:00am"), 103060); // overload minutes
43 assertEquals(DATEVALUE("1992/1/13 12:912347287:00am"), 667190); // overload minutes
44-
45+// (Dayname) Month DD YYYY
46+assertEquals(DATEVALUE("Sun Feb 09 2017"), 42775);
47+assertEquals(DATEVALUE("Sun Feb 9 2017"), 42775);
48+assertEquals(DATEVALUE("Mon Feb 09 2017"), 42775);
49+assertEquals(DATEVALUE("Thursday Feb 09 2017"), 42775);
50+assertEquals(DATEVALUE("Thursday February 09 2017"), 42775);
51+assertEquals(DATEVALUE("Sun September 01 20"), 44075);
52+assertEquals(DATEVALUE("Sun, Feb, 09, 2017"), 42775);
53+assertEquals(DATEVALUE("May 20 1992"), 33744);
54+assertEquals(DATEVALUE("December 31 100"), -657070);
55+assertEquals(DATEVALUE("January 13 0030"), 10971);
56+assertEquals(DATEVALUE("January 13 1200"), -255656);
57+assertEquals(DATEVALUE("January 22 2222"), 117631);
58+assertEquals(DATEVALUE("November 3 4243"), 856071);
59+assertEquals(DATEVALUE("Feb 29 2004"), 38046); // leap year, 29th ok
60+catchAndAssertEquals(function() {
61+ DATEVALUE("Feb 29 2001");// not leap year, 29th not ok
62+}, ERRORS.VALUE_ERROR);
63+catchAndAssertEquals(function() {
64+ DATEVALUE("June 32 2001");// overload numbers not ok
65+}, ERRORS.VALUE_ERROR);
66
67
68 // Test DB