Skip to main content

Date-expression domain specific language parsing. A neat way to express things like "First Saturday of any year", or "3rd thursdays each month" and such

Project description

Cronspell Python Package / CLI Tool

Chronometry Spelled Out

Github Pages

Details Tests License - MIT
Features linting - Ruff types - mypy test - pytest Pre-Commit docs - mkdocs

Date-expression domain specific language (DSL) parsing. A neat way to express things like "First Saturday of any year", or "3rd thursdays each month" and such.

Status

CronSpell is currently in Beta. While it is considered well tested and stable for most use cases, there may still be some edge cases and bugs that need to be addressed. The maintainer encourages users to try it out and provide feedback to help improving the library.

Your contributions and bug reports are highly appreciated.

Features

Cronspell is heavily inspired by Grafana's relative Date picker user interface. It was designed for the cases when configuration is needed to reflect irregular date-distances.

It offers a domain specific language to express relative dates and schedules. This way, any kind of date relation that humans easily understand can be conveyed.

Python Usage

Installation:

pip install cronspell

Import:

from cronspell import Cronspell, parse

Cli Usage

Installation:

pip install 'cronspell[cli]'

List available commands:

cronspell --help

Syntax Specification

Cronspell works by iteratively processing anything that matches its language specification.

Based on the starting point Anchor=now[UTC]{:.yml} zero or more subsequent date math directives 0..*{DateMath}{:.yml} affect the outcome.

The most simple although pointles example of a valid model is a zero-withd string due tho these permissive constraints.

Example Expressions

latest Saturday of past month:

/month -1 day /sat

Calendar Week divisible by 3 or 10:

{@cw 3, @cw 10}
Parser Internals — click to toggle visibility
Legend
Entity Pattern Examples
ISODate
\d{4}-\d{2}-\d{2}\S*
1979-01-01T00:00:00+00:00
Now
now
now
now[Europe/Berlin]
CalendarWeekModulo
(%|@)\s*(CW|Cw|cw)
@cw
YearModulo
(%|@)\s*(Y|y)(ears?)?
@y
MonthModulo
(%|@)\s*([Mm])(onths?)?
@m
Y
([Yy]ears?|Y\b)
Y
Years
Year
year
m
(?!mon\b)([mM]onths?|m\b)
m
Months
Month
month
W
\b([wW]eeks?|W)\b
W
Weeks
Week
week
d
([dD]ays?|d\b)
d
Days
Day
day
M
([mM]inutes?|M\b|[mM]in\b)
M
Minutes
Minute
minute
H
([hH]ours?|H\b|[Hh]rs)
H
Hours
Hour
hour
S
(?!sat$)([sS]econds?|S\b|[sS]ec\b)
S
Seconds
Second
second
Mon
\b[mM]on(?:day)?\b
Mon
Monday
monday
Tue
\b[tT]ue(?:sday)?\b
Tue
Tuesday
tuesday
Wed
\b[wW]ed(?:nesday)?\b
Wed
Wednesday
wednesday
Thu
\b[tT]hu(?:rsday)?\b
Thu
Thursday
thursday
Fri
\b[fF]ri(?:day)?\b
Fri
Friday
friday
Sat
\b[sS]at(?:urday)?\b
Sat
Saturday
saturday
Sun
\b[sS]un(?:day)?\b
Sun
Sunday
sunday
Jan
\b[jJ]an(?:uary)?\b
Jan
January
january
Feb
\b[fF]eb(?:ruary)?\b
Feb
February
february
Mar
\b[mM]ar(?:ch)?\b
Mar
March
march
Apr
\b[aA]pr(?:il)?\b
Apr
April
april
May
\b[mM]ay\b
May
May
may
Jun
\b[jJ]une?\b
Jun
June
june
Jul
\b[jJ]uly?\b
Jul
July
july
Aug
\b[aA]ug(?:ust)?\b
Aug
August
august
Sep
\b[sS]ep(?:tember)?\b
Sep
September
september
Oct
\b[oO]ct(?:ober)?\b
Oct
October
october
Nov
\b[nN]ov(?:ember)?\b
Nov
November
november
Dec
\b[dD]ec(?:ember)?\b
Dec
December
december
Comment
\/\*(.|\n)*?\*\/|\/\/.*?$
/*
    multi-line
    block
    comment
*/
/* inline comment */
/* inline comment */

pre-commit hook

This package comes with a pre-commit hook that allows for automated preflight checks on yaml files serving as sources for cronspell expressions.

Put this in your .pre-commit-config.yaml and adjust according to your needs:

repos:
  - repo: https://github.com/iilei/cronspell
    rev: c2b316f53c536e808353d2b30b97d1660c267d4b   # v0.4.1
    hooks:
      - id: cronspell
        files: .*\/cfg\.ya?ml$
        args: ["--yamlpath", "/*/*date*" ]

Credits

  • Domain-Specific-Language Parser: TextX
  • This package was created with The Hatchlor project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cronspell-0.5.0.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cronspell-0.5.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file cronspell-0.5.0.tar.gz.

File metadata

  • Download URL: cronspell-0.5.0.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cronspell-0.5.0.tar.gz
Algorithm Hash digest
SHA256 4f3f2ba25eb7e7071f2351cde9b3608424427d4c49e00f1ab4203e875c819b61
MD5 83d43708783edcb268a671a0378b6614
BLAKE2b-256 2ec95d3681fdecbe885675f89dcaa8c06561b23b521336d79b6c216acc33c286

See more details on using hashes here.

Provenance

The following attestation bundles were made for cronspell-0.5.0.tar.gz:

Publisher: build.yml on iilei/cronspell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cronspell-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: cronspell-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cronspell-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e540830750df5de12a56ec4e88d3cef26196a6c881a76ef43adf286e1577d33
MD5 0a06fd4c77e14e689713e012f9299b79
BLAKE2b-256 f848308c2d01d3ad897f7f5587427cd966d5d00d8171094873cf42884d83c700

See more details on using hashes here.

Provenance

The following attestation bundles were made for cronspell-0.5.0-py3-none-any.whl:

Publisher: build.yml on iilei/cronspell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page