Skip to content

Commit

Permalink
Split the functionality even furthar into dateToTimezone and
Browse files Browse the repository at this point in the history
applyTzOffset
  • Loading branch information
shepherdsam committed Feb 14, 2017
1 parent 8092b5f commit 79cc1b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,25 +546,24 @@ 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;
}

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.
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 79cc1b1

Please sign in to comment.