From d7a911ce23d4a6c11c7181538dd6a6bd0e838aeb Mon Sep 17 00:00:00 2001 From: Fabian Enos Date: Mon, 23 Oct 2023 19:06:19 +0530 Subject: [PATCH] feat: Added support for floating point tzOffset (#297) --- src/expressionDescriptor.ts | 26 ++++++++++++++++++++++++-- test/cronstrue.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/expressionDescriptor.ts b/src/expressionDescriptor.ts index d05b391..737d963 100644 --- a/src/expressionDescriptor.ts +++ b/src/expressionDescriptor.ts @@ -633,7 +633,30 @@ export class ExpressionDescriptor { } protected formatTime(hourExpression: string, minuteExpression: string, secondExpression: string) { - let hour: number = parseInt(hourExpression) + (this.options.tzOffset ? this.options.tzOffset : 0); + let hourOffset: number = 0; + let minuteOffset: number = 0; + + if(this.options.tzOffset) { + hourOffset = this.options.tzOffset > 0 ? Math.floor(this.options.tzOffset) : Math.ceil(this.options.tzOffset); + + minuteOffset = (parseFloat((this.options.tzOffset % 1).toFixed(2))); + + if(minuteOffset != 0) { + minuteOffset *= 60; + } + } + + let hour: number = parseInt(hourExpression) + (hourOffset); + let minute: number = parseInt(minuteExpression) + (minuteOffset); + + if (minute >= 60) { + minute -= 60; + hour += 1; + } else if (minute < 0) { + minute += 60; + hour -= 1; + } + if (hour >= 24) { hour = hour - 24; } else if (hour < 0) { @@ -652,7 +675,6 @@ export class ExpressionDescriptor { } } - const minute = minuteExpression; let second: string = ""; if (secondExpression) { second = `:${("00" + secondExpression).substring(secondExpression.length)}`; diff --git a/test/cronstrue.ts b/test/cronstrue.ts index ab3e11c..638546f 100644 --- a/test/cronstrue.ts +++ b/test/cronstrue.ts @@ -159,6 +159,42 @@ describe("Cronstrue", function () { }), "At 02:30 PM"); }); + it("31 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: 5.5 + }), "At 04:01 PM"); + }); + + it("29 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: 5.5 + }), "At 03:59 PM"); + }); + + it("30 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: 5.5 + }), "At 04:00 PM"); + }); + + it("31 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: -1.5 + }), "At 09:01 AM"); + }); + + it("29 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: -1.5 + }), "At 08:59 AM"); + }); + + it("30 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string,{ + tzOffset: -1.5 + }), "At 09:00 AM"); + }); + it("30 11 * * *", function () { assert.equal(cronstrue.toString(this.test?.title as string,{ tzOffset:3,