spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Using DateRegExBuilder to capture DAY_MONTHNAME_YEAR, DD(fd)Month(fd)YYYY, '24/July/1992'
author
Ben Vogt <[email protected]>
date
2017-04-01 01:51:08
stats
2 file(s) changed, 77 insertions(+), 16 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
  1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
  2index a9ae66b..5e36de9 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -53,8 +53,11 @@ const MONTHDIG_DAY_YEAR = DateRegExBuilder.DateRegExBuilder()
  6   .OPTIONAL_DAYNAME().OPTIONAL_COMMA().MM().FLEX_DELIMITER().DD().FLEX_DELIMITER().YYYY14()
  7   .end()
  8   .build();
  9-// const MONTHNAME_DAY_YEAR;
 10-// const DAY_MONTHNAME_YEAR;
 11+const DAY_MONTHNAME_YEAR = DateRegExBuilder.DateRegExBuilder()
 12+  .start()
 13+  .OPTIONAL_DAYNAME().OPTIONAL_COMMA().DD().FLEX_DELIMITER().MONTHNAME().FLEX_DELIMITER().YYYY14()
 14+  .end()
 15+  .build();
 16 // const YEAR_MONTHDIG;
 17 // const MONTHDIG_YEAR;
 18 // const YEAR_MONTHNAME;
 19@@ -82,8 +85,12 @@ var DATEVALUE = function (...values) : number {
 20     } else if (years >= 30 && years < 100) {
 21       actualYear = FIRST_YEAR + years;
 22     }
 23-    var tmpMoment = moment.utc([actualYear])
 24-      .add(months, 'months');
 25+    var tmpMoment = moment.utc([actualYear]).startOf("year");
 26+    if (typeof months === "string") {
 27+      tmpMoment.month(months);
 28+    } else {
 29+      tmpMoment.set("months", months);
 30+    }
 31     // If we're specifying more days than there are in this month
 32     if (days > tmpMoment.daysInMonth() - 1) {
 33       throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 34@@ -121,6 +128,23 @@ var DATEVALUE = function (...values) : number {
 35     }
 36   }
 37 
 38+  // Check DAY_MONTHNAME_YEAR, DD(fd)Month(fd)YYYY, '24/July/1992'
 39+  if (m === undefined) {
 40+    var matches = dateString.match(DAY_MONTHNAME_YEAR);
 41+    if (matches && matches.length === 8) {
 42+      var years = parseInt(matches[7]);
 43+      var monthName = matches[5];
 44+      var days = parseInt(matches[3]) - 1; // Days are zero indexed.
 45+      var firstDelimiter = matches[4].replace(/\s*/g, '');
 46+      var secondDelimiter = matches[6].replace(/\s*/g, '');
 47+      // Check delimiters. If they're not the same, and the first one isn't a space, throw error.
 48+      if (firstDelimiter !== secondDelimiter && firstDelimiter !== "") {
 49+        throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 50+      }
 51+      m = createMoment(years, monthName, days);
 52+    }
 53+  }
 54+
 55   // If we've not been able to parse the date by now, then we cannot parse it at all.
 56   if (m === undefined || !m.isValid()) {
 57     throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 58diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 59index e45db40..44d2964 100644
 60--- a/tests/DateFormulasTest.ts
 61+++ b/tests/DateFormulasTest.ts
 62@@ -154,14 +154,54 @@ assertEquals(DATEVALUE("1992, 6, 24"), 33779);
 63 catchAndAssertEquals(function() {
 64   DATEVALUE("Sunday,1992/6/24");// flex delimiter should not allow a comma without a space after it.
 65 }, ERRORS.VALUE_ERROR);
 66-
 67-
 68 catchAndAssertEquals(function() {
 69   DATEVALUE("2005/2/29");// Leap day on non-leap year.
 70 }, ERRORS.VALUE_ERROR);
 71 catchAndAssertEquals(function() {
 72   DATEVALUE("2005/1/44");// Out of range day for any month
 73 }, ERRORS.VALUE_ERROR);
 74+// DD/Month/YYYY
 75+assertEquals(DATEVALUE("Sun 09 Feb 2017"), 42775);
 76+assertEquals(DATEVALUE("Sun 9 Feb 2017"), 42775);
 77+assertEquals(DATEVALUE("Mon 09 Feb 2017"), 42775);
 78+assertEquals(DATEVALUE("Thursday 09 Feb 2017"), 42775);
 79+assertEquals(DATEVALUE("Thursday 09 February 2017"), 42775);
 80+assertEquals(DATEVALUE("Sun 01 September 20"), 44075);
 81+assertEquals(DATEVALUE("Sun, 09, Feb, 2017"), 42775);
 82+assertEquals(DATEVALUE("20 May 1992"), 33744);
 83+assertEquals(DATEVALUE("31 December 100"), -657070);
 84+assertEquals(DATEVALUE("13 January 0030"), 10971);
 85+assertEquals(DATEVALUE("13 January 1200"), -255656);
 86+assertEquals(DATEVALUE("22 January 2222"), 117631);
 87+assertEquals(DATEVALUE("3 November 4243"), 856071);
 88+assertEquals(DATEVALUE("13 November 0999"), -328765);
 89+assertEquals(DATEVALUE("13 November 1200"), -255351);
 90+assertEquals(DATEVALUE("13 January 0029"), 47131);
 91+assertEquals(DATEVALUE("13 January 0030"), 10971);
 92+assertEquals(DATEVALUE("13 January 0044"), 16084);
 93+assertEquals(DATEVALUE("13 January 0050"), 18276);
 94+assertEquals(DATEVALUE("13 January 0097"), 35443);
 95+assertEquals(DATEVALUE("13 January 0099"), 36173);
 96+assertEquals(DATEVALUE("13 January 0000"), 36538);
 97+assertEquals(DATEVALUE("13 January 0101"), -657057);
 98+assertEquals(DATEVALUE("13 January 0100"), -657422);
 99+assertEquals(DATEVALUE("Sun, 09, Feb, 2017"), 42775);
100+assertEquals(DATEVALUE("Sun, 09/Feb/2017"), 42775);
101+assertEquals(DATEVALUE("09/Feb/2017"), 42775);
102+assertEquals(DATEVALUE("09-Feb-2017"), 42775);
103+assertEquals(DATEVALUE("09.Feb.2017"), 42775);
104+assertEquals(DATEVALUE("09 Feb/2017"), 42775);
105+assertEquals(DATEVALUE("09 . Feb . 2017"), 42775);
106+catchAndAssertEquals(function() {
107+  DATEVALUE("09.Feb/2017"); // If the delimiters don't match the first one should be a space.
108+}, ERRORS.VALUE_ERROR);
109+
110+
111+
112+
113+
114+
115+
116 // // YYYY/MM/DD HH(am|pm)
117 // assertEquals(DATEVALUE("1992/6/24 00am"), 33779);
118 // assertEquals(DATEVALUE("1992/06/24 01am "), 33779);
119@@ -366,19 +406,6 @@ catchAndAssertEquals(function() {
120 //   DATEVALUE("June 32 2001");// overload numbers not ok
121 // }, ERRORS.VALUE_ERROR);
122 // // (Dayname) DD Month YYYY
123-// assertEquals(DATEVALUE("Sun 09 Feb 2017"), 42775);
124-// assertEquals(DATEVALUE("Sun 9 Feb 2017"), 42775);
125-// assertEquals(DATEVALUE("Mon 09 Feb 2017"), 42775);
126-// assertEquals(DATEVALUE("Thursday 09 Feb 2017"), 42775);
127-// assertEquals(DATEVALUE("Thursday 09 February 2017"), 42775);
128-// assertEquals(DATEVALUE("Sun 01 September 20"), 44075);
129-// assertEquals(DATEVALUE("Sun, 09, Feb, 2017"), 42775);
130-// assertEquals(DATEVALUE("20 May 1992"), 33744);
131-// assertEquals(DATEVALUE("31 December 100"), -657070);
132-// assertEquals(DATEVALUE("13 January 0030"), 10971);
133-// assertEquals(DATEVALUE("13 January 1200"), -255656);
134-// assertEquals(DATEVALUE("22 January 2222"), 117631);
135-// assertEquals(DATEVALUE("3 November 4243"), 856071);
136 // assertEquals(DATEVALUE("29 Feb 2004"), 38046); // leap year, 29th ok
137 // catchAndAssertEquals(function() {
138 //   DATEVALUE("29 Feb 2001");// not leap year, 29th not ok