Created
September 19, 2013 10:23
-
-
Save uris77/6621592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert an instance of Moment to the specified timeZone: | |
# date = new moment(new Date()) | |
# date.forTimeZone(moment.central) | |
moment.fn.forTimeZone = (timeZone) -> | |
currentOffset = (this.zone() * 60) * -1 | |
if this.isDST() | |
timeZoneOffset = timeZone.utcTotalOffset | |
else | |
timeZoneOffset = timeZone.utcOffset | |
if currentOffset > timeZoneOffset | |
adjustedOffset = timeZoneOffset - currentOffset | |
else | |
adjustedOffset = currentOffset - timeZoneOffset | |
newMoment = this.clone().add 'seconds', adjustedOffset | |
return newMoment | |
# Settings for Central Time Zone | |
moment.central = { | |
friendlyIdentifier: "America - Chicago", | |
identifier: "America/Chicago", | |
name: "Central Time (US & Canada)", | |
utcOffset: -21600, | |
utcTotalOffset: -18000 | |
} | |
test('should format pacific time to central', -> | |
pacific_time = moment.utc("2013 10 30 12:00:00 -0800", "YYYY MM DD HH:mm:ss ZZ") | |
sinon.stub(pacific_time, 'isDST').returns(false) | |
equal(pacific_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 14:00:00') | |
) | |
test('should format eastern time to central', -> | |
eastern_time = moment.utc("2013 10 30 12:00:00 -0500", "YYYY MM DD HH:mm:ss ZZ") | |
sinon.stub(eastern_time, 'isDST').returns(false) | |
equal(eastern_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 11:00:00') | |
) | |
test('should return same time for central', -> | |
central_time = moment('2013 10 30 12:00:00 -0600', 'YYYY MM DD HH:mm:ss ZZ') | |
sinon.stub(central_time, 'isDST').returns(false) | |
equal(central_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 12:00:00') | |
) | |
test('should format utc +800 to central time', -> | |
utc0800 = moment('2013 10 30 12:00:00 +0800', 'YYYY MM DD HH:mm:ss ZZ') | |
sinon.stub(utc0800, 'isDST').returns(false) | |
equal(utc0800.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '29 Oct 2013 22:00:00') | |
) | |
test('should format utc0845 to central time', -> | |
utc0845 = moment('2013 10 30 12:00:00 +0845', 'YYYY MM DD HH:mm:ss ZZ') | |
sinon.stub(utc0845, 'isDST').returns(false) | |
equal(utc0845.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '29 Oct 2013 21:15:00') | |
) | |
test('should format PST to central time DST', -> | |
pacific_time = moment.utc("2013 10 30 12:00:00 -0800", "YYYY MM DD HH:mm:ss ZZ") | |
sinon.stub(pacific_time, 'isDST').returns(true) | |
equal(pacific_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 15:00:00') | |
) | |
test('should format CST to central time DST', -> | |
central_time = moment.utc("2013 10 30 12:00:00 -0600", "YYYY MM DD HH:mm:ss ZZ") | |
sinon.stub(central_time, 'isDST').returns(true) | |
equal(central_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 13:00:00') | |
) | |
test('should format EST to central time DST', -> | |
eastern_time = moment.utc("2013 10 30 12:00:00 -0500", "YYYY MM DD HH:mm:ss ZZ") | |
sinon.stub(eastern_time, 'isDST').returns(true) | |
equal(eastern_time.forTimeZone(moment.central).format('DD MMM YYYY HH:mm:ss'), '30 Oct 2013 12:00:00') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment