commit
message
YYYY/MM/DD HH(am|pm) parsing for DATEVALUE
author
Ben Vogt <[email protected]>
date
2017-03-10 04:20:05
stats
2 file(s) changed,
94 insertions(+),
0 deletions(-)
files
src/RawFormulas/Date.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index fbfd05b..4d264d7 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -122,6 +122,51 @@ var DATEVALUE = function (...values) : number {
6 }
7 }
8
9+ // Check YYYY/MM/DD HH(am|pm)
10+ if (m === undefined) {
11+ // For reference: https://regex101.com/r/m8FSCr/6
12+ 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])\s*(am|pm)\s*$/i);
13+ if (matches && matches.length === 8) {
14+ var years = parseInt(matches[1]);
15+ var months = parseInt(matches[4]) - 1; // Months are zero indexed.
16+ var days = parseInt(matches[5]) - 1;// Months are zero indexed.
17+ var actualYear = years;
18+ if (years >= 0 && years < 30) {
19+ actualYear = Y2K_YEAR + years;
20+ } else if (years >= 30 && years < 100) {
21+ actualYear = FIRST_YEAR + years;
22+ }
23+ var tmpMoment = moment([actualYear])
24+ .add(months, 'months');
25+ // If we're specifying more days than there are in this month
26+ if (days > tmpMoment.daysInMonth() - 1) {
27+ throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
28+ }
29+ tmpMoment = tmpMoment.add(days, 'days');
30+ m = tmpMoment;
31+ }
32+ }
33+
34+ // Check YYYY/MM/DD HH:mm
35+ if (m === undefined) {
36+ // TODO: This.
37+ }
38+
39+ // Check YYYY/MM/DD HH:mm(am|pm)
40+ if (m === undefined) {
41+ // TODO: This.
42+ }
43+
44+ // Check YYYY/MM/DD HH:mm:ss
45+ if (m === undefined) {
46+ // TODO: This.
47+ }
48+
49+ // Check YYYY/MM/DD HH:mm:ss(am|pm)
50+ if (m === undefined) {
51+ // TODO: This.
52+ }
53+
54 // If we've not been able to parse the date by now, then we cannot parse it at all.
55 if (m === undefined || !m.isValid()) {
56 throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
57diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
58index 28691d0..91f6ac8 100644
59--- a/tests/FormulasTest.ts
60+++ b/tests/FormulasTest.ts
61@@ -864,6 +864,55 @@ catchAndAssertEquals(function() {
62 catchAndAssertEquals(function() {
63 DATEVALUE("2005/1/44");// Out of range day for any month
64 }, ERRORS.VALUE_ERROR);
65+// YYYY/MM/DD HH(am|pm)
66+assertEquals(DATEVALUE("1992/6/24 00am"), 33779);
67+assertEquals(DATEVALUE("1992/06/24 01am "), 33779);
68+assertEquals(DATEVALUE("1999/1/01 02pm"), 36161);
69+assertEquals(DATEVALUE("2222/1/01 03pm"), 117610);
70+assertEquals(DATEVALUE("1902/9/02 12pm"), 976);
71+assertEquals(DATEVALUE("1902/9/2 12pm"), 976);
72+assertEquals(DATEVALUE("4243/11/3 12pm "), 856071);
73+assertEquals(DATEVALUE(" 1992/04/19 12pm "), 33713);
74+assertEquals(DATEVALUE("1992/5/20 01am"), 33744);
75+assertEquals(DATEVALUE("1992/6/21 3pm"), 33776);
76+assertEquals(DATEVALUE("1992/9/29 3pm"), 33876);
77+assertEquals(DATEVALUE("1992/1/24 3pm"), 33627);
78+assertEquals(DATEVALUE("1992/12/21 3pm"), 33959);
79+assertEquals(DATEVALUE("1992/01/31 3pm"), 33634);
80+assertEquals(DATEVALUE("1992/1/13 3pm"), 33616);
81+assertEquals(DATEVALUE("2004/2/29 3pm"), 38046);
82+assertEquals(DATEVALUE("2004/2/28 3pm "), 38045);
83+assertEquals(DATEVALUE("1999/1/13 3pm"), 36173);
84+assertEquals(DATEVALUE("1999/01/13 3pm"), 36173);
85+assertEquals(DATEVALUE("0999/01/13 3pm"), -329069);
86+assertEquals(DATEVALUE("1200/01/13 3pm"), -255656);
87+assertEquals(DATEVALUE("0029/01/13 3pm"), 47131);
88+assertEquals(DATEVALUE("0030/01/13 3pm"), 10971);
89+assertEquals(DATEVALUE("0044/01/13 3pm"), 16084);
90+assertEquals(DATEVALUE("0050/01/13 3pm"), 18276);
91+assertEquals(DATEVALUE("0097/01/13 00pm"), 35443);
92+assertEquals(DATEVALUE("0099/01/13 3pm"), 36173);
93+assertEquals(DATEVALUE("0000/01/13 3pm"), 36538);
94+assertEquals(DATEVALUE("0101/01/13 3pm"), -657057);
95+assertEquals(DATEVALUE("0100/01/13 3pm"), -657422);
96+assertEquals(DATEVALUE("100/12/31 3pm"), -657070);
97+assertEquals(DATEVALUE("122/11/10 3pm"), -649086);
98+assertEquals(DATEVALUE("2222/1/22 3pm"), 117631);
99+assertEquals(DATEVALUE("222/1/22 3pm"), -612854);
100+catchAndAssertEquals(function() {
101+ DATEVALUE("2005/2/29 000pm");// Too many digits
102+}, ERRORS.VALUE_ERROR);
103+catchAndAssertEquals(function() {
104+ DATEVALUE("2001/2/2 13pm");// Hour out of range
105+}, ERRORS.VALUE_ERROR);
106+catchAndAssertEquals(function() {
107+ DATEVALUE("2005/2/29 11am");// Leap day on non-leap year.
108+}, ERRORS.VALUE_ERROR);
109+catchAndAssertEquals(function() {
110+ DATEVALUE("2005/1/44 11am");// Out of range day for any month
111+}, ERRORS.VALUE_ERROR);
112+
113+
114 // // yyyy-m-d
115 // assertEquals(DATEVALUE("1992-6-24"), 33779);
116 // assertEquals(DATEVALUE("1992-06-24"), 33779);