spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
WIP on ap/pm and minute overflow for YYYY/MM/DD HH:mm(am|pm)
author
Ben Vogt <[email protected]>
date
2017-03-12 18:43:57
stats
2 file(s) changed, 98 insertions(+), 5 deletions(-)
files
src/RawFormulas/Date.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
  2index ccffb35..24850f6 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -78,7 +78,7 @@ var DATEVALUE = function (...values) : number {
  6     if (matches && matches.length === 6) {
  7       var years = parseInt(matches[1]);
  8       var months = parseInt(matches[4]) - 1; // Months are zero indexed.
  9-      var days = parseInt(matches[5]) - 1;// Months are zero indexed.
 10+      var days = parseInt(matches[5]) - 1;// Days are zero indexed.
 11       var actualYear = years;
 12       if (years >= 0 && years < 30) {
 13         actualYear = Y2K_YEAR + years;
 14@@ -101,8 +101,8 @@ var DATEVALUE = function (...values) : number {
 15     var matches = dateString.match(/^\s*([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\/(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9])|[0-9]{0,3})\s*$/);
 16     if (matches && matches.length === 6) {
 17       var years = parseInt(matches[3]);
 18-      var months = parseInt(matches[1]) - 1;
 19-      var days = parseInt(matches[2]) - 1;
 20+      var months = parseInt(matches[1]) - 1; // Months are zero indexed.
 21+      var days = parseInt(matches[2]) - 1; // Days are zero indexed.
 22       var actualYear = years;
 23       if (years >= 0 && years < 30) {
 24         actualYear = Y2K_YEAR + years;
 25@@ -126,7 +126,7 @@ var DATEVALUE = function (...values) : number {
 26     if (matches && matches.length === 8) {
 27       var years = parseInt(matches[1]);
 28       var months = parseInt(matches[4]) - 1; // Months are zero indexed.
 29-      var days = parseInt(matches[5]) - 1;// Months are zero indexed.
 30+      var days = parseInt(matches[5]) - 1;// Days are zero indexed.
 31       var actualYear = years;
 32       if (years >= 0 && years < 30) {
 33         actualYear = Y2K_YEAR + years;
 34@@ -150,7 +150,7 @@ var DATEVALUE = function (...values) : number {
 35     if (matches && matches.length === 8) {
 36       var years = parseInt(matches[1]);
 37       var months = parseInt(matches[4]) - 1; // Months are zero indexed.
 38-      var days = parseInt(matches[5]) - 1;// Months are zero indexed.
 39+      var days = parseInt(matches[5]) - 1;// Days are zero indexed.
 40       var hours = parseInt(matches[6]);
 41       var minutes = parseInt(matches[7]);
 42       var actualYear = years;
 43@@ -171,7 +171,53 @@ var DATEVALUE = function (...values) : number {
 44 
 45   // Check YYYY/MM/DD HH:mm(am|pm)
 46   if (m === undefined) {
 47-    // TODO: This.
 48+    // For reference: https://regex101.com/r/DMA4Fv/2
 49+    var matches = dateString.match(/^\s*(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9]))\/([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\s*([0-9]|0[0-9]|1[0-2]):([0-9]+)\s*(am|pm)\s*$/i);
 50+    if (matches && matches.length === 9) {
 51+      var years = parseInt(matches[1]);
 52+      var months = parseInt(matches[4]) - 1; // Months are zero indexed.
 53+      var days = parseInt(matches[5]) - 1;// Days are zero indexed.
 54+      var hours = parseInt(matches[6]);
 55+      var minutes = parseInt(matches[7]);
 56+      var pm = matches[8].toLowerCase() === "pm";
 57+      console.log(hours, minutes, pm ? "pm" : "am");
 58+      var actualYear = years;
 59+      if (years >= 0 && years < 30) {
 60+        actualYear = Y2K_YEAR + years;
 61+      } else if (years >= 30 && years < 100) {
 62+        actualYear = FIRST_YEAR + years;
 63+      }
 64+      var tmpMoment = moment([actualYear])
 65+        .add(months, 'months');
 66+      // If we're specifying more days than there are in this month
 67+      if (days > tmpMoment.daysInMonth() - 1) {
 68+        throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 69+      }
 70+      var ORIGIN_MOMENT = moment([FIRST_YEAR]);
 71+      tmpMoment.add(days, 'days');
 72+      console.log("added days", days, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
 73+      if (pm) {
 74+        if (hours === 12) {
 75+          tmpMoment.set('hours', hours);
 76+        } else {
 77+          tmpMoment.set('hours', 12 + hours);
 78+        }
 79+      } else {
 80+        tmpMoment.set('hours', hours);
 81+      }
 82+      console.log("set pm hours", hours, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2, pm);
 83+      tmpMoment.add(minutes, 'minutes');
 84+      console.log("added minutes", minutes, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
 85+      tmpMoment.set('hours', 0).set('minutes', 0);
 86+      console.log("cleaned off", tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
 87+      m = tmpMoment;
 88+
 89+      // m = tmpMoment.add(days, 'days')
 90+      //   .add(hours, 'hours')
 91+      //   .add(minutes, 'minutes')
 92+      //   .set('hours', 0)
 93+      //   .set('minutes', 0);
 94+    }
 95   }
 96 
 97   // Check YYYY/MM/DD HH:mm:ss
 98diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 99index 5f4b208..0df7edf 100644
100--- a/tests/FormulasTest.ts
101+++ b/tests/FormulasTest.ts
102@@ -923,6 +923,51 @@ assertEquals(DATEVALUE("1992/6/24 23:59"), 33779);
103 assertEquals(DATEVALUE("1999/1/13 10:11111111"), 43889);
104 assertEquals(DATEVALUE("1999/1/13 25000:22"), 37214);
105 assertEquals(DATEVALUE("1999/1/13 25000:    22"), 37214);
106+// YYYY/MM/DD HH:mm(am|pm)
107+assertEquals(DATEVALUE("1992/6/24 00:00am"), 33779);
108+assertEquals(DATEVALUE("1992/06/24 01:44am "), 33779);
109+assertEquals(DATEVALUE("1999/1/01 02:59pm"), 36161);
110+assertEquals(DATEVALUE("2222/1/01 03:33pm"), 117610);
111+assertEquals(DATEVALUE("1902/9/02 12:33pm"), 976);
112+assertEquals(DATEVALUE("1902/9/2 12:33pm"), 976);
113+assertEquals(DATEVALUE("4243/11/3 12:33pm   "), 856071);
114+assertEquals(DATEVALUE("  1992/04/19   12:33pm   "), 33713);
115+assertEquals(DATEVALUE("1992/5/20 01:33am"), 33744);
116+assertEquals(DATEVALUE("1992/6/21  3:33pm"), 33776);
117+assertEquals(DATEVALUE("1992/9/29 3:33pm"), 33876);
118+assertEquals(DATEVALUE("1992/1/24 3:33pm"), 33627);
119+assertEquals(DATEVALUE("1992/12/21 3:33pm"), 33959);
120+assertEquals(DATEVALUE("1992/01/31 3:33pm"), 33634);
121+assertEquals(DATEVALUE("1992/1/13 3:33pm"), 33616);
122+assertEquals(DATEVALUE("2004/2/29 3:33pm"), 38046);
123+assertEquals(DATEVALUE("2004/2/28  3:33pm "), 38045);
124+assertEquals(DATEVALUE("1999/1/13 3:33pm"), 36173);
125+assertEquals(DATEVALUE("1999/01/13 3:33pm"), 36173);
126+assertEquals(DATEVALUE("0999/01/13 3:33pm"), -329069);
127+assertEquals(DATEVALUE("1200/01/13 3:33pm"), -255656);
128+assertEquals(DATEVALUE("0029/01/13 3:33pm"), 47131);
129+assertEquals(DATEVALUE("0030/01/13 3:33pm"), 10971);
130+assertEquals(DATEVALUE("0044/01/13 3:33pm"), 16084);
131+assertEquals(DATEVALUE("0050/01/13 3:33pm"), 18276);
132+assertEquals(DATEVALUE("0097/01/13 00:33pm"), 35443);
133+assertEquals(DATEVALUE("0099/01/13 3:33pm"), 36173);
134+assertEquals(DATEVALUE("0000/01/13 3:33pm"), 36538);
135+assertEquals(DATEVALUE("0101/01/13 3:33pm"), -657057);
136+assertEquals(DATEVALUE("0100/01/13 3:33pm"), -657422);
137+assertEquals(DATEVALUE("100/12/31 3:33pm"), -657070);
138+assertEquals(DATEVALUE("122/11/10 3:33pm"), -649086);
139+assertEquals(DATEVALUE("2222/1/22 3:33pm"), 117631);
140+assertEquals(DATEVALUE("222/1/22 3:33pm"), -612854);
141+assertEquals(DATEVALUE("1992/1/13 6:22222222am"), 49048); // overload minutes
142+assertEquals(DATEVALUE("1992/1/13 12:720pm"), 33617); // overload minutes
143+assertEquals(DATEVALUE("1992/1/13 00:720pm"), 33617); // overload minutes
144+assertEquals(DATEVALUE("1992/1/13 12:719pm"), 33616); // overload minutes
145+assertEquals(DATEVALUE("1992/1/13 00:720am"), 33616); // overload minutes
146+
147+assertEquals(DATEVALUE("1992/1/13 12:666669am"), 34078); // overload minutes
148+assertEquals(DATEVALUE("1992/1/13 12:666669pm"), 34079); // overload minutes
149+
150+
151 
152 
153