spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
experimenting with using utc() instead of dst
author
Ben Vogt <[email protected]>
date
2017-03-12 19:25:13
stats
3 file(s) changed, 21 insertions(+), 27 deletions(-)
files
src/ExcelDate.ts
src/RawFormulas/Date.ts
tests/FormulasTest.ts
  1diff --git a/src/ExcelDate.ts b/src/ExcelDate.ts
  2index 4b21bf4..8613997 100644
  3--- a/src/ExcelDate.ts
  4+++ b/src/ExcelDate.ts
  5@@ -1,7 +1,7 @@
  6 /// <reference path="../node_modules/moment/moment.d.ts"/>
  7 import * as moment from "moment";
  8 
  9-const ORIGIN_MOMENT = moment([1900]);
 10+const ORIGIN_MOMENT = moment.utc([1900]);
 11 
 12 
 13 /**
 14@@ -28,7 +28,7 @@ class ExcelDate {
 15    * @returns {string} day in the format M/D/YYYY.
 16    */
 17   toString() {
 18-    return ORIGIN_MOMENT.add('days', this.day).format("M/D/Y");
 19+    return ORIGIN_MOMENT.add(this.day, 'days').format("M/D/Y");
 20   }
 21 
 22   /**
 23diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 24index 24850f6..e33bbd2 100644
 25--- a/src/RawFormulas/Date.ts
 26+++ b/src/RawFormulas/Date.ts
 27@@ -24,12 +24,12 @@ import {
 28  */
 29 var DATE = function (...values) {
 30   const FIRST_YEAR = 1900;
 31-  const ORIGIN_DATE = moment([FIRST_YEAR]);
 32+  const ORIGIN_DATE = moment.utc([FIRST_YEAR]);
 33   ArgsChecker.checkLength(values, 3);
 34   var year = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0]))); // No negative values for year
 35   var month = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1; // Months are between 0 and 11.
 36   var day = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1; // Days are also zero-indexed.
 37-  var m = moment(ORIGIN_DATE)
 38+  var m = moment.utc(ORIGIN_DATE)
 39       .add(year < FIRST_YEAR ? year : year - FIRST_YEAR, 'years') // If the value is less than 1900, assume 1900 as start index for year
 40       .add(month, 'months')
 41       .add(day, 'days');
 42@@ -85,7 +85,7 @@ var DATEVALUE = function (...values) : number {
 43       } else if (years >= 30 && years < 100) {
 44         actualYear = FIRST_YEAR + years;
 45       }
 46-      var tmpMoment = moment([actualYear])
 47+      var tmpMoment = moment.utc([actualYear])
 48         .add(months, 'months');
 49       // If we're specifying more days than there are in this month
 50       if (days > tmpMoment.daysInMonth() - 1) {
 51@@ -109,7 +109,7 @@ var DATEVALUE = function (...values) : number {
 52       } else if (years >= 30 && years < 100) {
 53         actualYear = FIRST_YEAR + years;
 54       }
 55-      var tmpMoment = moment([actualYear])
 56+      var tmpMoment = moment.utc([actualYear])
 57         .add(months, 'months');
 58       // If we're specifying more days than there are in this month
 59       if (days > tmpMoment.daysInMonth() - 1) {
 60@@ -133,7 +133,7 @@ var DATEVALUE = function (...values) : number {
 61       } else if (years >= 30 && years < 100) {
 62         actualYear = FIRST_YEAR + years;
 63       }
 64-      var tmpMoment = moment([actualYear])
 65+      var tmpMoment = moment.utc([actualYear])
 66         .add(months, 'months');
 67       // If we're specifying more days than there are in this month
 68       if (days > tmpMoment.daysInMonth() - 1) {
 69@@ -159,7 +159,7 @@ var DATEVALUE = function (...values) : number {
 70       } else if (years >= 30 && years < 100) {
 71         actualYear = FIRST_YEAR + years;
 72       }
 73-      var tmpMoment = moment([actualYear])
 74+      var tmpMoment = moment.utc([actualYear])
 75         .add(months, 'months');
 76       // If we're specifying more days than there are in this month
 77       if (days > tmpMoment.daysInMonth() - 1) {
 78@@ -180,22 +180,19 @@ var DATEVALUE = function (...values) : number {
 79       var hours = parseInt(matches[6]);
 80       var minutes = parseInt(matches[7]);
 81       var pm = matches[8].toLowerCase() === "pm";
 82-      console.log(hours, minutes, pm ? "pm" : "am");
 83       var actualYear = years;
 84       if (years >= 0 && years < 30) {
 85         actualYear = Y2K_YEAR + years;
 86       } else if (years >= 30 && years < 100) {
 87         actualYear = FIRST_YEAR + years;
 88       }
 89-      var tmpMoment = moment([actualYear])
 90-        .add(months, 'months');
 91+      var tmpMoment = moment.utc([actualYear])
 92+        .add({"months": months});
 93       // If we're specifying more days than there are in this month
 94       if (days > tmpMoment.daysInMonth() - 1) {
 95         throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
 96       }
 97-      var ORIGIN_MOMENT = moment([FIRST_YEAR]);
 98-      tmpMoment.add(days, 'days');
 99-      console.log("added days", days, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
100+      tmpMoment.add({"days": days});
101       if (pm) {
102         if (hours === 12) {
103           tmpMoment.set('hours', hours);
104@@ -203,20 +200,11 @@ var DATEVALUE = function (...values) : number {
105           tmpMoment.set('hours', 12 + hours);
106         }
107       } else {
108-        tmpMoment.set('hours', hours);
109+        if (hours !== 12) {
110+          tmpMoment.set('hours', hours);
111+        }
112       }
113-      console.log("set pm hours", hours, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2, pm);
114-      tmpMoment.add(minutes, 'minutes');
115-      console.log("added minutes", minutes, tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
116-      tmpMoment.set('hours', 0).set('minutes', 0);
117-      console.log("cleaned off", tmpMoment, tmpMoment.diff(ORIGIN_MOMENT, "days") + 2);
118-      m = tmpMoment;
119-
120-      // m = tmpMoment.add(days, 'days')
121-      //   .add(hours, 'hours')
122-      //   .add(minutes, 'minutes')
123-      //   .set('hours', 0)
124-      //   .set('minutes', 0);
125+      m = tmpMoment.add({"minutes": minutes}).set('hours', 0).set('minutes', 0);
126     }
127   }
128 
129diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
130index 0df7edf..3bc5104 100644
131--- a/tests/FormulasTest.ts
132+++ b/tests/FormulasTest.ts
133@@ -963,7 +963,10 @@ assertEquals(DATEVALUE("1992/1/13 12:720pm"), 33617); // overload minutes
134 assertEquals(DATEVALUE("1992/1/13 00:720pm"), 33617); // overload minutes
135 assertEquals(DATEVALUE("1992/1/13 12:719pm"), 33616); // overload minutes
136 assertEquals(DATEVALUE("1992/1/13 00:720am"), 33616); // overload minutes
137-
138+assertEquals(DATEVALUE("1992/1/13 12:66669pm"), 33662); // overload minutes
139+assertEquals(DATEVALUE("1992/1/13 12:66669am"), 33662); // overload minutes
140+assertEquals(DATEVALUE("1992/1/13 12:66249pm"), 33662); // overload minutes
141+assertEquals(DATEVALUE("1992/1/13 12:66249am"), 33662); // overload minutes
142 assertEquals(DATEVALUE("1992/1/13 12:666669am"), 34078); // overload minutes
143 assertEquals(DATEVALUE("1992/1/13 12:666669pm"), 34079); // overload minutes
144