-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from MatthewTLackey/v2-beta
V2 -> master
- Loading branch information
Showing
8 changed files
with
1,066 additions
and
226 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
module.exports = (grunt) => { | ||
grunt.initConfig({ | ||
eslint: { | ||
target: ['**/*.js', '!node_modules/**'] | ||
}, | ||
mochaTest: { | ||
test: { | ||
options: { | ||
reporter: 'nyan' | ||
}, | ||
src: ['test/**/*.js'] | ||
} | ||
}, | ||
watch: { | ||
config: { | ||
files: './Gruntfile.js', | ||
options: { | ||
reload: true | ||
} | ||
}, | ||
tests: { | ||
files: ['test/**.js', '!node_modules/**', './moment-msdate.js'], | ||
tasks: ['test'], | ||
options: { | ||
spawn: true | ||
} | ||
} | ||
} | ||
}); | ||
|
||
grunt.loadNpmTasks('grunt-mocha-test'); | ||
grunt.loadNpmTasks('grunt-eslint'); | ||
grunt.loadNpmTasks('grunt-contrib-watch'); | ||
|
||
grunt.registerTask('test', ['eslint', 'mochaTest']); | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,97 @@ | ||
'use strict'; | ||
|
||
(function(root, factory) { | ||
|
||
/*global define*/ | ||
if (typeof define === 'function' && define.amd) { | ||
define(['moment-timezone'], factory); | ||
define(['moment', 'moment-timezone'], factory); // AMD | ||
} else if (typeof module === 'object' && module.exports) { | ||
module.exports = factory(require('moment-timezone')); | ||
module.exports = factory(require('moment'), require('moment-timezone')); // Node | ||
} else { | ||
factory(root.moment); | ||
factory(root.moment, root.moment.tz); // Browser | ||
} | ||
|
||
}(this, function(moment) { | ||
|
||
/** | ||
* Constants | ||
*/ | ||
}(this, function(moment, momentTimezone) { | ||
var MINUTE_MILLISECONDS = 60 * 1000; | ||
var DAY_MILLISECONDS = 86400000; | ||
var MS_DAY_OFFSET = 25569; | ||
|
||
const momentVersion = moment.version.split('.'); | ||
const major = +momentVersion[0]; | ||
const minor = +momentVersion[1]; | ||
|
||
if (major < 2 || (major === 2 && minor < 6)) { | ||
throw new Error(`moment-msdate requires Moment.js >= 2.6.0. You are using Moment.js ${moment.version}. See momentjs.com`); | ||
} | ||
|
||
if (!momentTimezone || !moment.tz) { | ||
throw new Error('moment-msdate requires moment-timezone.js. see momentjs.com/timezone'); | ||
} | ||
|
||
const oaDateToTicks = function(oaDate) { | ||
return ((oaDate - MS_DAY_OFFSET) * DAY_MILLISECONDS) + (oaDate >= 0.0 ? 0.5 : -0.5); | ||
}; | ||
|
||
const ticksToOADate = function(ticks) { | ||
return (ticks / DAY_MILLISECONDS) + MS_DAY_OFFSET; | ||
}; | ||
|
||
/** | ||
* @description To JavaScript date from OLE Automation date | ||
* @description takes an oaDate that is not in utc and converts it to a utc moment offset by a number of minutes | ||
* | ||
* @param msDate An OLE Automation date. Required. | ||
* @param offsetToUtcInMinutes An offset from the msDate to UTC. If not supplied the offset from the system timezone will be used. | ||
* @param {double} oaDate | ||
* @param {string} offsetToUtcInMinutes | ||
* @returns moment | ||
*/ | ||
moment.fromOADate = function(msDate, offsetToUtcInMinutes) { | ||
var jO = new Date(((msDate - MS_DAY_OFFSET) * DAY_MILLISECONDS) + (msDate >= 0.0 ? 0.5 : -0.5)); | ||
var tz = isNaN(parseInt(offsetToUtcInMinutes, 10)) ? jO.getTimezoneOffset() : offsetToUtcInMinutes; | ||
jO = new Date((((msDate - MS_DAY_OFFSET) + (tz / (60 * 24))) * DAY_MILLISECONDS) + (msDate >= 0.0 ? 0.5 : -0.5)); | ||
return moment(jO); | ||
const fromOADateOffsetToUtcByMinutes = function(oaDate, offsetToUtcInMinutes) { | ||
const offsetInTicks = offsetToUtcInMinutes * MINUTE_MILLISECONDS; | ||
const ticks = oaDateToTicks(oaDate); | ||
return moment(ticks + offsetInTicks).utc(); | ||
}; | ||
|
||
/** | ||
* To OLE Automation date from JavaScript date | ||
* @description takes an oaDate that is not in utc and converts it to a utc moment offset by the specified timezone | ||
* | ||
* @param jsDate A JavaScript date object to convert to OA Date. Defaults to existing moment instance or new Date | ||
* @returns Floating-point number, e.g., 41502.558798240745 | ||
* @param {double} oaDate | ||
* @param {string} timezone | ||
* @returns moment | ||
*/ | ||
moment.fn.toOADate = function(jsDateInput) { | ||
var jsDate = jsDateInput || this._d || new Date(); | ||
var timezoneOffset = jsDate.getTimezoneOffset() / (60 * 24); | ||
var msDateObj = (jsDate.getTime() / DAY_MILLISECONDS) + (MS_DAY_OFFSET - timezoneOffset); | ||
return msDateObj; | ||
const fromOADateOffsetToUtcByTimezone = function(oaDate, timezone) { | ||
if (!moment.tz.zone(timezone)) { throw new Error('timezone provided is not available in moment-timezone.js', 'moment-msdate.js', 59); } | ||
const ticks = oaDateToTicks(oaDate); | ||
const offset = moment(ticks).tz(timezone).utcOffset() * MINUTE_MILLISECONDS; | ||
return moment.tz(ticks - offset, timezone).utc(); | ||
}; | ||
|
||
moment.updateLocale('en', { | ||
invalidDate: undefined | ||
}); | ||
|
||
/** | ||
* Converts an OLE Automation date to a moment (baking in a timezone if one is supplied) | ||
* Returns a UTC Moment object instance | ||
*/ | ||
moment.fromOADateWithZone = function(msDate, timeZone) { | ||
if (timeZone) { | ||
return moment.fromOADate(msDate).tz(timeZone); | ||
} | ||
* @description takes an oaDate that is in utc and converts it to a utc moment or takes an oaDate and an offset to utc and converts it to a utc moment. The offset can be an int representing the offset to utc in minutes or a string indicating the timezone of the oaDate. | ||
* | ||
* @param {double} oaDate | ||
* @param {string=} {int=} offset | ||
* @returns moment | ||
*/ | ||
moment.fromOADate = function(oaDate, offset) { | ||
if (isNaN(parseInt(oaDate, 10))) { throw new TypeError('fromOADate requires an oaDate that is not null or undefined', 'moment-msdate.js', 72); } | ||
|
||
return moment.fromOADate(msDate); | ||
/* no offset */ | ||
if (!offset) { return fromOADateOffsetToUtcByMinutes(oaDate, 0); } | ||
|
||
/* timezone */ | ||
const parsedOffset = parseInt(offset, 10); | ||
if (isNaN(parsedOffset)) { return fromOADateOffsetToUtcByTimezone(oaDate, offset); } | ||
|
||
/* minutes */ | ||
return fromOADateOffsetToUtcByMinutes(oaDate, parsedOffset); | ||
}; | ||
|
||
/** | ||
* Converts a moment (with timezone) to an OLE Automation date in UTC | ||
* Returns an OLE Automation date in the form of a double | ||
*/ | ||
moment.fn.toOADateWithZone = function() { | ||
var nMsDate = (this.valueOf() / DAY_MILLISECONDS) + MS_DAY_OFFSET; | ||
return nMsDate; | ||
* @description converts a moment to a UTC OLE automation date represented as a double | ||
* | ||
* @returns {double} | ||
*/ | ||
moment.fn.toOADate = function() { | ||
const milliseconds = this.valueOf(); | ||
return ticksToOADate(milliseconds); | ||
}; | ||
|
||
|
||
return moment; | ||
})); |
Oops, something went wrong.