From 79cc1b155e22be15132b3fdebcb76ad8aadbb3eb Mon Sep 17 00:00:00 2001 From: Sam Shepherd Date: Tue, 14 Feb 2017 12:30:33 +0000 Subject: [PATCH] Split the functionality even furthar into dateToTimezone and applyTzOffset --- lib/index.js | 25 ++++++++++++++++++++----- spec/specs.js | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index edf4321..76ee506 100644 --- a/lib/index.js +++ b/lib/index.js @@ -546,7 +546,7 @@ Utils.parseDateTimeField = function(field, options) { var parsedDateWithTimezone = parseDateTime(field, options, timezone); if (SugarDate.isValid(parsedDateWithTimezone)) { var hasTZOffset = Utils.hasTZOffset(field); - var dateMoment = Utils.applyTzOffset(parsedDateWithTimezone, timezone, hasTZOffset); + var dateMoment = Utils.dateToTimezone(parsedDateWithTimezone, timezone, hasTZOffset); // Add the dateMoment to the return object rtnObject.moment = dateMoment; } @@ -554,17 +554,16 @@ Utils.parseDateTimeField = function(field, options) { return rtnObject; }; - /** - * Utils.applyTzOffset - create a Moment.js date in a timezone + * Utils.dateToTimezone - create a Moment.js date in a timezone * * @param {Date} date The date to create in the timezone * @param {String} timezone Timezone to create the date into ex, 'America/New_York' * @param {Boolean} hasTZOffset Specify if the incoming date had a offset already applied. * @return {Moment} A moment.js object */ -Utils.applyTzOffset = function(date, timezone, hasTZOffset) { - Utils.Logger.debug('Utils.applyTzOffset: date:', date.toISOString()); +Utils.dateToTimezone = function(date, timezone, hasTZOffset) { + Utils.Logger.debug('Utils.dateToTimezone: date:', date.toISOString()); // Convert to string. Important to remove offset/tz info (if none was provided originally) // or moment will ignore the passed in tz in later step. @@ -579,6 +578,22 @@ Utils.applyTzOffset = function(date, timezone, hasTZOffset) { return dateMoment; }; +/** + * Utils.applyTzOffset - Applies TZ Offset + * + * @param {Date} date The date to offset + * @param {String} timezone Timezone to offset the date for ex, 'America/New_York' + * @return {Date} A date with the offset applied + */ +Utils.applyTzOffset = function(date, timezone) { + Utils.Logger.debug('Utils.applyTzOffset: before:', date); + + SugarDate.addMinutes(date, moment.tz.zone(timezone).offset(date)); + + Utils.Logger.debug('Utils.applyTzOffset: after:', date); + return date; +}; + /** * Utils.hasTZOffset - Checks to see if the string has a timezone offset * Examples diff --git a/spec/specs.js b/spec/specs.js index bd16fec..311cc2d 100644 --- a/spec/specs.js +++ b/spec/specs.js @@ -1169,6 +1169,27 @@ describe('Utils', function() { }); }); + describe('Apply Timezone Offset', function() { + it('should return a date in the correct offset', function() { + var d; + + d = Utils.applyTzOffset(new Date('2017-02-14'), 'America/Chicago'); + expect(d).toEqual(moment.tz('2017-02-14', 'America/Chicago').toDate()); + expect(d.toISOString()).toEqual('2017-02-14T06:00:00.000Z'); + + d = Utils.applyTzOffset(new Date('2017-02-15'), 'America/New_York'); + expect(d).toEqual(moment.tz('2017-02-15', 'America/New_York').toDate()); + expect(d.toISOString()).toEqual('2017-02-15T05:00:00.000Z'); + + d = Utils.applyTzOffset(new Date('2017-02-16'), 'Europe/London'); + expect(d).toEqual(moment.tz('2017-02-16', 'Europe/London').toDate()); + expect(d.toISOString()).toEqual('2017-02-16T00:00:00.000Z'); + + d = Utils.applyTzOffset(new Date(), 'Europe/London'); + expect(d).toEqual(moment().tz('Europe/London').toDate()); + }); + }); + describe('Parse Boolean Field', function() { var expectTrue = function(input) { expect(Utils.parseBooleanField(input)).toEqual({