Skip to content

Commit

Permalink
Deprecate tzOffset option (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradymholt authored Feb 21, 2025
1 parent 31f3cbc commit 08ec53d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 16.14.2
nodejs 20.16.0
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ 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

Expand Down
34 changes: 19 additions & 15 deletions src/expressionDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class ExpressionDescriptor {
* monthStartIndexZero = false,
* use24HourTimeFormat = false,
* locale = 'en'
* tzOffset = 0
* }={}]
* @returns {string}
*/
Expand All @@ -40,7 +39,7 @@ export class ExpressionDescriptor {
monthStartIndexZero = false,
use24HourTimeFormat,
locale = null,
tzOffset = 0
tzOffset = 0,
}: Options = {}
): string {
// We take advantage of Destructuring Object Parameters (and defaults) in TS/ES6 and now we will reassemble back to
Expand All @@ -53,9 +52,13 @@ export class ExpressionDescriptor {
monthStartIndexZero: monthStartIndexZero,
use24HourTimeFormat: use24HourTimeFormat,
locale: locale,
tzOffset: tzOffset
tzOffset: tzOffset,
};

if (options.tzOffset) {
console.warn(`'tzOffset' option has been deprecated and will be removed in a future release.`);
}

let descripter = new ExpressionDescriptor(expression, options);
return descripter.getFullDescription();
}
Expand Down Expand Up @@ -221,8 +224,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);
}
);

Expand All @@ -249,8 +252,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);
}
Expand Down Expand Up @@ -442,7 +445,8 @@ export class ExpressionDescriptor {
case "LW":
description = this.i18n.commaOnTheLastWeekdayOfTheMonth();
break;
default: // i.e. 3W or W2
default:
// i.e. 3W or W2
let weekDayNumberMatches = expression.match(/(\d{1,2}W)|(W\d{1,2})/);
if (weekDayNumberMatches) {
let dayNumber: number = parseInt(weekDayNumberMatches[0].replace("W", ""));
Expand Down Expand Up @@ -471,8 +475,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);
Expand Down Expand Up @@ -654,18 +658,18 @@ export class ExpressionDescriptor {
let hourOffset: number = 0;
let minuteOffset: number = 0;

if(this.options.tzOffset) {
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)));
minuteOffset = parseFloat((this.options.tzOffset % 1).toFixed(2));

if(minuteOffset != 0) {
if (minuteOffset != 0) {
minuteOffset *= 60;
}
}

let hour: number = parseInt(hourExpression) + (hourOffset);
let minute: number = parseInt(minuteExpression) + (minuteOffset);
let hour: number = parseInt(hourExpression) + hourOffset;
let minute: number = parseInt(minuteExpression) + minuteOffset;

if (minute >= 60) {
minute -= 60;
Expand Down
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ export interface Options {
monthStartIndexZero?: boolean;
use24HourTimeFormat?: boolean;
locale?: string | null;

/**
* @deprecated Timezone offset in minutes.
*/
tzOffset?: number;
}
Loading

0 comments on commit 08ec53d

Please # to comment.