Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
9935396f2d | |||
1bc379daa5 | |||
f9b81fc801 | |||
6d8e39f11b | |||
cff5b5a7f4 | |||
f6be9b71f1 | |||
9c2d584170 | |||
10bff069c1 | |||
8f6272eca4 | |||
f02df7789c | |||
d720b8b084 | |||
6bca8557f9 | |||
f0a5e18335 | |||
57fbdb4a70 | |||
4a56527f7d | |||
2a14d928a8 | |||
f161f71c4e | |||
ed1f79caa4 |
@ -26,6 +26,7 @@ mirror:
|
|||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install -g snyk
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
@ -36,21 +37,11 @@ snyk:
|
|||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -62,6 +53,7 @@ testLTS:
|
|||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -117,8 +109,10 @@ pages:
|
|||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci command npm install -g typedoc typescript
|
||||||
- npmci command npmpage
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
@ -128,3 +122,4 @@ pages:
|
|||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"npmts"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smarttime",
|
||||||
|
"shortDescription": "handle time in smart ways",
|
||||||
|
"npmPackagename": "@pushrocks/smarttime",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
1511
package-lock.json
generated
1511
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@ -1,23 +1,30 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.2",
|
"version": "3.0.7",
|
||||||
"description": "handle timeformats in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tsrun ./test/test.ts)",
|
"test": "(tstest ./test/)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.1.2",
|
"@gitzone/tsbuild": "^2.1.8",
|
||||||
"@types/node": "^10.5.2",
|
"@gitzone/tsrun": "^1.2.5",
|
||||||
"tapbundle": "^2.0.2"
|
"@gitzone/tstest": "^1.0.20",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
|
"@types/node": "^11.13.2",
|
||||||
|
"tslint": "^5.15.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"moment": "^2.21.0"
|
"@types/cron": "^1.7.0",
|
||||||
|
"@types/luxon": "^1.12.0",
|
||||||
|
"cron": "^1.7.0",
|
||||||
|
"luxon": "^1.12.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
readme.md
50
readme.md
@ -1,33 +1,39 @@
|
|||||||
# smarttime
|
# @pushrocks/smarttime
|
||||||
|
handle time in smart ways
|
||||||
|
|
||||||
handle timeformats in smart ways
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smarttime)
|
||||||
## Availabililty
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smarttime)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smarttime)
|
||||||
[](https://www.npmjs.com/package/smarttime)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smarttime/)
|
||||||
[](https://GitLab.com/pushrocks/smarttime)
|
|
||||||
[](https://github.com/pushrocks/smarttime)
|
|
||||||
[](https://pushrocks.gitlab.io/smarttime/)
|
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
[](https://gitlab.com/pushrocks/smarttime/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smarttime/commits/master)
|
[](https://gitlab.com/pushrocks/smarttime/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smarttime/commits/master)
|
[](https://www.npmjs.com/package/@pushrocks/smarttime)
|
||||||
[](https://www.npmjs.com/package/smarttime)
|
[](https://snyk.io/test/npm/@pushrocks/smarttime)
|
||||||
[](https://david-dm.org/pushrocks/smarttime)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/pushrocks/smarttime/master/dependencies/npm)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/pushrocks/smarttime)
|
[](https://prettier.io/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
Smarttime offers smart ways to deal with time.
|
||||||
|
|
||||||
|
### class ExtendedDate
|
||||||
|
|
||||||
|
This class offers static functions to create zone speicific JavaScript dates from European formated time strings.
|
||||||
|
|
||||||
|
```TypeScript
|
||||||
|
import { ExtendedDate } from '@pushrocks/smarttime'
|
||||||
|
const myDate: Date = ExtendedDate.fromEuropeanDate('8.6.2018')
|
||||||
|
```
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://maintainedby.lossless.com)
|
||||||
|
27
test/test.cronmanager.ts
Normal file
27
test/test.cronmanager.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
|
let testCronManager: smarttime.CronManager;
|
||||||
|
|
||||||
|
tap.test('should create a valid instance of cronmanager', async () => {
|
||||||
|
testCronManager = new smarttime.CronManager();
|
||||||
|
expect(testCronManager).to.be.instanceOf(smarttime.CronManager);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a valid cronJon', async (tools) => {
|
||||||
|
const done = tools.defer();
|
||||||
|
let counter = 0;
|
||||||
|
testCronManager.addCronjob('* * * * * *', () => {
|
||||||
|
if(counter === 10) {
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
counter ++;
|
||||||
|
console.log(`hey ${counter}`);
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
testCronManager.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
19
test/test.date.ts
Normal file
19
test/test.date.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
|
tap.test('should create a valid JavaScript Date from European TimeStamp', async () => {
|
||||||
|
const extendedDate = smarttime.ExtendedDate.fromEuropeanDate('1.6.2018');
|
||||||
|
console.log(extendedDate);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a date and time with European Format', async () => {
|
||||||
|
const extendedDate = smarttime.ExtendedDate.fromEuropeanDateAndTime(
|
||||||
|
'9.8.2018',
|
||||||
|
'08:00:00',
|
||||||
|
'Europe/Berlin'
|
||||||
|
);
|
||||||
|
console.log(extendedDate);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
@ -1,5 +1,5 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import { Timer } from '../ts/index';
|
import { Timer } from '../ts/index';
|
||||||
|
|
||||||
@ -15,4 +15,12 @@ tap.test('should start timer', async () => {
|
|||||||
await testTimer.completed;
|
await testTimer.completed;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should reset a timer', async () => {
|
||||||
|
testTimer.reset();
|
||||||
|
testTimer.start();
|
||||||
|
testTimer.reset();
|
||||||
|
testTimer.start();
|
||||||
|
await testTimer.completed;
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smarttime from '../ts/index';
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
// Test TimeStamp class
|
// Test TimeStamp class
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export * from './smarttime.classes.cronmanager';
|
||||||
|
export * from './smarttime.classes.date';
|
||||||
export * from './smarttime.classes.hrtmeasurement';
|
export * from './smarttime.classes.hrtmeasurement';
|
||||||
export * from './smarttime.classes.timer';
|
export * from './smarttime.classes.timer';
|
||||||
export * from './smarttime.classes.timestamp';
|
export * from './smarttime.classes.timestamp';
|
||||||
export * from './smarttime.units';
|
export * from './smarttime.units';
|
||||||
|
|
||||||
export { moment } from './smarttime.plugins';
|
export { luxon } from './smarttime.plugins';
|
||||||
|
35
ts/smarttime.classes.cronmanager.ts
Normal file
35
ts/smarttime.classes.cronmanager.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export class CronManager {
|
||||||
|
public status: 'started' | 'stopped' = 'stopped';
|
||||||
|
|
||||||
|
public cronjobs: plugins.cron.CronJob[] = [];
|
||||||
|
|
||||||
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: plugins.cron.CronCommand) {
|
||||||
|
const newCronJob = new plugins.cron.CronJob(cronIdentifierArg,cronFunctionArg);
|
||||||
|
if (this.status === 'started') {
|
||||||
|
newCronJob.start();
|
||||||
|
}
|
||||||
|
this.cronjobs.push(newCronJob);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts the cronjob
|
||||||
|
*/
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
for (const cron of this.cronjobs) {
|
||||||
|
cron.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stops all cronjobs
|
||||||
|
*/
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
for (const cron of this.cronjobs) {
|
||||||
|
cron.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,40 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export type TAvailableZone = 'Europe/Berlin';
|
||||||
|
|
||||||
|
export class ExtendedDate extends Date {
|
||||||
|
public static fromEuropeanDate(europeanDate: string) {
|
||||||
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
||||||
|
const luxonDate = plugins.luxon.DateTime.utc(
|
||||||
|
parseFloat(dateArray[3]), // year
|
||||||
|
parseFloat(dateArray[2]), // month
|
||||||
|
parseFloat(dateArray[1]) // day
|
||||||
|
);
|
||||||
|
const unixMilli = luxonDate.toMillis();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public static fromEuropeanDateAndTime(
|
||||||
|
europeanDateArg: string,
|
||||||
|
timeArg: string,
|
||||||
|
zoneArg: TAvailableZone
|
||||||
|
) {
|
||||||
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
|
||||||
|
const sliceDate = (dateString: string) => {
|
||||||
|
return `0${dateString}`.slice(-2);
|
||||||
|
};
|
||||||
|
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
|
||||||
|
dateArray[1]
|
||||||
|
)}T${timeArg}`;
|
||||||
|
const luxonDate = plugins.luxon.DateTime.fromISO(dateTimeString, {
|
||||||
|
zone: zoneArg
|
||||||
|
});
|
||||||
|
const unixMilli = luxonDate.toMillis();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(unixMilli: number) {
|
||||||
|
super(unixMilli);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -61,15 +61,26 @@ export class Timer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public pause() {
|
public pause() {
|
||||||
|
if (this.startedAt) {
|
||||||
clearTimeout(this.currentTimeout);
|
clearTimeout(this.currentTimeout);
|
||||||
|
this.currentTimeout = null;
|
||||||
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public resume() {
|
public resume() {
|
||||||
if (this.startedAt) {
|
if (this.startedAt) {
|
||||||
this.currentTimeout = setTimeout(() => {
|
this.currentTimeout = setTimeout(() => {
|
||||||
this.completedDeferred.resolve();
|
this.completedDeferred.resolve();
|
||||||
}, this.timeLeft);
|
}, this.timeLeft);
|
||||||
|
} else {
|
||||||
|
throw new Error('timer has NOT been started before. Please use .start() instead');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public reset() {
|
||||||
|
this.pause();
|
||||||
|
this.startedAt = null;
|
||||||
|
this.pausedAt = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,11 @@ export class TimeStamp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a boolean for wether a timestamp is younger
|
||||||
|
* @param TimeStampArg
|
||||||
|
* @param tresholdTimeArg
|
||||||
|
*/
|
||||||
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
|
||||||
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
|
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import * as moment from 'moment';
|
// @pushrocks scope
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { moment, smartpromise };
|
export { smartpromise };
|
||||||
|
|
||||||
|
// third parties
|
||||||
|
import * as luxon from 'luxon';
|
||||||
|
import * as cron from 'cron';
|
||||||
|
|
||||||
|
export { luxon, cron };
|
||||||
|
17
tslint.json
17
tslint.json
@ -1,6 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": [
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
"tslint:latest",
|
"rules": {
|
||||||
"tslint-config-prettier"
|
"semicolon": [true, "always"],
|
||||||
|
"no-console": false,
|
||||||
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
]
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user