Skip to content

Commit

Permalink
Merge pull request #6 from michael-lechner/feature/updating-from-inte…
Browse files Browse the repository at this point in the history
…rnal-repository

feature/updating from internal repository
  • Loading branch information
brianbaker authored Feb 2, 2017
2 parents 9ff3b55 + 8da3534 commit 7b8a62b
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.settings/**
node_modules
29 changes: 22 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
{
"env": {
"mocha": true,
"node": true
},
"extends": [
"airbnb"
],
"rules": {
"camelcase": [1, { "properties": "never" }],
"eol-last": 0,
"quotes": [2, "single"]
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"object-shorthand": 0,
"spaced-comment": 0,
"no-invalid-this": 0,
"linebreak-style": 0,
"no-tabs": 0,
"padded-blocks": 0,
"indent": [2, "tab", { "SwitchCase": 1 }],
"strict": 0,
"func-names": 0,
"space-before-function-paren": [2, { "anonymous": "never", "named": "never" }],
"global-require": 0,
"max-len": [0, 100, 4, {
"ignoreUrls": true,
"ignoreComments": true
}],
"comma-dangle": [2, "never"],
"no-underscore-dangle": 0,
"no-empty-function": 1
}
}
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.DS_Store
node_modules
node_modules

#user preferences
.vscode

# Logs
logs
*.log
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ For exact date _and_ time (time is the value right of the decimal):

`moment.fromOADate(41493.706892280097000)` returns `Wed Aug 07 2013 16:57:55 GMT-0600 (MDT)`

By default moment.fromOADate() uses the server time as the offset to UTC a second argument can be provided that indicates the offset of the OA date to UTC in minutes.

`moment.fromOADate(42754.835023148145, 360)` returns `Fri Jan 20 2017 02:02:25 GMT+0000 (UTC)`

For Moment formatting:

```
Expand All @@ -48,6 +52,16 @@ This could easily be chained together as:

**Note**: OLE Automation dates are unspecified, meaning they’re based on the local timezone by default.

### fromOADateWithZone()
Converts an OLE Automation date to a moment (baking in a timezone if one is supplied) and returns a UTC Moment object instance.

`moment.fromOADateWithZone('42754.835023148145', 'America/Denver');` returns `Fri Jan 20 2017 03:02:25 GMT+0000 (UTC)`

### toOADateWithZone()
Converts a moment (with timezone) to an OLE Automation date in UTC.

`moment('2017-01-19T20:02:26.000Z').toOADateWithZone()` returns `42754.835023148145`

## License

Copyright © 2014 Markit On Demand, Inc.
Expand Down
62 changes: 49 additions & 13 deletions moment-msdate.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
'use strict';

(function() {

var moment = (typeof require !== 'undefined' && require !== null) && !require.amd ? require('moment') : this.moment;
/**
* Constants
*/
const DAY_MILLISECONDS = 86400000;
const MINUTE_MILLISECONDS = 60000;
const MS_DAY_OFFSET = 25569;

const moment = (typeof require !== 'undefined' && require !== null) && !require.amd ? require('moment-timezone') : this.moment;

/**
* To JavaScript date from OLE Automation date
* @description To JavaScript date from OLE Automation date
*
* @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.
* @returns moment
*/
moment.fromOADate = function(msDate) {
var jO = new Date( ((msDate - 25569) * 86400000) + (msDate >= 0.0 ? 0.5 : -0.5) );
var tz = jO.getTimezoneOffset();
var jO = new Date( ( (msDate-25569 + (tz / (60*24) ) ) * 86400000) + (msDate >= 0.0 ? 0.5 : -0.5) );
moment.fromOADate = function(msDate, offsetToUtcInMinutes) {
let jO = new Date(((msDate - MS_DAY_OFFSET) * DAY_MILLISECONDS) + (msDate >= 0.0 ? 0.5 : -0.5));
const 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);
};

Expand All @@ -21,15 +31,41 @@
* @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
*/
moment.fn.toOADate = function(jsDate){
var jsDate = jsDate || this._d || new Date();
var timezoneOffset = jsDate.getTimezoneOffset() / (60 * 24);
var msDateObj = ( jsDate.getTime() / 86400000 ) + (25569 - timezoneOffset);
moment.fn.toOADate = function(jsDateInput) {
const jsDate = jsDateInput || this._d || new Date();
const timezoneOffset = jsDate.getTimezoneOffset() / (60 * 24);
const msDateObj = (jsDate.getTime() / DAY_MILLISECONDS) + (MS_DAY_OFFSET - timezoneOffset);
return msDateObj;
}
};

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) {
const jsTicks = (msDate - MS_DAY_OFFSET) * DAY_MILLISECONDS;
const offset = moment.tz(timeZone).utcOffset() * MINUTE_MILLISECONDS;
if (timeZone) {
return moment.tz(jsTicks - offset, timeZone).utc();
}
return moment.utc(jsTicks);
};

/**
* 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() {
const nMsDate = (this.valueOf() / DAY_MILLISECONDS) + MS_DAY_OFFSET;
return nMsDate;
};

if ((typeof module !== 'undefined' && module !== null ? module.exports : void 0) != null) {
if ((typeof module !== 'undefined' && module !== null ? module.exports : undefined) !== null) {
module.exports = moment;
}

}).call(this);
}).call(this);
38 changes: 31 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "moment-msdate",
"version": "0.1.0",
"version": "0.2.0",
"description": "Adds OLE Automation parsing capabilities to Moment.js",
"main": "moment-msdate.js",
"scripts": {
"test": "mocha"
"lint": "eslint ./test ./moment-msdate.js",
"mocha": "mocha --check-leaks --reporter=spec ./test",
"test": "npm run lint && npm run mocha"
},
"repository": {
"type": "git",
Expand All @@ -15,18 +17,40 @@
"msdate",
"ole-automation"
],
"contributors": [
{
"name": "Mark Healey",
"email": ""
},
{
"name": "Brian Baker",
"email": "[email protected]"
},
{
"name": "Frank Martino",
"email": "[email protected]"
},
{
"name": "Michael Lechner",
"email": "[email protected]"
}
],
"author": "Markit On Demand",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/markitondemand/moment-msdate/issues"
},
"homepage": "https://github.com/markitondemand/moment-msdate#readme",
"dependencies": {
"moment": "^2.10.6"
"moment": "^2.17.1",
"moment-timezone": "^0.5.11"
},
"devDependencies": {
"eslint": "^1.1.0",
"mocha": "^2.2.5",
"mocha-eslint": "^1.0.0"
"eslint": "^3.13.0",
"eslint-config-airbnb": "^14.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^3.0.2",
"eslint-plugin-react": "^6.9.0",
"mocha": "^3.2.0"
}
}
}
9 changes: 9 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"mocha": true
},
"rules":{
// assert.throws doesnt respond to lambda syntax
"prefer-arrow-callback": 0
}
}
137 changes: 107 additions & 30 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,126 @@
'use strict';

var assert = require('assert'),
lint = require('mocha-eslint'),
moment = require('../moment-msdate');
const assert = require('assert');
const moment = require('../moment-msdate');

lint(['./*.js', './test/*.js']);
describe('moment-msdate: moment.fromOADate', () => {
it('should convert an OLE Automation date to a moment with a 0 offset to UTC', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADate(42754.835023148145, 0); // UTC
assert.equal('2017-01-19T20:02:26.000Z', date.toISOString());
});

it('should convert an OLE Automation date to a moment with a 300 minute offset to UTC', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADate(42754.835023148145, 300); // ET
assert.equal('2017-01-20T01:02:26.000Z', date.toISOString());
});

it('should convert an OLE Automation date to a moment with a 360 minute offset to UTC', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADate(42754.835023148145, 360); // CT
assert.equal('2017-01-20T02:02:26.000Z', date.toISOString());
});

it('should convert an OLE Automation date to a moment with a 420 minute offset to UTC', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADate(42754.835023148145, 420); // MT
assert.equal('2017-01-20T03:02:26.000Z', date.toISOString());
});
});

describe('moment-msdate', function() {
it('should parse an OLE Automation date int', function(done) {
var date = moment.fromOADate(41493);
describe('moment-msdate', () => {
it('should parse an OLE Automation date int', () => {
const date = moment.fromOADate(41493);
assert.equal(date.toString().search('Wed Aug 07 2013 00:00:00'), 0);
done();
});

it('should parse an OLE Automation date double', function(done) {
var date = moment.fromOADate(41493.706892280097000);
it('should parse an OLE Automation date double', () => {
const date = moment.fromOADate(41493.706892280097000);
assert.equal(date.toString().search('Wed Aug 07 2013 16:57:55'), 0);
done();
});

it('should handle rounding quirks', function(done) {
var date = moment.fromOADate(42681.501388888886);
it('should handle rounding quirks', () => {
const date = moment.fromOADate(42681.501388888886);
assert.equal(date.toString().search('Mon Nov 07 2016 12:02:00'), 0);
done();
});
});

it('should convert an empty JavaScript date to an OLE Automation date of 0', function(done) {
var date = new Date(1899, 11, 30, 0, 0, 0);
var oaDate = moment(date).toOADate();
describe('moment-msdate: moment.toOADate', () => {
it('return an OLE automation date from a jsDate input', () => {

});
});

describe('moment-msdate: moment.fn.toOADate', () => {
it('should convert an empty JavaScript date to an OLE Automation date of 0', () => {
const date = new Date(1899, 11, 30, 0, 0, 0);
const oaDate = moment(date).toOADate();
assert.equal(oaDate, 0);
done();
});

it('should convert a JavaScript date to an OLE Automation date int', function(done) {
var date = new Date(2012, 9, 15);
var oaDate = moment(date).toOADate();
it('should convert a JavaScript date to an OLE Automation date int', () => {
const date = new Date(2012, 9, 15);
const oaDate = moment(date).toOADate();
assert.equal(oaDate, 41197);
done();
});
});

describe('moment-msdate: moment.fromOADateWithZone', () => {
it('should convert an OLE automation date with an ET timezone to a utc moment', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADateWithZone('42754.835023148145', 'America/New_York');
assert.equal('2017-01-20T01:02:25.999Z', date.toISOString());
});

it('should convert an OLE automation date with a CT timezone to a utc moment', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADateWithZone('42754.835023148145', 'America/Chicago');
assert.equal('2017-01-20T02:02:25.999Z', date.toISOString());
});

it('should convert an OLE automation date with a MT timezone to a utc moment', () => {
// 1/19/2017 8:02:26 PM
const date = moment.fromOADateWithZone('42754.835023148145', 'America/Denver');
assert.equal('2017-01-20T03:02:25.999Z', date.toISOString());
});
});

describe('moment-msdate: moment.fn.toOADateWithZone', () => {
it('should convert a moment with a UTC offset to a UTC OLE automation date', () => {
const momentDate = moment.parseZone('2017-01-19T20:02:26.000Z');
const oaDate = momentDate.toOADateWithZone();
// 1/19/2017 8:02:26 PM
assert.equal(42754.835023148145, oaDate);
});

it('should convert a moment with a ET offset to a UTC OLE automation date', () => {
const momentDate = moment.parseZone('2017-01-19T20:02:26-05:00');
const oaDate = momentDate.toOADateWithZone();
assert.equal(42755.04335648148, oaDate);
// 1/20/2017 1:02:26 AM
});

it('should convert a moment with a CT offset to a UTC OLE automation date', () => {
const momentDate = moment.parseZone('2017-01-19T20:02:26-06:00');
const oaDate = momentDate.toOADateWithZone();
assert.equal(42755.085023148145, oaDate);
// 1/20/2017 2:02:26 AM
});

it('should convert a moment with a MT offset to a UTC OLE automation date', () => {
const momentDate = moment.parseZone('2017-01-19T20:02:26-07:00');
const oaDate = momentDate.toOADateWithZone();
assert.equal(42755.12668981482, oaDate);
// 1/20/2017 3:02:26 AM
});

it('should convert a moment to a UTC OLE automation date if timezone (tz) is set', () => {
const momentDate = moment('2017-01-19T20:02:26.000Z');
momentDate.tz('America/New_York');
const oaDate = momentDate.toOADateWithZone();
assert.equal(42754.835023148145, oaDate);
// 1/19/2017 8:02:26 PM
});
});

// not a real reliable way to do this because of js number precision:
// it('should convert a JavaScript date to an OLE Automation date double', function(done) {
// var date = new Date(2015, 7, 10, 8, 10, 10, 0);
// var oaDate = moment(date).toOADate();
// assert.equal(oaDate, 42226.3403935185);
// done();
// });
});

0 comments on commit 7b8a62b

Please sign in to comment.