stardate-converter
Advanced tools
Comparing version 1.0.0 to 2.0.0
65
index.js
@@ -1,36 +0,47 @@ | ||
const BASE_YEAR = 2323; | ||
var STAR_TREK_EPOCH = 2323; | ||
function isLeapYear(year) { | ||
return new Date(year, 1, 29).getMonth() === 1; | ||
/** | ||
* Converts given date into a stardate. | ||
* | ||
* Formula is based on http://www.wikihow.com/Calculate-Stardates | ||
* | ||
* @param {Date} date | ||
* @return {Number} stardate | ||
*/ | ||
module.exports = function(date) { | ||
var year = date.getFullYear(); | ||
var month = date.getMonth(); | ||
var day = date.getDate(); | ||
return round(starYear(year) + starDay(year, month, day)); | ||
}; | ||
function starYear(year) { | ||
return 1000 * (year - STAR_TREK_EPOCH); | ||
} | ||
function round(number) { | ||
return Math.round(number * 100) / 100; | ||
function starDay(year, month, day) { | ||
return 1000 / daysInYear(year) * dayOfYear(year, month, day); | ||
} | ||
function monthNumber(year, month) { | ||
const number = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334][month]; | ||
function daysInYear(year) { | ||
return isLeapYear(year) ? 366 : 365; | ||
} | ||
return month >= 2 && isLeapYear() ? number + 1 : number; | ||
function dayOfYear(year, month, day) { | ||
var dayOfYear = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334][month] + day - 1; | ||
if (month >= 2 && isLeapYear(year)) { | ||
dayOfYear ++; | ||
} | ||
return dayOfYear; | ||
} | ||
module.exports = { | ||
/** | ||
* Converts given date into a stardate | ||
* | ||
* Formula is based on http://www.wikihow.com/Calculate-Stardates | ||
* | ||
* @param {Date} date | ||
* @return {Number} stardate | ||
*/ | ||
convert: function(date) { | ||
const year = date.getFullYear(); | ||
const month = date.getMonth(); | ||
const day = date.getDay(); | ||
const daysInYear = isLeapYear(year) ? 366 : 365; | ||
function isLeapYear(year) { | ||
return new Date(year, 1, 29).getMonth() === 1; | ||
} | ||
const starYear = 1000 * (year - BASE_YEAR); | ||
return round(starYear + 1000 / daysInYear * (monthNumber(year, month) + day - 1)); | ||
} | ||
}; | ||
// Stardates are usually quoted to two decimal places. | ||
function round(number) { | ||
return Math.round(number * 100) / 100; | ||
} |
{ | ||
"name": "stardate-converter", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Converts common date to Star Trek stardate", | ||
"main": "index.js", | ||
"keywords": [ | ||
@@ -13,2 +12,6 @@ "stardate", | ||
], | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
@@ -15,0 +18,0 @@ "test": "jest", |
@@ -1,9 +0,7 @@ | ||
# Stardate Converter | ||
# Stardate Converter · HovpoH MughwI' [![NPM version](https://img.shields.io/npm/v/stardate-converter.svg)](https://npmjs.com/package/stardate-converter) [![Build Status](https://travis-ci.org/zeroturnaround/stardate-converter.svg?branch=master)](https://travis-ci.org/zeroturnaround/stardate-converter) | ||
**Stardate Converter** is a JavaScript library for converting common date to Star Trek stardate. | ||
**Stardate Converter (HovpoH MughwI')** is a JavaScript library for converting common date to Star Trek stardate (Hov Trek hovpoh). | ||
## Install | ||
## Install · Jom | ||
Get the latest version from NPM: | ||
``` | ||
@@ -16,15 +14,15 @@ npm install stardate-converter --save | ||
```js | ||
import stardateConverter from "stardate-converter"; | ||
import stardate from "stardate-converter"; | ||
console.log(stardateConverter.convert(new Date(2017, 9, 24))); | ||
console.log(stardate(new Date(2017, 9, 24))); | ||
``` | ||
This will output: | ||
Output: | ||
``` | ||
-305249.32 | ||
-305189.04 | ||
``` | ||
## License | ||
## License · Chaw' | ||
[MIT](https://github.com/zeroturnaround/stardate-converter/blob/master/LICENSE) |
3660
4
38
28