spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[TIME] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-28 23:39:17
stats
4 file(s) changed, 54 insertions(+), 13 deletions(-)
files
src/ExcelTime.ts
src/RawFormulas/Date.ts
src/RawFormulas/RawFormulas.ts
tests/DateFormulasTest.ts
  1diff --git a/src/ExcelTime.ts b/src/ExcelTime.ts
  2index cf82082..6fa22aa 100644
  3--- a/src/ExcelTime.ts
  4+++ b/src/ExcelTime.ts
  5@@ -13,7 +13,8 @@ class ExcelTime {
  6    * @param seconds in this timestamp.
  7    */
  8   constructor(hours: number, minutes: number, seconds: number) {
  9-    this.fractionOfDay = (((hours % 24) * 60* 60) + ((minutes % 60) * 60) + (seconds % 60)) / 86400;
 10+    var v = (((hours % 24) * 60 * 60) + ((minutes) * 60) + (seconds)) / 86400;
 11+    this.fractionOfDay = v % 1;
 12   }
 13 
 14 
 15diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 16index d3babe6..568c76d 100644
 17--- a/src/RawFormulas/Date.ts
 18+++ b/src/RawFormulas/Date.ts
 19@@ -794,17 +794,24 @@ var TODAY = function (...values) {
 20 
 21 
 22 /**
 23- *
 24- * @param values
 25- * @returns {ExcelTime}
 26+ * Converts a provided hour, minute, and second into a time. Will silently recalculate numeric time values which fall
 27+ * outside of valid ranges. Eg: TIME(24, 0, 0) is the same as TIME(0, 0, 0).
 28+ * @param values[0] hour - The hour component of the time.
 29+ * @param values[1] minute - The minute component of the time.
 30+ * @param values[2] second - The second component of the time.
 31+ * @returns {ExcelTime} time
 32  * @constructor
 33  */
 34 var TIME = function (...values) : ExcelTime {
 35   ArgsChecker.checkLength(values, 3);
 36-  var hours = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0])));
 37-  var minutes = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1;
 38-  var seconds = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1;
 39-  return new ExcelTime(hours, minutes, seconds);
 40+  var hours = Math.floor(TypeCaster.firstValueAsNumber(values[0]));
 41+  var minutes = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
 42+  var seconds = Math.floor(TypeCaster.firstValueAsNumber(values[2]));
 43+  var e = new ExcelTime(hours, minutes, seconds);
 44+  if (e.toNumber() < 0) {
 45+    throw new NumError("TIME evaluates to an out of range value -1.201273148. It should be greater than or equal to 0.");
 46+  }
 47+  return e;
 48 };
 49 
 50 
 51@@ -833,5 +840,6 @@ export {
 52   NETWORKDAYS,
 53   NETWORKDAYS$INTL,
 54   NOW,
 55-  TODAY
 56+  TODAY,
 57+  TIME
 58 }
 59\ No newline at end of file
 60diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 61index c0d2a4e..f2a7682 100644
 62--- a/src/RawFormulas/RawFormulas.ts
 63+++ b/src/RawFormulas/RawFormulas.ts
 64@@ -129,7 +129,8 @@ import {
 65   NETWORKDAYS,
 66   NETWORKDAYS$INTL,
 67   NOW,
 68-  TODAY
 69+  TODAY,
 70+  TIME
 71 } from "./Date"
 72 
 73 var ACCRINT = Formula["ACCRINT"];
 74@@ -262,5 +263,6 @@ export {
 75   NETWORKDAYS,
 76   NETWORKDAYS$INTL,
 77   NOW,
 78-  TODAY
 79+  TODAY,
 80+  TIME
 81 }
 82\ No newline at end of file
 83diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 84index 511e1ed..443accc 100644
 85--- a/tests/DateFormulasTest.ts
 86+++ b/tests/DateFormulasTest.ts
 87@@ -18,7 +18,8 @@ import {
 88   MINUTE,
 89   SECOND,
 90   NETWORKDAYS,
 91-  NETWORKDAYS$INTL
 92+  NETWORKDAYS$INTL,
 93+  TIME
 94 } from "../src/RawFormulas/RawFormulas"
 95 import * as ERRORS from "../src/Errors"
 96 import {
 97@@ -27,6 +28,37 @@ import {
 98 } from "./utils/Asserts"
 99 import moment = require("moment");
100 
101+// Test TIME
102+assertEquals(TIME(10, 10, 10).toNumber(), 0.4237268518518518);
103+assertEquals(TIME(34, 10, 10).toNumber(), 0.4237268518518518);
104+assertEquals(TIME(29, 10, 10).toNumber(), 0.2153935185185185);
105+assertEquals(TIME(13, 9, 6).toNumber(), 0.5479861111111111);
106+assertEquals(TIME(3, 1, 14).toNumber(), 0.12585648148148149);
107+assertEquals(TIME(0, 0, 0).toNumber(), 0);
108+assertEquals(TIME(24, 0, 0).toNumber(), 0);
109+assertEquals(TIME(23, 60, 0).toNumber(), 0);
110+assertEquals(TIME(23, 59, 60).toNumber(), 0);
111+assertEquals(TIME(18, 0, 0).toNumber(), 0.75);
112+assertEquals(TIME(12, 0, 0).toNumber(), 0.5);
113+assertEquals(TIME(6, 0, 0).toNumber(), 0.25);
114+assertEquals(TIME(3, 0, 0).toNumber(), 0.125);
115+assertEquals(TIME("3", ["0"], false).toNumber(), 0.125);
116+catchAndAssertEquals(function() {
117+  TIME();
118+}, ERRORS.NA_ERROR);
119+catchAndAssertEquals(function() {
120+  TIME(1, 1);
121+}, ERRORS.NA_ERROR);
122+catchAndAssertEquals(function() {
123+  TIME(1, 1, 1, 1);
124+}, ERRORS.NA_ERROR);
125+catchAndAssertEquals(function() {
126+  TIME(-29, 10, 10);
127+}, ERRORS.NUM_ERROR);
128+catchAndAssertEquals(function() {
129+  TIME(1, 1, []);
130+}, ERRORS.REF_ERROR);
131+
132 
133 // Test NETWORKDAYS$INTL
134 assertEquals(NETWORKDAYS$INTL("1992-1-1", "1992-1-30"), 22);