0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cron 式を日本語と英語の自然言語で説明するツール

0
Posted at

作ったもの

Cron Describehttps://sen.ltd/portfolio/cron-describe/

スクリーンショット

  • 標準 5 フィールド cron + ショートカット(@hourly など)
  • 日本語 + 英語の両方で自然言語化
  • 次回実行時刻 5 件
  • 16 サンプル
  • バリデーション

vanilla JS、ゼロ依存、ビルド不要node --test で 55 ケース。

フィールドパース

各フィールドは共通文法:

  • *: 範囲内全部
  • N: 固定値
  • N,M: リスト
  • N-M: 範囲
  • */K: ステップ

結果オブジェクトにパターン種別と値配列の両方を持たせる。種別は説明生成用、値配列は次回実行時刻計算用。

日本語と英語では語順が違う

"at 14:30 on weekdays"  → "平日の 14 時 30 分"

英語は「時刻 + 条件」、日本語は「条件 + 時刻」。単純な翻訳ではなくそれぞれの言語で描き直す。

次回実行時刻

愚直に「1 分進めて、5 フィールドが全部マッチするか確認」を繰り返す:

while (result.length < count) {
  if (matches(cron, d)) result.push(new Date(d));
  d.setMinutes(d.getMinutes() + 1);
}

高速ではないが正確。通常パターンなら数ミリ秒。「毎年 2 月 29 日」のような疎パターンでも 1 年探索で諦める安全策付き。

@reboot は特別扱い

次回実行時刻がない(システム起動時のみ)。パーサがマーカーオブジェクトを返し、次回実行リストは空、説明は「システム起動時」。

シリーズ

100+ 公開ポートフォリオ シリーズの #95 です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?