Skip to content

v4.0.0

Compare
Choose a tag to compare
@doniseferi doniseferi released this 06 Jan 20:50
· 61 commits to master since this release

Api Changes

  • None

Changes

  • Javascript module type is now UMD
  • Bundled and minified into one file named suntimes.js
  • suntimes.js file minified to 9Kb with all comments kept
  • All comments kept

Test Results

TOC

The correct date time utc of all expected points of a date are returned with a +/- 60 second accuracy.

[testModel(new Date(2019, 0, 1), _utcJanNoon["default"], noonWrapper), testModel(new Date(2019, 6, 1), _utcJulyNoon["default"], noonWrapper), testModel(new Date(2019, 0, 1), _utcJanSunrise["default"], _index.getSunriseDateTimeUtc), testModel(new Date(2019, 6, 1), _utcJulySunrise["default"], _index.getSunriseDateTimeUtc), testModel(new Date(2019, 0, 1), _utcJanSunset["default"], _index.getSunsetDateTimeUtc), testModel(new Date(2019, 6, 1), _utcJulySunset["default"], _index.getSunsetDateTimeUtc), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getAstronomicalDawnStartDateTimeUtc, 'ExpectedAstronomicalDawnUtc'), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getNauticalDawnStartDateTimeUtc, 'ExpectedNauticalDawnUtc'), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getCivilDawnStartDateTimeUtc, 'ExpectedCivilDawnUtc'), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getAstronomicalDuskStartDateTimeUtc, 'ExpectedAstronomicalDuskUtc'), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getNauticalDuskStartDateTimeUtc, 'ExpectedNauticalDuskUtc'), testModel(new Date(2022, 0, 1), _utcJanTwilight["default"], _index.getCivilDuskStartDateTimeUtc, 'ExpectedCivilDuskUtc')].forEach(function (testModel) {
  return assertTimesAreCloseToDelta(testModel);
});

Scientific decimal time to time

returns hours.

_chai.assert.equal((0, _index.toTime)(1).hours, 1);
_chai.assert.equal((0, _index.toTime)(10).hours, 10);
_chai.assert.equal((0, _index.toTime)(100).hours, 100);
_chai.assert.equal((0, _index.toTime)(100.9).hours, 100);
_chai.assert.equal((0, _index.toTime)(10.9).hours, 10);
_chai.assert.equal((0, _index.toTime)(1.9).hours, 1);

returns minutes.

_chai.assert.equal((0, _index.toTime)(0.0166667).minutes, 1);
_chai.assert.equal((0, _index.toTime)(0.1666667).minutes, 10);
_chai.assert.equal((0, _index.toTime)(0.1666667).minutes, 10);
_chai.assert.equal((0, _index.toTime)(0.5).minutes, 30);
_chai.assert.equal((0, _index.toTime)(0.983335).minutes, 59);

returns seconds.

_chai.assert.equal((0, _index.toTime)(0.000277778).seconds, 1);
_chai.assert.equal((0, _index.toTime)(0.00277778).seconds, 10);
_chai.assert.equal((0, _index.toTime)(0.008333361).seconds, 30);
_chai.assert.equal((0, _index.toTime)(0.0138889).seconds, 50);
_chai.assert.equal((0, _index.toTime)(0.0163889).seconds, 59);
_chai.assert.equal((0, _index.toTime)(0.0166667).seconds, 0);

returns milliseconds.

_chai.assert.equal((0, _index.toTime)(0.0002775).milliseconds, 999);
_chai.assert.equal((0, _index.toTime)(0.000100278).milliseconds, 361);
_chai.assert.equal((0, _index.toTime)(0.000277778).milliseconds, 0);
_chai.assert.equal((0, _index.toTime)(0.000234167).milliseconds, 843);

returns the complete every time unit.

var decimalTime = 23.756;
var actual = (0, _index.toTime)(decimalTime);
_chai.assert.equal(actual.hours, 23);
_chai.assert.equal(actual.minutes, 45);
_chai.assert.equal(actual.seconds, 21);
_chai.assert.equal(actual.milliseconds, 600);

throws an exception when the value is null, undefined or not a number.

_chai.assert["throws"](function () {
  return (0, _index.toTime)(null);
});
_chai.assert["throws"](function () {
  return (0, _index.toTime)(undefined);
});
_chai.assert["throws"](function () {
  return (0, _index.toTime)('One');
});
_chai.assert["throws"](function () {
  return (0, _index.toTime)('');
});

Get all dates for a year

returns every distinct date for a non leap year.

var expectedYear = 2001;
var allDates = (0, _dateTestUtilities["default"])(expectedYear);
var distinctDates = allDates.map(function (s) {
  return s.toString();
}).filter(function (s, i, a) {
  return a.indexOf(s) === i;
}).map(function (s) {
  return new Date(s);
});
var uniqueYears = _toConsumableArray(new Set(allDates.map(function (x) {
  return x.getFullYear();
})));
var expectedNumberOfUniqueYears = 1;
_chai.assert.equal(allDates.length, distinctDates.length);
_chai.assert.equal(uniqueYears.length, expectedNumberOfUniqueYears);
_chai.assert.equal(uniqueYears[0], expectedYear);

returns every distinct date for a leap year.

var expectedYear = 2000;
var allDates = (0, _dateTestUtilities["default"])(expectedYear);
var distinctDates = allDates.map(function (s) {
  return s.toString();
}).filter(function (s, i, a) {
  return a.indexOf(s) === i;
}).map(function (s) {
  return new Date(s);
});
var uniqueYears = _toConsumableArray(new Set(allDates.map(function (x) {
  return x.getFullYear();
})));
var expectedNumberOfUniqueYears = 1;
_chai.assert.equal(allDates.length, distinctDates.length);
_chai.assert.equal(uniqueYears.length, expectedNumberOfUniqueYears);
_chai.assert.equal(uniqueYears[0], expectedYear);

Get day of the year

returns the correct day of the year.

_getDayOfTheYearTestData["default"].forEach(function (data) {
  var date = data.date,
      expectedDayOfTheYear = data.expectedDayOfTheYear;
  var actualDayOfTheYearResult = (0, _index.getDayOfTheYear)(new Date(date));
  _chai.assert.equal(actualDayOfTheYearResult, expectedDayOfTheYear);
});

returns day of the year for every day on a leap year.

var allDaysOnALeapYear = (0, _dateTestUtilities["default"])(1944);
allDaysOnALeapYear.forEach(function (date) {
  var expectedDayOfTheYear = allDaysOnALeapYear.indexOf(date) + 1;
  var actualDayOfTheYear = (0, _index.getDayOfTheYear)(date);
  _chai.assert.equal(actualDayOfTheYear, expectedDayOfTheYear);
});

returns day of the year for every day on non leap year.

var allDaysOnALeapYear = (0, _dateTestUtilities["default"])(2017);
allDaysOnALeapYear.forEach(function (date) {
  var expectedDayOfTheYear = allDaysOnALeapYear.indexOf(date) + 1;
  var actualDayOfTheYear = (0, _index.getDayOfTheYear)(date);
  _chai.assert.equal(actualDayOfTheYear, expectedDayOfTheYear);
});

throws exception when an invalid date is passed.

_chai.assert["throw"](function () {
  return (0, _index.getDayOfTheYear)(undefined);
}, 'Please provide a valid date');
_chai.assert["throw"](function () {
  return (0, _index.getDayOfTheYear)(null);
}, 'Please provide a valid date');
_chai.assert["throw"](function () {
  return (0, _index.getDayOfTheYear)();
}, 'Please provide a valid date');

Declination of the sun

returns the correct value in degrees within +/- 0.4 degree accuracy.

_declinationOfTheSunTestData["default"].forEach(function (data) {
  var date = data.date,
      declination = data.declination;
  var result = (0, _index["default"])(new Date(date));
  var difference = result - declination;
  _chai.assert.closeTo(difference, 0, 0.4);
});

throws an error if an invalid date is passed in.

var expectedErrorMessage = 'Please provide a valid date';
_chai.assert["throws"](function () {
  return (0, _index["default"])(null);
}, expectedErrorMessage);
_chai.assert["throws"](function () {
  return (0, _index["default"])(undefined);
}, expectedErrorMessage);
_chai.assert["throws"](function () {
  return (0, _index["default"])();
}, expectedErrorMessage);

Declination Of The Sun Factory

throws an exception if you do not pass in all parameters.

var stub = function stub() {
  return 1;
};
_chai.assert["throws"](function () {
  return (0, _index.declinationOfTheSunFactory)(null, stub, stub, stub);
}, 'Please provide a function for sine.');
_chai.assert["throws"](function () {
  return (0, _index.declinationOfTheSunFactory)(stub, null, stub, stub);
}, 'Please provide a function for cosine.');
_chai.assert["throws"](function () {
  return (0, _index.declinationOfTheSunFactory)(stub, stub, null, stub);
}, 'Please provide a function for arcsine.');
_chai.assert["throws"](function () {
  return (0, _index.declinationOfTheSunFactory)(stub, stub, stub, null);
}, 'Please provide a function for getDayOfTheYear.');

returns a get declination of the sun function.

var stub = function stub() {
  return 1;
};
_chai.assert.typeOf(function () {
  return (0, _index.declinationOfTheSunFactory)(stub, stub, stub, stub).getDeclinationOfTheSun;
}, 'function');

Equation of time

returns the correct equation of time with a margin of error of +/- 4 seconds.

_equationOfTimeTestData["default"].forEach(function (expectedResult) {
  var eot = expectedResult.eot,
      date = expectedResult.date;
  var actual = (0, _index.equationOfTime)(new Date(date));
  var difference = eot * 60 - actual * 60;
  _chai.assert.closeTo(difference, 0, 3.99);
});

throws an exception when an invalid date is passed in.

var expectedErrorMessage = 'Please provide a valid date';
_chai.assert["throws"](function () {
  return (0, _index.equationOfTime)(null);
}, expectedErrorMessage);
_chai.assert["throws"](function () {
  return (0, _index.equationOfTime)(undefined);
}, expectedErrorMessage);
_chai.assert["throws"](function () {
  return (0, _index.equationOfTime)();
}, expectedErrorMessage);

Index

exports the suntimes object.

(0, _chai.expect)(_index["default"]).to.not.equal(null).and.not.to.equal(undefined);

the suntimes object exports all methods.

var _lat$lng$date$angle = {
  lat: 0,
  lng: 0,
  date: new Date(Date.now()),
  angle: 0
},
    lat = _lat$lng$date$angle.lat,
    lng = _lat$lng$date$angle.lng,
    date = _lat$lng$date$angle.date,
    angle = _lat$lng$date$angle.angle;
(0, _chai.expect)(function () {
  return _index["default"].getDeclinationOfTheSun(date);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].equationOfTime(date);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getDayOfTheYear(date);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getHourAngleSinceNoon(date, lat, angle);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNoonHourAngle(date, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNoonDateTimeUtc(date, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getDateTimeUtcOfAngleBeforeNoon(angle, date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getDateTimeUtcOfAngleAfterNoon(angle, date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getSunriseDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getSunsetDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getCivilDawnEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getCivilDawnStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNauticalDawnEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNauticalDawnStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getAstronomicalDawnEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getAstronomicalDawnStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getAstronomicalDuskStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getAstronomicalDuskEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNauticalDuskEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getNauticalDuskStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getCivilDuskEndDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();
(0, _chai.expect)(function () {
  return _index["default"].getCivilDuskStartDateTimeUtc(date, lat, lng);
}).to.not.be["null"].and.not["throw"]();

Get Noon Time Utcs

East most location with a 12 hour offset returns correct noon datetime in utc.

var targetDate = new Date(2019, 9, 1);
var anadyrRussia = {
  Latitude: 64.732857,
  Longitude: 177.507812,
  ExpectedTimeUtc: '2019-09-30T23:59:00.000Z'
};
var actual = (0, _index.getNoonDateTimeUtc)(targetDate, anadyrRussia.Longitude);
var expectedTime = new Date(anadyrRussia.ExpectedTimeUtc);
var differenceInSeconds = (expectedTime.getTime() - actual.getTime()) / 1000;
_chai.assert.closeTo(differenceInSeconds, 0, 60);

West most location with a -12 hour offset returns correct noon datetime in utc.

var targetDate = new Date(2019, 0, 1);
var bakerIslandUsa = {
  Latitude: 0.193627,
  Longitude: -176.476913,
  ExpectedTimeUtc: '2019-01-01T23:49:00.000Z'
};
var actual = (0, _index.getNoonDateTimeUtc)(targetDate, bakerIslandUsa.Longitude);
var expectedTime = new Date(bakerIslandUsa.ExpectedTimeUtc);
var differenceInSeconds = (expectedTime.getTime() - actual.getTime()) / 1000;
_chai.assert.closeTo(differenceInSeconds, 0, 60);

Trigonometry objects

sine is equal to built in Math objects sin method.

var result = (0, _index.sine)(1);
var expected = Math.sin(_1DegInRadians);
_chai.assert.equal(result, expected);

cosine is equal to built in Math objects cos method.

var result = (0, _index.cosine)(1);
var expected = Math.cos(_1DegInRadians);
_chai.assert.equal(result, expected);

arcsine is equal to built in Math objects asin method.

var result = (0, _index.arcsine)(_1DegInRadians);
var resultInRadians = (0, _index.degreesToRadians)(result);
var expected = Math.asin(_1DegInRadians);
_chai.assert.equal(resultInRadians, expected);

arccosine is equal to built in Math objects asin method.

var result = (0, _index.arccosine)(_1DegInRadians);
var resultInRadians = (0, _index.degreesToRadians)(result);
var expected = Math.acos(_1DegInRadians);
_chai.assert.equal(resultInRadians, expected);

Angle conversions

converts 1 degree to 0.01745rad.

_chai.assert.equal((0, _index.degreesToRadians)(1), _1DegInRadians);

converts 1 radian to 57.296deg.

_chai.assert.equal((0, _index.radiansToDegrees)(1), _1RadInDegrees);