Skip to content

Commit

Permalink
add tzOffset (#288)
Browse files Browse the repository at this point in the history
* add tzOffset

* fix pr

---------

Co-authored-by: nikita <tulula.nk@gmail.com>
  • Loading branch information
khorevnikita and nikita authored Aug 9, 2023
1 parent a263852 commit c10ce7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 15 additions & 7 deletions src/expressionDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ExpressionDescriptor {
* monthStartIndexZero = false,
* use24HourTimeFormat = false,
* locale = 'en'
* tzOffset = 0
* }={}]
* @returns {string}
*/
Expand All @@ -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
Expand All @@ -51,6 +53,7 @@ export class ExpressionDescriptor {
monthStartIndexZero: monthStartIndexZero,
use24HourTimeFormat: use24HourTimeFormat,
locale: locale,
tzOffset: tzOffset
};

let descripter = new ExpressionDescriptor(expression, options);
Expand Down Expand Up @@ -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);
}
);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Options {
monthStartIndexZero?: boolean;
use24HourTimeFormat?: boolean;
locale?: string | null;
tzOffset?: number;
}
27 changes: 27 additions & 0 deletions test/cronstrue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit c10ce7c

Please sign in to comment.