spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
parsing '(Dayname) DD Month YYYY' values for DATEVALUE
author
Ben Vogt <[email protected]>
date
2017-03-20 00:34:02
stats
2 file(s) changed, 47 insertions(+), 2 deletions(-)
files
src/RawFormulas/Date.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index c4b666f..9d09a7c 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -54,8 +54,8 @@ var DATE = function (...values) {
 6  * "1999/1/13 10:10am"      DONE
 7  * "1999/1/13 10:10:10"     DONE
 8  * "1999/1/13 10:10:10pm"   DONE
 9- * "Sun Feb 09 2017"
10- * "09 Feb 2017"
11+ * "Sun Feb 09 2017"        DONE
12+ * "Sun 09 Feb 2017"        DONE
13  * "Feb-2017"
14  * "22-Feb"
15  * "10-22"
16@@ -295,6 +295,29 @@ var DATEVALUE = function (...values) : number {
17       }
18       m = tmpMoment.add({"days": days});
19     }
20+  }
21+
22+  // Check (Dayname) DD Month YYYY
23+  if (m === undefined) {
24+    // For reference: https://regex101.com/r/22TD0r/3
25+    var matches = dateString.match(/^\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?,?\s*(0?[0-9]|1[0-9]|2[0-9]|3[0-1]),?\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-9]{4}|[1-9][0-9]{2}|[0-9]{2})\s*$/i);
26+    if (matches && matches.length === 5) {
27+      var years = parseInt(matches[4]);
28+      var monthName = matches[3];
29+      var days = parseInt(matches[2]) - 1; // Days are zero indexed.
30+      var actualYear = years;
31+      if (years >= 0 && years < 30) {
32+        actualYear = Y2K_YEAR + years;
33+      } else if (years >= 30 && years < 100) {
34+        actualYear = FIRST_YEAR + years;
35+      }
36+      var tmpMoment = moment.utc([actualYear]).month(monthName);
37+      // If we're specifying more days than there are in this month
38+      if (days > tmpMoment.daysInMonth() - 1) {
39+        throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
40+      }
41+      m = tmpMoment.add({"days": days});
42+    }
43 
44   }
45 
46diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
47index 584348d..818defc 100644
48--- a/tests/FormulasTest.ts
49+++ b/tests/FormulasTest.ts
50@@ -1067,6 +1067,27 @@ catchAndAssertEquals(function() {
51 catchAndAssertEquals(function() {
52   DATEVALUE("June 32 2001");// overload numbers not ok
53 }, ERRORS.VALUE_ERROR);
54+// (Dayname) DD Month YYYY
55+assertEquals(DATEVALUE("Sun 09 Feb 2017"), 42775);
56+assertEquals(DATEVALUE("Sun 9 Feb 2017"), 42775);
57+assertEquals(DATEVALUE("Mon 09 Feb 2017"), 42775);
58+assertEquals(DATEVALUE("Thursday 09 Feb 2017"), 42775);
59+assertEquals(DATEVALUE("Thursday 09 February 2017"), 42775);
60+assertEquals(DATEVALUE("Sun 01 September 20"), 44075);
61+assertEquals(DATEVALUE("Sun, 09, Feb, 2017"), 42775);
62+assertEquals(DATEVALUE("20 May 1992"), 33744);
63+assertEquals(DATEVALUE("31 December 100"), -657070);
64+assertEquals(DATEVALUE("13 January 0030"), 10971);
65+assertEquals(DATEVALUE("13 January 1200"), -255656);
66+assertEquals(DATEVALUE("22 January 2222"), 117631);
67+assertEquals(DATEVALUE("3 November 4243"), 856071);
68+assertEquals(DATEVALUE("29 Feb 2004"), 38046); // leap year, 29th ok
69+catchAndAssertEquals(function() {
70+  DATEVALUE("29 Feb 2001");// not leap year, 29th not ok
71+}, ERRORS.VALUE_ERROR);
72+catchAndAssertEquals(function() {
73+  DATEVALUE("32 June 2001");// overload numbers not ok
74+}, ERRORS.VALUE_ERROR);
75 
76 
77 // Test DB