diff --git a/frontend/app/components/input/transformers/transform-date-utc.directive.ts b/frontend/app/components/input/transformers/transform-date-utc.directive.ts index 15b2b01a28..b936686b8b 100644 --- a/frontend/app/components/input/transformers/transform-date-utc.directive.ts +++ b/frontend/app/components/input/transformers/transform-date-utc.directive.ts @@ -34,15 +34,15 @@ function transformDateUtc(TimezoneService:any) { require: '^ngModel', link: function (scope:ng.IScope, element:HTMLElement, attrs:any, ngModelController:any) { ngModelController.$parsers.push(function (data:string) { - if (!moment(data, 'YYYY-MM-DD', true).isValid()) { - return undefined; + if (!TimezoneService.isValidISODate(data)) { + return ''; } var d = TimezoneService.parseLocalDateTime(data); return TimezoneService.formattedISODateTime(d); }); ngModelController.$formatters.push(function (data:string) { - if (!moment(data, 'YYYY-MM-DDTHH:mm:ssZ', true).isValid()) { - return undefined; + if (!TimezoneService.isValidISODateTime(data)) { + return ''; } var d = TimezoneService.parseISODatetime(data); return TimezoneService.formattedISODate(d); diff --git a/frontend/app/services/timezone-service.js b/frontend/app/services/timezone-service.js index 99fe23faff..510c60a307 100644 --- a/frontend/app/services/timezone-service.js +++ b/frontend/app/services/timezone-service.js @@ -84,15 +84,7 @@ module.exports = function(ConfigurationService, I18n) { * @returns {Moment} */ parseISODatetime: function(datetime) { - var result = moment(datetime, 'YYYY-MM-DDTHH:mm:ssZ'); - - if (ConfigurationService.isTimezoneSet()) { - // TODO:needs to be moved out of the conditional, all date/datetimes need to be displayed using the system timezone - result.local(); - result.tz(ConfigurationService.timezone()); - } - - return result; + return this.parseDatetime(datetime, 'YYYY-MM-DDTHH:mm:ssZ'); }, parseISODate: function(date) { @@ -142,13 +134,13 @@ module.exports = function(ConfigurationService, I18n) { }, isValidISODateTime: function(dateTime) { - return TimezoneService.isValid(dateTime, moment.ISO_8601); + return TimezoneService.isValid(dateTime, 'YYYY-MM-DDTHH:mm:ssZ'); }, isValid: function(date, dateFormat) { var format = dateFormat || (ConfigurationService.dateFormatPresent() ? ConfigurationService.dateFormat() : 'L'); - return moment(date, [format]).isValid(); + return moment(date, [format], true).isValid(); }, @@ -159,15 +151,6 @@ module.exports = function(ConfigurationService, I18n) { getTimeFormat: function() { return ConfigurationService.timeFormatPresent() ? ConfigurationService.timeFormat() : 'LT'; }, - - getTimezoneNG: function() { - var now = moment().utc(); - if (ConfigurationService.isTimezoneSet()) { - now.local(); - now.tz(ConfigurationService.timezone()); - } - return now.format('ZZ'); - } }; return TimezoneService; diff --git a/frontend/tests/unit/tests/components/input/transformers/transform-date-utc-test.js b/frontend/tests/unit/tests/components/input/transformers/transform-date-utc-test.js index b11f1b0b48..d724954856 100644 --- a/frontend/tests/unit/tests/components/input/transformers/transform-date-utc-test.js +++ b/frontend/tests/unit/tests/components/input/transformers/transform-date-utc-test.js @@ -29,13 +29,13 @@ describe('transformDateUtc Directive', function() { var compile, element, rootScope, scope, ConfigurationService, isTimezoneSetStub, timezoneStub; - var prepare = function (timeOfDay) { + var prepare = function () { angular.mock.module('openproject'); inject(function($rootScope, $compile, _ConfigurationService_) { var html = - ''; + ''; ConfigurationService = _ConfigurationService_; isTimezoneSetStub = sinon.stub(ConfigurationService, 'isTimezoneSet'); @@ -73,16 +73,16 @@ describe('transformDateUtc Directive', function() { var shouldBehaveCorrectlyWhenGivenInvalidDateValues = function () { describe('when given invalid date values', function () { - shouldBehaveLikeAParser('', undefined); - shouldBehaveLikeAParser('invalid', undefined); - shouldBehaveLikeAParser('2016-12', undefined); - shouldBehaveLikeAFormatter(undefined, ''); + shouldBehaveLikeAParser(null, null); + shouldBehaveLikeAParser('invalid', ''); + shouldBehaveLikeAParser('2016-12', ''); + shouldBehaveLikeAFormatter('', ''); shouldBehaveLikeAFormatter('invalid', ''); shouldBehaveLikeAFormatter('2016-12', ''); }); }; - context('should behave like begin-of-day with no time-of-day value given', function () { + context('when having UTC configured', function () { beforeEach(function () { prepare(); timezoneStub.returns('UTC'); @@ -90,7 +90,7 @@ describe('transformDateUtc Directive', function() { describe('when given valid date values', function() { var value = '2016-12-01'; - var expectedValue = value + 'T00:00:00+00:00'; + var expectedValue = value + 'T00:00:00Z'; shouldBehaveLikeAParser(value, expectedValue); shouldBehaveLikeAFormatter(expectedValue, value); }); @@ -98,95 +98,31 @@ describe('transformDateUtc Directive', function() { shouldBehaveCorrectlyWhenGivenInvalidDateValues(); }); - context('with begin-of-day', function () { + context('when having GMT+1 zone configured', function () { beforeEach(function () { - prepare('begin-of-day'); - timezoneStub.returns('UTC'); + prepare(); + // moment-timezone: GMT+1 is actually GMT-1 + timezoneStub.returns('Etc/GMT+1'); }); - describe('when given valid date values', function() { - var value = '2016-12-01'; - var expectedValue = value + 'T00:00:00+00:00'; - shouldBehaveLikeAParser(value, expectedValue); - shouldBehaveLikeAFormatter(expectedValue, value); + describe('it has the time adjusted', function () { + var value = '2016-12-03T00:00:00Z'; + var expectedValue = '2016-12-02'; + shouldBehaveLikeAFormatter(value, expectedValue); }); - - shouldBehaveCorrectlyWhenGivenInvalidDateValues(); }); - context('with end-of-day', function () { + context('when having GMT-1 zone configured', function () { beforeEach(function () { - prepare('end-of-day'); - timezoneStub.returns('UTC'); + prepare(); + // moment-timezone: GMT-1 is actually GMT+1 + timezoneStub.returns('Etc/GMT-1'); }); - describe('when given valid date values', function() { + describe('it has the time adjusted', function () { var value = '2016-12-01'; - var expectedValue = value + 'T23:59:59+00:00'; + var expectedValue = '2016-11-30T23:00:00Z'; shouldBehaveLikeAParser(value, expectedValue); - shouldBehaveLikeAFormatter(expectedValue, value); - }); - - shouldBehaveCorrectlyWhenGivenInvalidDateValues(); - }); - - context('when receiving datetime values from a different timezone', function () { - context('with begin-of-day', function () { - beforeEach(function () { - prepare('begin-of-day'); - // moment-timezone: GMT+1 is actually GMT-1 - timezoneStub.returns('Etc/GMT+1'); - }); - - describe('it should be transformed to the local timezone', function () { - var value = '2016-12-03T00:00:00+00:00'; - var expectedValue = '2016-12-02'; - shouldBehaveLikeAFormatter(value, expectedValue); - }); - }); - - context('with end-of-day', function () { - beforeEach(function () { - prepare('end-of-day'); - // moment-timezone: GMT-1 is actually GMT+1 - timezoneStub.returns('Etc/GMT-1'); - }); - - describe('it should be transformed to the local timezone', function () { - var value = '2016-12-01T23:59:59+00:00'; - var expectedValue = '2016-12-02'; - shouldBehaveLikeAFormatter(value, expectedValue); - }); - }); - }); - - context('when operating in a different timezone than UTC', function () { - context('with begin-of-day', function () { - beforeEach(function () { - prepare('begin-of-day'); - // moment-timezone: GMT-1 is actually GMT+1 - timezoneStub.returns('Etc/GMT-1'); - }); - - describe('it should have the expected timezone offset', function () { - var value = '2016-12-01'; - var expectedValue = value + 'T00:00:00+01:00'; - shouldBehaveLikeAParser(value, expectedValue); - }); - }); - - context('with end-of-day', function () { - beforeEach(function () { - prepare('end-of-day'); - // moment-timezone: GMT+1 is actually GMT-1 - timezoneStub.returns('Etc/GMT+1'); - }); - - describe('it should have the expected timezone offset', function () { - var value = '2016-12-01'; - var expectedValue = '2016-11-30T23:59:59-01:00'; - shouldBehaveLikeAParser(value, expectedValue); - }); }); }); }); diff --git a/frontend/tests/unit/tests/services/timezone-service-test.js b/frontend/tests/unit/tests/services/timezone-service-test.js index f9b727b6e0..21c114d2de 100644 --- a/frontend/tests/unit/tests/services/timezone-service-test.js +++ b/frontend/tests/unit/tests/services/timezone-service-test.js @@ -82,40 +82,4 @@ describe('TimezoneService', function() { expect(time.format('HH:mm')).to.eq('00:00'); }); }); - - // describe('#parseLocalDate', function() { - // it('has UTC time zone', function() { - // var time = TimezoneService.parseLocalDate(DATE, 'YYYY-MM-DD'); - // expect(time.utcOffset()).to.equal(0); - // }); - // - // it('has no time information', function() { - // var time = TimezoneService.parseLocalDate(DATE, 'YYYY-MM-DD'); - // expect(time.format('HH:mm')).to.eq('00:00'); - // }); - // }); - // - // describe('#parseISODatetime', function() { - // it('has UTC time zone', function() { - // var time = TimezoneService.parseISODatetime(DATETIME); - // expect(time.utcOffset()).to.equal(0); - // }); - // - // it('has no time information', function() { - // var time = TimezoneService.parseLocalDate(DATE, 'YYYY-MM-DD'); - // expect(time.format('HH:mm')).to.eq('00:00'); - // }); - // }); - // - // describe('#getTimezoneNG', function() { - // it('has UTC time zone', function() { - // var time = TimezoneService.parseLocalDate(DATE, 'YYYY-MM-DD'); - // expect(time.utcOffset()).to.equal(0); - // }); - // - // it('has no time information', function() { - // var time = TimezoneService.parseLocalDate(DATE, 'YYYY-MM-DD'); - // expect(time.format('HH:mm')).to.eq('00:00'); - // }); - // }); });