unix-timestamp
Advanced tools
Comparing version 0.2.0 to 1.0.0
/** | ||
* @license | ||
* [BSD-3-Clause](https://github.com/pryv/unix-timestamp-js/blob/master/LICENSE) | ||
*/ | ||
/** | ||
* Tiny library to create and manipulate Unix timestamps | ||
@@ -6,3 +10,3 @@ * (i.e. defined as the number of seconds since Unix epoch time). | ||
var timestamp = module.exports = {}; | ||
const timestamp = module.exports = {}; | ||
@@ -23,12 +27,12 @@ // constants | ||
var DeltaRegExp = new RegExp('^\\s*' + | ||
const DeltaRegExp = new RegExp('^\\s*' + | ||
'([-+]?)\\s*' + | ||
[ 'y', 'M', 'w', 'd', 'h', 'm', 's', 'ms' ] | ||
.map(function (t) { return '(?:(\\d+)\\s*' + t + ')?'; }) | ||
.join('\\s*') + | ||
['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms'] | ||
.map(function (t) { return '(?:(\\d+)\\s*' + t + ')?'; }) | ||
.join('\\s*') + | ||
'\\s*$'); | ||
var outputFn = dontRound; | ||
function dontRound(time) { return time; } | ||
function round(time) { return Math.round(time); } | ||
let outputFn = dontRound; | ||
function dontRound (time) { return time; } | ||
function round (time) { return Math.round(time); } | ||
/** | ||
@@ -42,3 +46,2 @@ * Set to `true` to round all returned timestamps to the second. Defaults to `false`. | ||
/** | ||
@@ -53,3 +56,3 @@ * Gets the current time as Unix timestamp. | ||
timestamp.now = function (delta) { | ||
var now = Date.now() / 1000; | ||
const now = Date.now() / 1000; | ||
return outputFn(delta ? timestamp.add(now, delta) : now); | ||
@@ -68,8 +71,8 @@ }; | ||
timestamp.add = function (time, delta) { | ||
if (! isNumber(time)) { | ||
if (!isNumber(time)) { | ||
throw new Error('Time must be a number'); | ||
} | ||
if (isString(delta)) { | ||
var matches = DeltaRegExp.exec(delta); | ||
if (! matches) { | ||
const matches = DeltaRegExp.exec(delta); | ||
if (!matches) { | ||
throw new Error('Expected delta string format: [+|-] [{years}y] [{months}M] [{weeks}w] ' + | ||
@@ -79,12 +82,12 @@ '[{days}d] [{hours}h] [{minutes}m] [{seconds}s] [{milliseconds}ms]'); | ||
delta = (matches[1] === '-' ? -1 : 1) * ( | ||
(matches[2] || 0) * timestamp.Year + | ||
(matches[3] || 0) * timestamp.Month + | ||
(matches[4] || 0) * timestamp.Week + | ||
(matches[5] || 0) * timestamp.Day + | ||
(matches[6] || 0) * timestamp.Hour + | ||
(matches[7] || 0) * timestamp.Minute + | ||
(matches[8] || 0) * timestamp.Second + | ||
(matches[9] || 0) * timestamp.Millisecond | ||
(matches[2] || 0) * timestamp.Year + | ||
(matches[3] || 0) * timestamp.Month + | ||
(matches[4] || 0) * timestamp.Week + | ||
(matches[5] || 0) * timestamp.Day + | ||
(matches[6] || 0) * timestamp.Hour + | ||
(matches[7] || 0) * timestamp.Minute + | ||
(matches[8] || 0) * timestamp.Second + | ||
(matches[9] || 0) * timestamp.Millisecond | ||
); | ||
} else if (! isNumber(delta)) { | ||
} else if (!isNumber(delta)) { | ||
throw new Error('Delta must be either a string or a number'); | ||
@@ -115,3 +118,3 @@ } | ||
date = new Date(date); | ||
} else if (! isDate(date)) { | ||
} else if (!isDate(date)) { | ||
throw new Error('Expected either a string or a date'); | ||
@@ -129,3 +132,3 @@ } | ||
timestamp.toDate = function (time) { | ||
if (! isNumber(time)) { | ||
if (!isNumber(time)) { | ||
throw new Error('Expected a number'); | ||
@@ -136,12 +139,12 @@ } | ||
function isString(value) { | ||
function isString (value) { | ||
return typeof value === 'string' || Object.prototype.toString.call(value) === '[object String]'; | ||
} | ||
function isNumber(value) { | ||
function isNumber (value) { | ||
return typeof value === 'number' || Object.prototype.toString.call(value) === '[object Number]'; | ||
} | ||
function isDate(value) { | ||
function isDate (value) { | ||
return Object.prototype.toString.call(value) === '[object Date]'; | ||
} |
{ | ||
"name": "unix-timestamp", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "Tiny library to create and manipulate Unix timestamps", | ||
@@ -13,23 +13,24 @@ "keywords": [ | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/pryv/unix-timestamp-js.git" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"author": { | ||
"name": "Simon Goumaz", | ||
"url": "http://github.com/sgoumaz" | ||
}, | ||
"author": "Pryv S.A <info@pryv.com> (https://pryv.com)", | ||
"main": "./lib/timestamp", | ||
"files": [ | ||
"lib" | ||
], | ||
"main": "./lib/timestamp", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/pryv/unix-timestamp-js.git" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha" | ||
"license": "source-licenser -c .licenser.yml .", | ||
"test": "semistandard && mocha", | ||
"test-cover": "nyc npm run test" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "^2.1.0", | ||
"should": "^5.0.0" | ||
"mocha": "^9.2.2", | ||
"nyc": "^15.1.0", | ||
"semistandard": "^16.0.1", | ||
"should": "^13.2.3", | ||
"source-licenser": "^2.0.3" | ||
} | ||
} |
# unix-timestamp | ||
[](https://github.com/pryv/unix-timestamp-js/actions/workflows/ci.yml) [](https://www.npmjs.com/package/unix-timestamp) | ||
Tiny library to create and manipulate Unix timestamps in Javascript. (A Unix timestamp is the number of seconds elapsed since Unix epoch time, i.e. January 1 1970 00:00 UTC.) | ||
[](http://badge.fury.io/js/unix-timestamp) | ||
@@ -17,3 +18,3 @@ ## Usage | ||
A delta can be either a number (unit: seconds) or a string with format `[+|-] [{years}y] [{months}M] [{weeks}w] [{days}d] [{hours}h] [{minutes}m] [{seconds}s] [{milliseconds}ms]` (for example `-30s`). The actual values (in seconds) used for each unit of time are accessible in properties `Millisecond`, `Second`, `Minute`, `Hour`, `Day`, `Week`, `Month` (i.e. mean gregorian month) and `Year`. | ||
A delta can be either a number (unit: seconds) or a string with format `[+|-] [{years}y] [{months}M] [{weeks}w] [{days}d] [{hours}h] [{minutes}m] [{seconds}s] [{milliseconds}ms]` (for example `-30s`). The actual values (in seconds) used for each unit of time are accessible in properties `Millisecond`, `Second`, `Minute`, `Hour`, `Day`, `Week`, `Month` (i.e. mean Gregorian month) and `Year`. | ||
@@ -23,8 +24,15 @@ By default timestamps include decimals (fractions of a second). You can set the lib to round all returned timestamps to the second with `timestamp.round = true`. | ||
## Tests | ||
## Contributing | ||
Install dev dependencies with `npm install`, then `npm test`. | ||
`npm test` runs the [tests](./test) with [Mocha](https://mochajs.org/). | ||
`npm run test-cover` runs the tests and outputs coverage stats with [Istanbul](https://istanbul.js.org/). | ||
`npm run license` updates license information with [source-licenser](https://github.com/pryv/source-licenser). | ||
The code follows the [Semi-Standard](https://github.com/standard/semistandard) style. | ||
## License | ||
[Revised BSD license](https://github.com/pryv/documents/blob/master/license-bsd-revised.md) | ||
[BSD-3-Clause](https://github.com/pryv/unix-timestamp-js/blob/master/LICENSE) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8480
4
128
1
37
5