commit
message
[SECOND] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-20 02:10:50
stats
3 file(s) changed,
58 insertions(+),
5 deletions(-)
files
src/RawFormulas/Date.ts
src/RawFormulas/RawFormulas.ts
tests/DateFormulasTest.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index 8621ce3..ac577e8 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -586,6 +586,23 @@ var MINUTE = function (...values) {
6 return m.minute();
7 };
8
9+/**
10+ * Returns the second component of a specific time, in numeric format.
11+ * @param values[0] time - The time from which to calculate the second component. Must be a reference to a cell
12+ * containing a date/time, a function returning a date/time type, or a number.
13+ * @returns {number} second component of a specific time.
14+ * @constructor
15+ */
16+var SECOND = function (...values) : number {
17+ ArgsChecker.checkLength(values, 1);
18+ var time = TypeCaster.firstValueAsTimestampNumber(values[0]);
19+ if (time % 1 === 0) {
20+ return 0;
21+ }
22+ var m = moment.utc([1900]).add(time * MILLISECONDS_IN_DAY, "milliseconds");
23+ return m.second();
24+};
25+
26
27 // Functions unimplemented.
28 var NETWORKDAYS;
29@@ -594,7 +611,6 @@ var __COMPLEX_ITL = {
30 "WORKDAY.INTL": function () {}
31 };
32 var NOW;
33-var SECOND;
34 var TIME;
35 var TODAY;
36 var WORKDAY;
37@@ -615,5 +631,6 @@ export {
38 YEARFRAC,
39 TIMEVALUE,
40 HOUR,
41- MINUTE
42+ MINUTE,
43+ SECOND
44 }
45\ No newline at end of file
46diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
47index 05aaf13..ba1b430 100644
48--- a/src/RawFormulas/RawFormulas.ts
49+++ b/src/RawFormulas/RawFormulas.ts
50@@ -124,7 +124,8 @@ import {
51 YEARFRAC,
52 TIMEVALUE,
53 HOUR,
54- MINUTE
55+ MINUTE,
56+ SECOND
57 } from "./Date"
58
59 var ACCRINT = Formula["ACCRINT"];
60@@ -252,5 +253,6 @@ export {
61 DATEDIF,
62 TIMEVALUE,
63 HOUR,
64- MINUTE
65+ MINUTE,
66+ SECOND
67 }
68\ No newline at end of file
69diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
70index 0be98f0..7db05cc 100644
71--- a/tests/DateFormulasTest.ts
72+++ b/tests/DateFormulasTest.ts
73@@ -15,7 +15,8 @@ import {
74 YEARFRAC,
75 TIMEVALUE,
76 HOUR,
77- MINUTE
78+ MINUTE,
79+ SECOND
80 } from "../src/RawFormulas/RawFormulas"
81 import * as ERRORS from "../src/Errors"
82 import {assertEquals} from "./utils/Asserts"
83@@ -36,6 +37,38 @@ function catchAndAssertEquals(toExecute, expected) {
84 }
85 }
86
87+
88+// Test SECOND
89+assertEquals(SECOND("8:10"), 0);
90+assertEquals(SECOND("8:11"), 0);
91+assertEquals(SECOND("8:44"), 0);
92+assertEquals(SECOND("8:70"), 0);
93+assertEquals(SECOND("8:120"), 0);
94+assertEquals(SECOND("8:10:22"), 22);
95+assertEquals(SECOND("8:11:12"), 12);
96+assertEquals(SECOND("8:44:09"), 9);
97+assertEquals(SECOND("8:70:02"), 2);
98+assertEquals(SECOND("8:120:44"), 44);
99+assertEquals(SECOND("8:120:104"), 44);
100+assertEquals(SECOND("1992-1-1 8:120:104"), 44);
101+assertEquals(SECOND(0.511111111111), 0);
102+catchAndAssertEquals(function() {
103+ SECOND("8:10", 5);
104+}, ERRORS.NA_ERROR);
105+catchAndAssertEquals(function() {
106+ SECOND();
107+}, ERRORS.NA_ERROR);
108+catchAndAssertEquals(function() {
109+ SECOND("str");
110+}, ERRORS.VALUE_ERROR);
111+catchAndAssertEquals(function() {
112+ SECOND(" ");
113+}, ERRORS.VALUE_ERROR);
114+catchAndAssertEquals(function() {
115+ SECOND([]);
116+}, ERRORS.REF_ERROR);
117+
118+
119 // Test MINUTE
120 assertEquals(MINUTE("8:10"), 10);
121 assertEquals(MINUTE("8:11"), 11);