Skip to content

Commit

Permalink
feat: Added support for floating point tzOffset (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianTMC authored Oct 23, 2023
1 parent 6cb4501 commit d7a911c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/expressionDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -652,7 +675,6 @@ export class ExpressionDescriptor {
}
}

const minute = minuteExpression;
let second: string = "";
if (secondExpression) {
second = `:${("00" + secondExpression).substring(secondExpression.length)}`;
Expand Down
36 changes: 36 additions & 0 deletions test/cronstrue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d7a911c

Please sign in to comment.