From 410e9163f7bea91d38884063fb2f487dbd6df8e2 Mon Sep 17 00:00:00 2001 From: nikita Date: Mon, 31 Jul 2023 17:09:31 +0300 Subject: [PATCH 1/2] add tzOffset --- package-lock.json | 4 ++-- package.json | 2 +- src/expressionDescriptor.ts | 22 +++++++++++++++------- src/options.ts | 1 + test/cronstrue.ts | 27 +++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 93f2cb0..f05d611 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cronstrue", - "version": "2.28.0", + "version": "2.28.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cronstrue", - "version": "2.28.0", + "version": "2.28.1", "license": "MIT", "bin": { "cronstrue": "bin/cli.js" diff --git a/package.json b/package.json index d3d4860..93fe020 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cronstrue", "title": "cRonstrue", - "version": "2.28.0", + "version": "2.28.1", "description": "Convert cron expressions into human readable descriptions", "author": "Brady Holt", "license": "MIT", diff --git a/src/expressionDescriptor.ts b/src/expressionDescriptor.ts index ee4ea50..d05b391 100644 --- a/src/expressionDescriptor.ts +++ b/src/expressionDescriptor.ts @@ -27,6 +27,7 @@ export class ExpressionDescriptor { * monthStartIndexZero = false, * use24HourTimeFormat = false, * locale = 'en' + * tzOffset = 0 * }={}] * @returns {string} */ @@ -39,6 +40,7 @@ export class ExpressionDescriptor { monthStartIndexZero = false, use24HourTimeFormat, locale = null, + tzOffset = 0 }: Options = {} ): string { // We take advantage of Destructuring Object Parameters (and defaults) in TS/ES6 and now we will reassemble back to @@ -51,6 +53,7 @@ export class ExpressionDescriptor { monthStartIndexZero: monthStartIndexZero, use24HourTimeFormat: use24HourTimeFormat, locale: locale, + tzOffset: tzOffset }; let descripter = new ExpressionDescriptor(expression, options); @@ -218,8 +221,8 @@ export class ExpressionDescriptor { return s == "0" ? "" : parseInt(s) < 20 - ? this.i18n.atX0SecondsPastTheMinute(s) - : this.i18n.atX0SecondsPastTheMinuteGt20() || this.i18n.atX0SecondsPastTheMinute(s); + ? this.i18n.atX0SecondsPastTheMinute(s) + : this.i18n.atX0SecondsPastTheMinuteGt20() || this.i18n.atX0SecondsPastTheMinute(s); } ); @@ -246,8 +249,8 @@ export class ExpressionDescriptor { return s == "0" && hourExpression.indexOf("/") == -1 && secondsExpression == "" ? this.i18n.everyHour() : parseInt(s) < 20 - ? this.i18n.atX0MinutesPastTheHour(s) - : this.i18n.atX0MinutesPastTheHourGt20() || this.i18n.atX0MinutesPastTheHour(s); + ? this.i18n.atX0MinutesPastTheHour(s) + : this.i18n.atX0MinutesPastTheHourGt20() || this.i18n.atX0MinutesPastTheHour(s); } catch (e) { return this.i18n.atX0MinutesPastTheHour(s); } @@ -450,8 +453,8 @@ export class ExpressionDescriptor { return s == "L" ? this.i18n.lastDay() : this.i18n.dayX0 - ? StringUtilities.format(this.i18n.dayX0(), s) - : s; + ? StringUtilities.format(this.i18n.dayX0(), s) + : s; }, (s) => { return s == "1" ? this.i18n.commaEveryDay() : this.i18n.commaEveryX0Days(s); @@ -630,7 +633,12 @@ export class ExpressionDescriptor { } protected formatTime(hourExpression: string, minuteExpression: string, secondExpression: string) { - let hour: number = parseInt(hourExpression); + let hour: number = parseInt(hourExpression) + (this.options.tzOffset ? this.options.tzOffset : 0); + if (hour >= 24) { + hour = hour - 24; + } else if (hour < 0) { + hour = 24 + hour; + } let period: string = ""; let setPeriodBeforeTime: boolean = false; if (!this.options.use24HourTimeFormat) { diff --git a/src/options.ts b/src/options.ts index 6532732..ba95a9d 100644 --- a/src/options.ts +++ b/src/options.ts @@ -5,4 +5,5 @@ export interface Options { monthStartIndexZero?: boolean; use24HourTimeFormat?: boolean; locale?: string | null; + tzOffset?: number; } diff --git a/test/cronstrue.ts b/test/cronstrue.ts index da194f8..7e20248 100644 --- a/test/cronstrue.ts +++ b/test/cronstrue.ts @@ -153,6 +153,33 @@ describe("Cronstrue", function () { }); describe("at", function () { + it("30 11 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset:3 + }), "At 02:30 PM"); + }); + + it("30 11 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset:3, + use24HourTimeFormat:true + }), "At 14:30"); + }); + + it("30 3 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset:-5, + use24HourTimeFormat:true + }), "At 22:30"); + }); + + it("30 22 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset:5, + use24HourTimeFormat:true + }), "At 03:30"); + }); + it("30 11 * * *", function () { assert.equal(cronstrue.toString(this.test?.title as string), "At 11:30 AM"); }); From 0fbb8cde4981b46ac918f188fa3cb7f6b9b5c659 Mon Sep 17 00:00:00 2001 From: nikita Date: Tue, 1 Aug 2023 08:14:03 +0300 Subject: [PATCH 2/2] fix pr --- README.md | 1 + package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c420d62..578c854 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ An options object can be passed as the second parameter to `cronstrue.toString`. - **monthStartIndexZero: boolean** - Wether to interpret January as `0` or `1`. (Default: false) - **use24HourTimeFormat: boolean** - If true, descriptions will use a [24-hour clock](https://en.wikipedia.org/wiki/24-hour_clock) (Default: false but some translations will default to true) - **locale: string** - The locale to use (Default: "en") +- **tzOffset: number** - Your time offset (in hours) from UTC (Default: 0) ## i18n diff --git a/package-lock.json b/package-lock.json index f05d611..93f2cb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cronstrue", - "version": "2.28.1", + "version": "2.28.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cronstrue", - "version": "2.28.1", + "version": "2.28.0", "license": "MIT", "bin": { "cronstrue": "bin/cli.js" diff --git a/package.json b/package.json index 93fe020..d3d4860 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cronstrue", "title": "cRonstrue", - "version": "2.28.1", + "version": "2.28.0", "description": "Convert cron expressions into human readable descriptions", "author": "Brady Holt", "license": "MIT",