CronExpressionDescriptor

repository·master·Indexed 22 days ago

https://github.com/bradymholt/cron-expression-descriptor

A .NET library that converts cron expressions into human-readable descriptions. It supports various cron formats (5, 6, or 7 parts), extensive localization for 29 languages, and customizable output options via the ExpressionDescriptor.GetDescription method. Compatible with .NET Standard 1.1 and 2.0, .NET Core >= 1.0, .NET Framework >= 4.5, and Mono >= 4.6.

Tokens
1.4K
Snippets
5
Records
7
Agent score
28%

What's inside CronExpressionDescriptor

  1. Use localization and multiple languages

    master

    The library supports 29 languages. You can specify a locale in three ways:

    1. Per-call: Pass the Locale property in an Options object to GetDescription.
    2. Globally: Call ExpressionDescriptor.SetDefaultLocale("locale_code") to set a default for all subsequent calls.
    3. System Culture: On platforms supporting .NET Standard >= 2.0, the library will respect Thread.CurrentThread.CurrentUICulture if no Locale option is provided.

    Supported Locales: en, zh-Hans (zh-CN), zh-Hant (zh-TW), cz, da, nl, fi, fr, de, he, hu, it, ja, ko, nb, fa, pl, pt-BR, ro, ru, sl-SI, es, es-MX, sv, vi, tr, uk, el, kk, ky.

    // Example: Using a specific locale per call
    ExpressionDescriptor.GetDescription("0-10 11 * * *", new Options(){ Locale = "de" });
    // Returns: "Jede Minute zwischen 11:00 und 11:10"
    
    // Example: Setting a global default locale
    ExpressionDescriptor.SetDefaultLocale("es");
    ExpressionDescriptor.GetDescription("*/45 * * * * *");
    // Returns: "Cada 45 segundos"
  2. Get human-readable descriptions for cron expressions using ExpressionDescriptor.GetDescription()

    master

    Use the ExpressionDescriptor.GetDescription() method to convert a cron expression string into a human-readable English description. This method supports various cron formats, including those with seconds, years, and complex ranges or increments.

    Common Usage Patterns

    • Basic intervals: * * * * * becomes "Every minute".
    • Seconds support: * * * * * * becomes "Every second".
    • Specific times: 0 23 ? * MON-FRI becomes "At 11:00 PM, Monday through Friday".
    • Complex ranges: 5-10 30-35 10-12 * * * becomes "Seconds 5 through 10 past the minute, minutes 30 through 35 past the hour, between 10:00 AM and 12:00 PM".
    • Yearly constraints: * * * * * 2013 becomes "Every minute, only in 2013".
    • Special characters: Supports # (e.g., MON#3 for the third Monday) and L (e.g., 4L for the last Thursday).
    // Every minute
    ExpressionDescriptor.GetDescription("* * * * *");
    
    // Every 45 seconds
    ExpressionDescriptor.GetDescription("*/45 * * * * *");
    
    // At 11:30 AM, Monday through Friday
    ExpressionDescriptor.GetDescription("30 11 * * 1-5");
    
    // Every minute, on the third Monday of the month
    ExpressionDescriptor.GetDescription("* * * * MON#3");
    
    // Every minute, on the last Thursday of the month
    ExpressionDescriptor.GetDescription("* * * * 4L");
    
    // At 12:23 PM, January through February, 2013 through 2014
    ExpressionDescriptor.GetDescription("23 12 * JAN-FEB * 2013-2014");
  3. Configure GetDescription with Options

    master

    You can pass a CronExpressionDescriptor.Options object to GetDescription to customize the output. Available options include:

    • bool ThrowExceptionOnParseError: If true, throws an exception on parse errors; if false, returns the exception message as the description. (Default: true)
    • bool Verbose: Whether to use a more detailed description. (Default: false)
    • bool DayOfWeekStartIndexZero: Whether to interpret Day of Week 1 as Sunday or Monday. (Default: true)
    • ?bool Use24HourTimeFormat: Whether to use a 24-hour clock. (Default: false, though some translations may default to true)
    • string Locale: The locale to use for the description. (Default: "en")
    ExpressionDescriptor.GetDescription("0-10 11 * * *", new Options(){
        DayOfWeekStartIndexZero = false,
        Use24HourTimeFormat = true,
        Locale = "fr"
    });