Skip to content

Commit

Permalink
Fix a bug, when using the Date.toDateOnlyISOString() method, we were …
Browse files Browse the repository at this point in the history
…calling date.toISOString() and then truncating the string. This method converts the time to UTC and may not return the date string you expect. This method indicates it should return a ISO 8601 compliant string, this only dictates that the format be in YYYY-MM-DD. I found this while on www, the day would be one off from what I was selecting.
  • Loading branch information
robotdan committed Jun 19, 2018
1 parent b870295 commit b1ba12c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion build.savant
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
savantVersion = "1.0.0"

project(group: "org.inversoft.prime", name: "prime.js", version: "1.2.0", licenses: ["ApacheV2_0"]) {
project(group: "org.inversoft.prime", name: "prime.js", version: "1.2.1", licenses: ["ApacheV2_0"]) {
workflow {
standard()
}
Expand Down Expand Up @@ -80,9 +80,11 @@ target(name: "test", description: "Runs tests for the project") {

def karma
if (switches.has("debug")) {
println " > gulp test"
karma = "gulp test".execute()
karma.consumeProcessOutput(System.out, System.err)
} else {
println " > gulp fastTest"
karma = "gulp fastTest".execute()
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/js/Date.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017, Inversoft Inc., All Rights Reserved
* Copyright (c) 2015-2018, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
* language governing permissions and limitations under the License.
*/
'use strict';
import {Utils} from "./Utils"

const PrimeDate = {
DAYS_IN_MONTH: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
Expand Down Expand Up @@ -222,7 +223,9 @@ const PrimeDate = {
*/
toDateOnlyISOString: function(date) {
if (date instanceof Date) {
return date.toISOString().substring(0, 10);
return date.getFullYear()
+ '-' + Utils.leftPadNumber(date.getMonth() + 1, 2)
+ '-' + Utils.leftPadNumber(date.getDate(), 2);
}
throw TypeError('date parameter must be a Date object.');
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/js/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ const Utils = {
return value !== null && typeof(value) !== 'undefined';
},

/**
* Left pad a number.
* @param {number} number the number to pad
* @param {number} width the width of the final result
* @returns {string}
*/
leftPadNumber: function(number, width) {
const sign = Math.sign(number) === -1 ? '-' : '';
return sign + new Array(width).concat([Math.abs(number)]).join('0').slice(-width);
},

/**
* Parses a CSS measure value (12px) as an integer.
*
Expand Down
15 changes: 14 additions & 1 deletion src/test/js/DateTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016, Inversoft Inc., All Rights Reserved
* Copyright (c) 2015-2018, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -284,4 +284,17 @@ describe('Prime.Date namespace tests', function() {
var date = new Date(2015, 6, 4); // July 4th 2015
assert.equal(Prime.Date.toDateOnlyISOString(date), '2015-07-04');
});

it('toDateOnlyISOString', function() {
var date = new Date(2018, 6, 13); // July 13th 2018
assert.equal(Prime.Date.toDateOnlyISOString(date), '2018-07-13');

// Regardless of the time of day, we should find the same day
let hour = 0;
while (hour < 24) {
date = new Date(2018, 6, 13, hour); // July 13th 2018 @ [0-23] $hour
assert.equal(Prime.Date.toDateOnlyISOString(date), '2018-07-13');
hour++;
}
});
});
4 changes: 1 addition & 3 deletions src/test/js/Document.ElementTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2015, Inversoft Inc., All Rights Reserved
* Copyright (c) 2012-2018, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1274,15 +1274,13 @@ describe('Element class tests', function() {
};
MyEventListener.prototype = {
handle: function(evt) {
console.log('handle');
this.called = true;
this.count++;
this.memo = evt.memo;
this.event = evt.event;
},

handle2: function(evt) {
console.log('handle2');
this.called2 = true;
this.count2++;
}
Expand Down

0 comments on commit b1ba12c

Please sign in to comment.