Skip to content

Commit

Permalink
Fixed Date test; cleaned up Date() library range; checking to see if …
Browse files Browse the repository at this point in the history
…Pico now uses more or less memory.
  • Loading branch information
deirdreobyrne committed Jan 29, 2024
1 parent a2552d6 commit fcaadd8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/jswrap_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const char *MONTHNAMES = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0Jul\0Aug\0Sep\0Oct\0Nov\
const char *DAYNAMES = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";

#ifdef ESPR_LIMIT_DATE_RANGE
#define INTEGER_DIVIDE_FLOOR(a,b) (a/b)

int integerDivideFloor(int a, int b) {
return a/b;
}

#define INTEGER_DIVIDE_FLOOR(a,b) integerDivideFloor(a,b)

#else
#define INTEGER_DIVIDE_FLOOR(a,b) ((a<0 ? a-b+1 : a)/b)
#endif
Expand All @@ -36,9 +42,9 @@ int getDayNumberFromDate(int y, int m, int d) {
int ans;

#ifdef ESPR_LIMIT_DATE_RANGE
if (y < 1601 || y > 1250000) {
if (y < 1500 || y >= 1250000) { // Should actually work down to 1101, but since the Gregorian calendar started in 1582 . . .
#else
if (y < -1265580 || y > 1269519) {
if (y < -1250000 || y >= 1250000) {
#endif
jsExceptionHere(JSET_ERROR, "Date out of bounds");
return 0; // Need to head off any overflow error
Expand Down Expand Up @@ -402,9 +408,9 @@ Set the time/date of this Date class
*/
JsVarFloat jswrap_date_setTime(JsVar *date, JsVarFloat timeValue) {
#ifdef ESPR_LIMIT_DATE_RANGE
if (timeValue < -1.16e13 || timeValue > 3.0e16) {
if (timeValue < -1.48317696e13 || timeValue >= 3.93840543168E+016) { // This should actually work down to 1101AD . . .
#else
if (fabs(timeValue) > 4.0e16) {
if (timeValue < -3.95083256832E+016 || timeValue >= 3.93840543168E+016) {
#endif
jsExceptionHere(JSET_ERROR, "Date out of bounds");
return 0.0;
Expand Down
7 changes: 5 additions & 2 deletions tests/test_date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ try { new Date(4.1e16); pass--; } catch(e) { };
try { new Date(-4.1e16); pass--; } catch(e) { };
try { new Date(2000000,0); pass--; } catch(e) { };
try { new Date(-2000000,0); pass--; } catch(e) { };
// This next Date() constructor use to give a segfault. However it is now out of range for the PICO_R1_3
try { a = new Date(-12,8).toString(); if (!(a== "Wed Aug 31 -12 00:00:00 GMT+0000")) pass--; } catch (e) { };

var gmt = [
[ Date.parse("2011-10-20") , 1319068800000.0 ],
Expand All @@ -17,8 +19,9 @@ var gmt = [
[ new Date("Fri, 20 Jun 2014 15:27:22 GMT").toString(), "Fri Jun 20 2014 15:27:22 GMT+0000"],
[ new Date("Fri, 20 Jun 2014 15:27:22 GMT").toISOString(), "2014-06-20T15:27:22.000Z"],
[ new Date("Fri, 20 Jun 2014 17:27:22 GMT+0200").toISOString(), "2014-06-20T15:27:22.000Z"],
[ new Date("5000-1-1").toISOString(), "5000-01-01T00:00:00.000Z"],
[ new Date(-12,8).toString(), "Wed Aug 31 -12 00:00:00 GMT+0000"]
[ new Date("5000-1-1").toString(), "Wed Jan 1 5000 00:00:00 GMT+0000"],
[ new Date(1500,0,1).toString(), "Mon Jan 1 1500 00:00:00 GMT+0000"],
[ new Date(1500,0,1).getTime(), -14831769600000]
];

gmt.forEach(function(n) { if (n[0]==n[1]) pass++; });
Expand Down

0 comments on commit fcaadd8

Please sign in to comment.