Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ An options object can be passed as the second parameter to `cronstrue.toString`.
- **dayOfWeekStartIndexZero: boolean** - Whether to interpret cron expression DOW `1` as Sunday or Monday. (Default: true)
- **monthStartIndexZero: boolean** - Whether 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)
- **trimHoursLeadingZero: boolean** - Whether to trim the leading zero that may appear in the hours description; e.g. "02:00 AM" -> "2:00 AM", "08:00" -> "8:00" (Default: false)
- **locale: string** - The locale to use (Default: "en")
- **logicalAndDayFields: boolean** - If true, descriptions for cron expressions with both day of month and day of week specified will follow a logical-AND wording rather than logical-OR wording; e.g. "...between day 11 and 17 of the month, only on Friday" rather than "...between day 11 and 17 of the month, and on Friday" (Default: false)

Expand Down
15 changes: 12 additions & 3 deletions src/expressionDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class ExpressionDescriptor {
* dayOfWeekStartIndexZero = true,
* monthStartIndexZero = false,
* use24HourTimeFormat = false,
* trimHoursLeadingZero = false,
* locale = 'en'
* }={}]
* @returns {string}
Expand All @@ -38,6 +39,7 @@ export class ExpressionDescriptor {
dayOfWeekStartIndexZero = true,
monthStartIndexZero = false,
use24HourTimeFormat,
trimHoursLeadingZero = false,
locale = null,
logicalAndDayFields = false,
}: Options = {}
Expand All @@ -51,6 +53,7 @@ export class ExpressionDescriptor {
dayOfWeekStartIndexZero: dayOfWeekStartIndexZero,
monthStartIndexZero: monthStartIndexZero,
use24HourTimeFormat: use24HourTimeFormat,
trimHoursLeadingZero: trimHoursLeadingZero,
locale: locale,
logicalAndDayFields: logicalAndDayFields,
};
Expand Down Expand Up @@ -706,9 +709,15 @@ export class ExpressionDescriptor {
second = `:${("00" + secondExpression).substring(secondExpression.length)}`;
}

return `${setPeriodBeforeTime ? period : ""}${("00" + hour.toString()).substring(hour.toString().length)}:${(
"00" + minute.toString()
).substring(minute.toString().length)}${second}${!setPeriodBeforeTime ? period : ""}`;
const hourStr = hour.toString();
const paddedHour = ("00" + hourStr).substring(hourStr.length);
const minuteStr = minute.toString();
const paddedMinute = ("00" + minuteStr).substring(minuteStr.length);
const displayHour = this.options.trimHoursLeadingZero ? hourStr : paddedHour;

return `${setPeriodBeforeTime ? period : ""}${displayHour}:${paddedMinute}${second}${
!setPeriodBeforeTime ? period : ""
}`;
}

protected transformVerbosity(description: string, useVerboseFormat: boolean) {
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Options {
dayOfWeekStartIndexZero?: boolean;
monthStartIndexZero?: boolean;
use24HourTimeFormat?: boolean;
trimHoursLeadingZero?: boolean;
locale?: string | null;
/**
* The day of month and day of week are logical AND'ed (as opposed to OR'ed).
Expand Down
29 changes: 29 additions & 0 deletions test/cronstrue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,35 @@ describe("Cronstrue", function () {
assert.equal(cronstrue.toString(this.test?.title as string), "At 03:30 AM");
});

it("30 10 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 10:30 AM");
});

it("31 10 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
"At 10:31"
);
});

it("29 9 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 9:29 AM");
});

it("30 9 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
"At 9:30"
);
});

it("0 0 * * *", function () {
assert.equal(
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
"At 0:00"
);
});

it("10 11 * * *", function () {
assert.equal(cronstrue.toString(this.test?.title as string, { verbose: true }), "At 11:10 AM, every day");
});
Expand Down
3 changes: 1 addition & 2 deletions test/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "mocha";
import cronstrue from "../src/cronstrue-i18n";
import chai = require("chai");
let assert = chai.assert;
import { assert } from "chai";

describe("i18n", function () {
describe("de", function () {
Expand Down