From e8b9d0c2250911f125c664c8df3404d33bbf4eab Mon Sep 17 00:00:00 2001 From: giulianovarriale Date: Fri, 11 Nov 2016 10:57:51 -0200 Subject: Pass date as integer params on instantiate new Date in order to avoid time zone inconsistency --- app/assets/javascripts/due_date_select.js.es6 | 11 ++++++++--- app/assets/javascripts/lib/utils/datetime_utility.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/due_date_select.js.es6 b/app/assets/javascripts/due_date_select.js.es6 index 2b7d57d86c6..f35c024e716 100644 --- a/app/assets/javascripts/due_date_select.js.es6 +++ b/app/assets/javascripts/due_date_select.js.es6 @@ -2,9 +2,10 @@ (function(global) { class DueDateSelect { - constructor({ $dropdown, $loading } = {}) { + constructor({ $dropdown, $loading, $context } = {}) { const $dropdownParent = $dropdown.closest('.dropdown'); const $block = $dropdown.closest('.block'); + this.$context = $context || $('body'); this.$loading = $loading; this.$dropdown = $dropdown; this.$dropdownParent = $dropdownParent; @@ -80,9 +81,12 @@ } parseSelectedDate() { - this.rawSelectedDate = $("input[name='" + this.fieldName + "']").val(); + this.rawSelectedDate = this.$context.find(`input[name='${this.fieldName}']`).val(); + if (this.rawSelectedDate.length) { - let dateObj = new Date(this.rawSelectedDate); + // Avoid time zone inconsistency using the utils.createDateObject + // method, instead of the native Date object. + const dateObj = gl.utils.createDateObject(this.rawSelectedDate); this.displayedDate = $.datepicker.formatDate('M d, yy', dateObj); } else { this.displayedDate = 'No due date'; @@ -176,5 +180,6 @@ } global.DueDateSelectors = DueDateSelectors; + global.DueDateSelect = DueDateSelect; })(window.gl || (window.gl = {})); diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js index e8e502694d6..7a136a355ec 100644 --- a/app/assets/javascripts/lib/utils/datetime_utility.js +++ b/app/assets/javascripts/lib/utils/datetime_utility.js @@ -16,6 +16,19 @@ } w.gl.utils.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; + // createDateObject must be used instead of using native Date object + // to create a new Date instance using string as param - '2016-11-10' or + // '2016/11/10' in order to avoid time zone inconsistency. + w.gl.utils.createDateObject = function(string) { + var dateSeparator = string.indexOf('-') > -1 ? '-' : '/'; + + var dateArray = string.split(dateSeparator).map(function(dateItem) { + return parseInt(dateItem, 10); + }); + + return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); + } + w.gl.utils.formatDate = function(datetime) { return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z'); }; -- cgit v1.2.1 From 7d988e4483cfe13cccd797850ab6372c74c01d14 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Tue, 13 Dec 2016 15:37:59 -0600 Subject: fix eslint violations --- app/assets/javascripts/lib/utils/datetime_utility.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js index 7a136a355ec..76520f67a4d 100644 --- a/app/assets/javascripts/lib/utils/datetime_utility.js +++ b/app/assets/javascripts/lib/utils/datetime_utility.js @@ -1,4 +1,4 @@ -/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, comma-dangle, no-unused-expressions, prefer-template, padded-blocks, max-len */ +/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, comma-dangle, no-unused-expressions, prefer-template, prefer-arrow-callback, padded-blocks, max-len */ /* global timeago */ /* global dateFormat */ @@ -26,8 +26,8 @@ return parseInt(dateItem, 10); }); - return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); - } + return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); + }; w.gl.utils.formatDate = function(datetime) { return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z'); -- cgit v1.2.1 From 4d2fbe65cdcecf36c6c27553175b13b8f80abb73 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 2 Jan 2017 14:35:33 -0600 Subject: remove unnecessary fixtures and tests --- app/assets/javascripts/due_date_select.js.es6 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/due_date_select.js.es6 b/app/assets/javascripts/due_date_select.js.es6 index f35c024e716..cb92b8c7658 100644 --- a/app/assets/javascripts/due_date_select.js.es6 +++ b/app/assets/javascripts/due_date_select.js.es6 @@ -2,10 +2,9 @@ (function(global) { class DueDateSelect { - constructor({ $dropdown, $loading, $context } = {}) { + constructor({ $dropdown, $loading } = {}) { const $dropdownParent = $dropdown.closest('.dropdown'); const $block = $dropdown.closest('.block'); - this.$context = $context || $('body'); this.$loading = $loading; this.$dropdown = $dropdown; this.$dropdownParent = $dropdownParent; @@ -81,7 +80,7 @@ } parseSelectedDate() { - this.rawSelectedDate = this.$context.find(`input[name='${this.fieldName}']`).val(); + this.rawSelectedDate = $(`input[name='${this.fieldName}']`).val(); if (this.rawSelectedDate.length) { // Avoid time zone inconsistency using the utils.createDateObject @@ -180,6 +179,5 @@ } global.DueDateSelectors = DueDateSelectors; - global.DueDateSelect = DueDateSelect; })(window.gl || (window.gl = {})); -- cgit v1.2.1 From e4883e7efce8ea578b32d89be089476f1fd26134 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 2 Jan 2017 14:48:58 -0600 Subject: remove unnecessary utility function --- app/assets/javascripts/due_date_select.js.es6 | 5 ++--- app/assets/javascripts/lib/utils/datetime_utility.js | 15 +-------------- 2 files changed, 3 insertions(+), 17 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/due_date_select.js.es6 b/app/assets/javascripts/due_date_select.js.es6 index cb92b8c7658..6ea8adb7c7b 100644 --- a/app/assets/javascripts/due_date_select.js.es6 +++ b/app/assets/javascripts/due_date_select.js.es6 @@ -83,9 +83,8 @@ this.rawSelectedDate = $(`input[name='${this.fieldName}']`).val(); if (this.rawSelectedDate.length) { - // Avoid time zone inconsistency using the utils.createDateObject - // method, instead of the native Date object. - const dateObj = gl.utils.createDateObject(this.rawSelectedDate); + const dateArray = this.rawSelectedDate.split('-').map(v => parseInt(v, 10)); + const dateObj = new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); this.displayedDate = $.datepicker.formatDate('M d, yy', dateObj); } else { this.displayedDate = 'No due date'; diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js index 76520f67a4d..e8e502694d6 100644 --- a/app/assets/javascripts/lib/utils/datetime_utility.js +++ b/app/assets/javascripts/lib/utils/datetime_utility.js @@ -1,4 +1,4 @@ -/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, comma-dangle, no-unused-expressions, prefer-template, prefer-arrow-callback, padded-blocks, max-len */ +/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, comma-dangle, no-unused-expressions, prefer-template, padded-blocks, max-len */ /* global timeago */ /* global dateFormat */ @@ -16,19 +16,6 @@ } w.gl.utils.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; - // createDateObject must be used instead of using native Date object - // to create a new Date instance using string as param - '2016-11-10' or - // '2016/11/10' in order to avoid time zone inconsistency. - w.gl.utils.createDateObject = function(string) { - var dateSeparator = string.indexOf('-') > -1 ? '-' : '/'; - - var dateArray = string.split(dateSeparator).map(function(dateItem) { - return parseInt(dateItem, 10); - }); - - return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); - }; - w.gl.utils.formatDate = function(datetime) { return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z'); }; -- cgit v1.2.1 From 49acf5d831b0699d60689b7d86528f4970091b49 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 2 Jan 2017 14:49:31 -0600 Subject: make expected due_date value format explicit --- app/views/shared/issuable/_sidebar.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/views/shared/issuable/_sidebar.html.haml b/app/views/shared/issuable/_sidebar.html.haml index bc57d48ae7c..5f199301364 100644 --- a/app/views/shared/issuable/_sidebar.html.haml +++ b/app/views/shared/issuable/_sidebar.html.haml @@ -97,7 +97,7 @@ remove due date - if can?(current_user, :"admin_#{issuable.to_ability_name}", @project) .selectbox.hide-collapsed - = f.hidden_field :due_date, value: issuable.due_date + = f.hidden_field :due_date, value: issuable.due_date.try(:strftime, 'yy-mm-dd') .dropdown %button.dropdown-menu-toggle.js-due-date-select{ type: 'button', data: { toggle: 'dropdown', field_name: "#{issuable.to_ability_name}[due_date]", ability_name: issuable.to_ability_name, issue_update: issuable_json_path(issuable) } } %span.dropdown-toggle-text Due date -- cgit v1.2.1 From cf3c6a015f9ba644f1cec10319b247ef04114189 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Mon, 2 Jan 2017 15:03:16 -0600 Subject: add comment to explain why we are avoiding the dateString constructor option for our Date object --- app/assets/javascripts/due_date_select.js.es6 | 1 + 1 file changed, 1 insertion(+) (limited to 'app') diff --git a/app/assets/javascripts/due_date_select.js.es6 b/app/assets/javascripts/due_date_select.js.es6 index 6ea8adb7c7b..201f9fdc3fe 100644 --- a/app/assets/javascripts/due_date_select.js.es6 +++ b/app/assets/javascripts/due_date_select.js.es6 @@ -83,6 +83,7 @@ this.rawSelectedDate = $(`input[name='${this.fieldName}']`).val(); if (this.rawSelectedDate.length) { + // Construct Date object manually to avoid buggy dateString support within Date constructor const dateArray = this.rawSelectedDate.split('-').map(v => parseInt(v, 10)); const dateObj = new Date(dateArray[0], dateArray[1] - 1, dateArray[2]); this.displayedDate = $.datepicker.formatDate('M d, yy', dateObj); -- cgit v1.2.1