New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

unix-timestamp

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unix-timestamp - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

44

lib/timestamp.js

@@ -26,3 +26,3 @@ /**

const DeltaRegExp = new RegExp('^\\s*' +
const OffsetRegExp = new RegExp('^\\s*' +
'([-+]?)\\s*' +

@@ -41,3 +41,3 @@ ['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms']

Object.defineProperty(timestamp, 'round', {
get: function () { return outputFn; },
get: function () { return outputFn === round; },
set: function (value) { outputFn = value ? round : dontRound; }

@@ -48,33 +48,33 @@ });

* Gets the current time as Unix timestamp.
* Optionally applying a given delta specified as either a human-readable string or a number of
* Optionally applying a given offset specified as either a human-readable string or a number of
* seconds.
*
* @param {String|Number} delta The optional delta time to apply
* @param {String|Number} offset The optional time offset to apply
* @returns {Number} The corresponding timestamp
*/
timestamp.now = function (delta) {
timestamp.now = function (offset) {
const now = Date.now() / 1000;
return outputFn(delta ? timestamp.add(now, delta) : now);
return outputFn(offset ? timestamp.add(now, offset) : now);
};
/**
* Applies the given delta to the given timestamp.
* The delta is specified as either a human-readable string or a number of
* Applies the given offset to the given timestamp.
* The offset is specified as either a human-readable string or a number of
* seconds.
*
* @param {Number} time The original timestamp
* @param {String|Number} delta The delta time to apply
* @param {String|Number} offset The time offset to apply
* @returns {Number} The result timestamp
*/
timestamp.add = function (time, delta) {
timestamp.add = function (time, offset) {
if (!isNumber(time)) {
throw new Error('Time must be a number');
}
if (isString(delta)) {
const matches = DeltaRegExp.exec(delta);
if (isString(offset)) {
const matches = OffsetRegExp.exec(offset);
if (!matches) {
throw new Error('Expected delta string format: [+|-] [{years}y] [{months}M] [{weeks}w] ' +
throw new Error('Expected offset string format: [+|-] [{years}y] [{months}M] [{weeks}w] ' +
'[{days}d] [{hours}h] [{minutes}m] [{seconds}s] [{milliseconds}ms]');
}
delta = (matches[1] === '-' ? -1 : 1) * (
offset = (matches[1] === '-' ? -1 : 1) * (
(matches[2] || 0) * timestamp.Year +

@@ -89,17 +89,17 @@ (matches[3] || 0) * timestamp.Month +

);
} else if (!isNumber(delta)) {
throw new Error('Delta must be either a string or a number');
} else if (!isNumber(offset)) {
throw new Error('Offset must be either a string or a number');
}
return outputFn(time + delta);
return outputFn(time + offset);
};
/**
* Gets the delta timestamp for the given delta string.
* Gets the offset timestamp for the given offset string.
* (Alias for .add() using a time of zero.)
*
* @param {String|Number} delta The delta time for the duration
* @returns {Number} The result time delta
* @param {String|Number} offset The time offset for the duration
* @returns {Number} The result time offset
*/
timestamp.duration = function (delta) {
return timestamp.add(0, delta);
timestamp.duration = function (offset) {
return timestamp.add(0, offset);
};

@@ -106,0 +106,0 @@

{
"name": "unix-timestamp",
"version": "1.0.0",
"version": "1.0.1",
"description": "Tiny library to create and manipulate Unix timestamps",

@@ -5,0 +5,0 @@ "keywords": [

@@ -10,12 +10,25 @@ # unix-timestamp

Install with `npm install unix-timestamp`, then:
```sh
npm install unix-timestamp
```
- `timestamp.now([delta])` gives the current time, optionally applying a delta (see below)
- `timestamp.fromDate(dateOrString)` gives the time from a Javascript Date object or an ISO 8601 date string
- `timestamp.toDate(time)` correspondingly gives the date from a timestamp
- `timestamp.add(time, delta)` applies a delta to the given time
- `timestamp.duration(delta)` gives the delta timestamp for the given delta string
Then:
```javascript
const timestamp = require('unix-timestamp');
```
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`.
### Methods
- `.now([offset])` gives the current time, optionally applying an offset (see below)
- `.fromDate(dateOrString)` gives the time from a Javascript Date object or an ISO 8601 date string
- `.toDate(time)` correspondingly gives the date from a timestamp
- `.add(time, offset)` applies an offset to the given time
- `.duration(offset)` gives the offset timestamp for the given offset string
An offset 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 in an offset string are exposed by properties `.Millisecond`, `.Second`, `.Minute`, `.Hour`, `.Day`, `.Week`, `.Month` (i.e. mean Gregorian month) and `.Year`.
### About rounding
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`.

@@ -22,0 +35,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc