Skip to content

Commit

Permalink
Release, add methods and convert back to Date()
Browse files Browse the repository at this point in the history
  • Loading branch information
Faraday committed Feb 7, 2018
1 parent d0c525c commit 81c5c01
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# npmignore

original
.gitignore
.git
64 changes: 55 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ I'm still working on this module. New methods and opportunities will soon appear

## Usage

Clone repository to you *node_modules* folder.
Clone repository to you *node_modules* folder or:

```sh
npm install darian-system --save-dev
```

```javascript
var ds = require('darian-system');
Expand All @@ -18,31 +22,73 @@ Class constructor can recive *Date()* object or date as arguments in syntax:
```javascript
new Darian_Date(year[, month[, day[, hour[, min[, sec[, timeZoneModifer]]]]]])
```
*timeZoneModifer* - for UTC+3 is *3*, UTC-5 - *-5*
```timeZoneModifer``` for UTC+3 -- ```3```, UTC-5 -- ```-5```
If the constructor receives an empty argument string, it will create an object for the current time.

```javascript
var marsTime = new ds.Darian_Date();
console.log(marsTime.getTime()); // Sol Martis, 17 Mesha 217, 17:18:57
marsTime.getDate(); // Sol Martis, 17 Mesha 217, 17:18:57
```

### Methods
### Methods of Darian_Date() object

- #### .getDate()

Return date in format *[dayOfWeek], [monthDay] [Month] [Year], [Hour]:[Min]:[Sec]*

```javascript
.getTime() // Sol Mercurii, 04 Gemini 217, 03:42:40
.getDate() // Sol Mercurii, 04 Gemini 217, 03:42:40
```
Outputs date in format *[dayOfWeek], [monthDay] [Month] [Year], [Hour]:[Min]:[Sec]*

- #### .getJSON()

Return date as object like:

```javascript
{
dispThisSol: [value], // Events on this sol
dispThisDay: [value], // Events on this day
mYear: [value], // Mars year
mMonth: [value], // Mars month number
mMonthName: [value], // Mars month name
mDay: [value], // Mars day in month
mJulianDay: [value], // Julian Date
mNumDay: [value], // Sol since beginning of the year
mSolName: [value], // Name of sol of the week
mSolarLongitude: [value], // Solar Longitude(degrees)
mHour: [value], // Hours
mMin: [value], // Minutes
mSec: [value] // Seconds
}
```

- #### .getDispThisSol()

Return events that occurred on this day of the sol(Mars year).

```javascript
.getDispThisSol() // On this sol in 197: Contact with Viking Orbiter 1 was lost after 1,469 sols in Mars orbit.
```
Outputs events that occurred on this day of the sol(Mars year).

- #### .getDispThisDay()

Return events that occurred on this day of the Earth year.

```javascript
.getDispThisDay() // On this day in 1999: Mars Polar Lander was launched.
```
Outputs events that occurred on this day of the Earth year.

### Functions

- #### convMarsToEarth()

Converts Martian Time to Earth Time. Recive Darian_Date() object or Darian_Date().getJSON() and returns Date() object of Earth Time.

```javascript
var earthTime = ds.convMarsToEarth(marsTime);
earthTime; // Wed Feb 07 2018 18:31:26 GMT+0300 (+03)
```

## Based on

* [MARTIAN DATE CALCULATOR](http://ops-alaska.com/time/gangale_converter/calendar_clock.htm) - Online converter developed by Alan Hensel and Thomas Gangale.
* [Martian Date Calculator](http://ops-alaska.com/time/gangale_converter/calendar_clock.htm) --- Online converter developed by Alan Hensel and Thomas Gangale.
154 changes: 145 additions & 9 deletions lib/darian_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ class RangeException extends Error {
case '05':
message = 'There are not that many days in this month on Earth.';
break;
case '06':
message = 'There are not that many months on Mars.';
break;
case '07':
message = 'There are not that many days in this month on Mars.';
break;
default:
message = 'Unknow Error.'
}
super(message);
this.name = "RangeException";
this.name = 'RangeException';

if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
Expand All @@ -45,6 +51,121 @@ class RangeException extends Error {
}
}

// Convert martian time to Date()
function convMarsToEarth(mDate) {
var solsSince;
var daysSince;
var partialDay;

// Range checking:
if (mDate['mMonth'] < 1 || mDate['mMonth'] > 24) {
throw new RangeException('06');
}

if (mDate['mDay'] < 1 || mDate['mDay'] > 28 || (mDate['mMonth'] % 6 == 0 && mDate['mDay'] == 28 && !(mDate['mMonth'] == 24 && isMartianLeapYear(mDate['mYear'])))) {
throw new RangeException('07');
}

// Convert Martian date to sols
var year = parseInt(mDate['mYear'], 10);
var month = parseInt(mDate['mMonth'], 10);
var day = parseInt(mDate['mDay'], 10);
var TestData = 3;

solsSince = day + ((month-1) * 28) - Math.floor((month-1)/6)
+ 668 * year
+ Math.floor(year / 2)
+ Math.floor((year-1) / 10)
- Math.floor((year-1) / 100)
+ Math.floor((year-1) / 1000);

var hour = parseInt(mDate['mHour'], 10)/24
var min = parseInt(mDate['mMin'], 10)/1440
var sec = parseInt(mDate['mSec'], 10)/86400;

if (!isNaN(hour)) solsSince += hour;
if (!isNaN(min)) solsSince += min;
if (!isNaN(sec)) solsSince += sec;

// Timezone for now "Airy"
// solsSince -=
// document.calc.mTZ.options[document.calc.mTZ.selectedIndex].value/360;

// Convert sols to days
daysSince = solsSince*MARS_TO_EARTH_DAYS + EPOCH_OFFSET + ROUND_UP_SECOND;

daysSince -= (new Date()).getTimezoneOffset()/1440;

// Convert back to date, and put it it form
// get the fractional part, to do the time later
partialDay = daysSince - Math.floor(daysSince);

// Convert days to Gregorian date:

var d = Math.floor(daysSince)+1;

var sCD = Math.floor(d/146097); // what 400 year span
var doCD= Math.floor(d-(sCD*146097));

var sC = 0;
var doC = doCD;
if (doCD != 0) sC = Math.floor((doCD-1)/36524);
if (sC != 0) doC -= (sC*36524+1);

var sIV = 0;
var doIV = doC;
if (sC != 0) { // 1460 + 1461*24
sIV = Math.floor((doC+1)/1461);
if(sIV != 0) doIV -= (sIV*1461-1);
} else { // 1461*25
sIV = Math.floor(doC/1461);
if(sIV != 0) doIV -= (sIV*1461);
}

var sI = 0;
var doI = doIV;
if (sC != 0 && sIV == 0) { // four 365-day years in a row
sI = Math.floor(doIV/365);
if(sI != 0) doI -= (sI*365);
} else { // normal leap year cycle
if(doI != 0) sI = Math.floor((doIV-1)/365);
if(sI != 0) doI -= (sI*365 + 1);
}

var earthYear = 400*sCD + 100*sC + 4*sIV + sI;
var tmpDayOfYear = doI+1;

for (var i=1; i<12; i++) {
var tmpDaysInMonth = eDaysInMonth[i];
if (i==2 && !Darian_Date.isEarthLeapYear(earthYear))
tmpDaysInMonth -= 1;

if(tmpDayOfYear > tmpDaysInMonth)
tmpDayOfYear -= tmpDaysInMonth;
else
break;
}

var earthMonth = i-1;
var earthDay = tmpDayOfYear;

var tmpHour = partialDay*24;
var tmpMin = (tmpHour - Math.floor(tmpHour))*60;
var tmpSec = (tmpMin - Math.floor(tmpMin))*60;

var date = new Date(earthYear, earthMonth, earthDay, Math.floor(tmpHour), tmpMin, tmpSec);

// Send Date()
return date;
}
function isMartianLeapYear(year) {
if((year % 500) == 0) return true;
if((year % 100) == 0) return false;
if((year % 10) == 0) return true;
if((year % 2) == 0) return false;
return true;
}

// Darian_Date Class
class Darian_Date {
constructor() {
Expand Down Expand Up @@ -369,17 +490,16 @@ class Darian_Date {
this.dispThisSol = events.this_sol(doI);

// This day
// balblabla
this.dispThisDay = events.this_day(doI, leapDay);

this.mYear = Darian_Date.threeDigit(marsYear);
this.mMonth = marsMonth;
this.mMonthName = marsMonthName;
this.mDay = Darian_Date.twoDigit(marsDay);
this.mJulianDay = (Math.floor(solsSince*100000)/100000); // ??
this.mNumDay = Darian_Date.threeDigit(doI); // Sol in mYear
this.mJulianDay = (Math.floor(solsSince*100000)/100000); // Julian Calendar Date
this.mNumDay = Darian_Date.threeDigit(doI); // Sol in mYear
this.mSolName = nSolName;
// ??
// Mars Solar Longitude
if (Math.round(ls*10)/10 < 10) {
this.mSolarLongitude = "00" + Math.round(ls*10)/10;
} else {
Expand All @@ -398,7 +518,8 @@ class Darian_Date {
this.mSec = Darian_Date.twoDigit(tmpSec);
}

getTime() {
// Methods
getDate() {
var output = '';
if (this.mSolName != undefined) {
output += this.mSolName+', ';
Expand All @@ -423,7 +544,23 @@ class Darian_Date {
}
return output;
}

getJSON() {
return {
dispThisSol: this.dispThisSol,
dispThisDay: this.dispThisDay,
mYear: this.mYear,
mMonth: this.mMonth,
mMonthName: this.mMonthName,
mDay: this.mDay,
mJulianDay: this.mJulianDay,
mNumDay: this.mNumDay,
mSolName: this.mSolName,
mSolarLongitude: this.mSolarLongitude,
mHour: this.mHour,
mMin: this.mMin,
mSec: this.mSec
};
}
getDispThisSol() {
return this.dispThisSol;
}
Expand All @@ -448,14 +585,12 @@ class Darian_Date {
if(n<10) return "0"+Math.floor(n);
else return Math.floor(n);
}

static threeDigit(n) {
if (n < 0)
return "-" + Darian_Date.PadLeft(-n, 3, "0");
else
return Darian_Date.PadLeft(n, 3, "0");
}

static fiveDigit(n) {
if (n < 0)
return "-" + Darian_Date.PadLeft(-n, 5, "0");
Expand All @@ -479,3 +614,4 @@ class Darian_Date {
}

module.exports.Darian_Date = Darian_Date;
module.exports.convMarsToEarth = convMarsToEarth;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "darian-system",
"version": "0.8.0",
"version": "1.0.0",
"description": "Martian time converter",
"main": "./lib/darian_system.js",
"scripts": {
Expand All @@ -13,7 +13,9 @@
"keywords": [
"darian",
"martian",
"converter"
"converter",
"date",
"time"
],
"author": "mrfaraday",
"license": "MIT",
Expand Down

0 comments on commit 81c5c01

Please sign in to comment.