commit
message
[ExcelTime, TIME] It would be useful to have a class for time of day
author
Ben Vogt <[email protected]>
date
2017-04-27 02:30:55
stats
3 file(s) changed,
58 insertions(+),
5 deletions(-)
files
README.md
src/ExcelTime.ts
src/RawFormulas/Date.ts
1diff --git a/README.md b/README.md
2index 70516dc..55d103f 100644
3--- a/README.md
4+++ b/README.md
5@@ -35,7 +35,7 @@ Listing them inside RawFormulas.ts is unwieldy.
6 Pass name of calling formula into all functions that throw user-facing errors, or have some sort of error mapper.
7
8 ### Dates have special types
9-Like dollars, dates are special types, but can be compared as if they're primatives. For example, this statement is
10+Like dollars, dates are special types, but can be compared as if they're primitives. For example, this statement is
11 valid inside a cell: `=DATE(1992, 6, 6) > =DATE(1992, 6, 10)`. We should check types and and have Date-to-number
12 conversion inside parser.js.
13
14diff --git a/src/ExcelTime.ts b/src/ExcelTime.ts
15new file mode 100644
16index 0000000..2f3a8cb
17--- /dev/null
18+++ b/src/ExcelTime.ts
19@@ -0,0 +1,45 @@
20+/// <reference path="../node_modules/moment/moment.d.ts"/>
21+import * as moment from "moment";
22+
23+class ExcelTime {
24+ // Value representing the time of day. Between 0 and 1, exclusive on end.
25+ private fractionOfDay: number;
26+
27+
28+ /**
29+ * Create ExcelTime from hours, seconds, and minutes. All capable of being overloaded, but are modded.
30+ * @param hours in this timestamp.
31+ * @param minutes in this timestamp.
32+ * @param seconds in this timestamp.
33+ */
34+ constructor(hours: number, minutes: number, seconds: number) {
35+ this.fractionOfDay = (((hours % 24) * 60* 60) + ((minutes % 60) * 60) + (seconds % 60)) / 86400;
36+ }
37+
38+
39+ /**
40+ * Returns the number of seconds in this timestamp.
41+ * @returns {number} of seconds.
42+ */
43+ toNumber() : number {
44+ return this.fractionOfDay;
45+ }
46+
47+ /**
48+ * Returns the string in the format "12:04:09 AM".
49+ * @returns {string} representing this timestamp.
50+ */
51+ toString() : string {
52+ return moment.utc([1900]).startOf("year").add(this.fractionOfDay * 86400, "seconds").format("h:mm:ss A");
53+ }
54+
55+ /**
56+ * Equality checking.
57+ * @param other ExcelTime to compare to.
58+ * @returns {boolean} true if equals
59+ */
60+ equals(other: ExcelTime) : boolean {
61+ return other.toNumber() === this.toNumber();
62+ }
63+
64+}
65\ No newline at end of file
66diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
67index aa9a509..b440e2c 100644
68--- a/src/RawFormulas/Date.ts
69+++ b/src/RawFormulas/Date.ts
70@@ -24,9 +24,9 @@ import {
71 */
72 var DATE = function (...values) : ExcelDate {
73 const FIRST_YEAR = 1900;
74+ ArgsChecker.checkLength(values, 3);
75 var year = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0]))); // No negative values for year
76 var month = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1; // Months are between 0 and 11.
77- ArgsChecker.checkLength(values, 3);
78 var day = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1; // Days are also zero-indexed.
79 var m = moment.utc(ORIGIN_MOMENT)
80 .add(2, "days")
81@@ -790,9 +790,16 @@ var TODAY = function (...values) {
82 };
83
84
85+var TIME = function (...values) {
86+ ArgsChecker.checkLength(values, 3);
87+ var hours = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0])));
88+ var minutes = Math.floor(TypeCaster.firstValueAsNumber(values[1])) - 1;
89+ var seconds = Math.floor(TypeCaster.firstValueAsNumber(values[2])) - 1;
90+};
91+
92+
93 // Functions unimplemented.
94 var WORKDAY$INTL;
95-var TIME;
96 var WORKDAY;
97
98 export {