A method and apparatus for parsing and calculating the execution time of a CRON expression.

CN122569937APending Publication Date: 2026-08-14HANGZHOU JIECHUANGRUI MEDICAL TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-04-29
Publication Date
2026-08-14

AI Technical Summary

Technical Problem

1.校验不完整、兼容性差仅做简单字段数与正则校验,不支持 5/6/7 字段等多字段的适配,缺少合法范围校验,对 L、W、¥等特殊字符支持不足,易出现非法表达式导致任务异常

Benefits of technology

[0017]本发明的有益效果:方法及装置通过一体化的工具类实现 CRON 表达式的校验、解析、可读描述生成、执行时间计算及执行匹配判断,提高精准度与用户体验。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122569937A_ABST
    Figure CN122569937A_ABST
Patent Text Reader

Abstract

This invention discloses a method and apparatus for parsing and calculating the execution time of a CRON expression. The method includes: S1: Expression validation: receiving a CRON expression, determining that the CRON expression is not empty and is of string type, splitting the fields, and identifying and validating 5, 6, or 7 fields; validating the legality of characters, numerical range, and special characters; S2: Structured parsing: decomposing the CRON expression into structured data; generating a natural language description based on the structured data; S3: Execution time calculation: matching the parsing results with preset execution rules, calculating and returning the next execution timestamp. The method and apparatus realize the validation, parsing, readable description generation, execution time calculation, and execution matching judgment of CRON expressions through an integrated utility class, improving accuracy and user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer software technology, and in particular to a method and apparatus for parsing and calculating the execution time of CRON expressions. Background Technology

[0002] CRON expressions are time rule syntaxes widely used for scheduled tasks. They consist of 5–7 fields, including seconds, minutes, hours, days, months, weeks, and years. They are used in medical management systems, data platforms, automated operation and maintenance systems to execute scheduled tasks such as follow-up reminders, data statistics, and log cleanup.

[0003] Common implementations include various open-source scheduling libraries and basic validation tools, which mainly complete expression parsing and task triggering, but most of them only support basic syntax, have limited functionality, and are poorly integrated.

[0004] At least the following problems or defects exist in existing technologies: 1. Incomplete validation and poor compatibility: It only performs simple field count and regular expression validation, does not support multiple fields such as 5 / 6 / 7 fields, lacks valid range validation, and has insufficient support for special characters such as L, W, and ¥, which can easily lead to illegal expressions and cause task exceptions.

[0005] 2. The parsing results are not intuitive and have low readability. They only output the execution time and do not provide structured parsing or natural language descriptions. This makes them difficult for non-technical personnel to understand and configure, resulting in high maintenance costs.

[0006] 3. Lack of execution time prediction capability makes it impossible to quickly calculate the next execution time and execution frequency, and makes it difficult to verify the correctness of the rules in advance. Summary of the Invention

[0007] The purpose of this invention is to provide a method and apparatus for parsing and calculating the execution time of CRON expressions, so as to solve one or more of the above-mentioned technical problems.

[0008] To achieve this objective, the present invention adopts the following technical solution: A method for parsing and calculating the execution time of CRON expressions includes: S1: Expression validation: S11: Receive CRON expression, determine if the CRON expression is not empty and is of string type, split the fields, and identify and verify fields 5, 6 or 7; S12: Verify the validity of characters, their numerical range, and special characters; S2: Structured parsing: S21: Decompose the CRON expression into structured data; S22: Based on this structured data, generate a natural language description; S3: Execution time calculation; Based on the parsing results, match the preset execution rules, calculate and return the timestamp of the next execution.

[0009] In some implementations, in step S11, the first field of field 5 is minutes, and the first field of fields 6 and 7 is seconds; The format of a CRON expression is either Quartz or node-cron. In step S12, special characters include: numbers, *, / , -, , ?, L, W, #, etc.

[0010] In some implementations, in step S11, the validate method is called to first determine that the CRON expression is not empty and is of string type, and after splitting the fields, the number of fields to be validated is 5, 6 or 7. In step S12, the character validity of each field is validated using regular expressions.

[0011] In some implementations, in step S21, a structured parsing result is formed based on the structured data; Structured data includes field types, value lists, and original values. In step S22, readable text describing the natural language is generated.

[0012] In some implementations, in step S21, the parse method is called to automatically adapt to the number of fields and the parseField method is called to parse each field. In step S22, the generateDescription method is called to integrate the parsing results of each field and generate readable text describing the natural language.

[0013] In some implementations, in step S21, If the field value is "*" or "?", it is recognized as the "any" type, and a fixed-time description is generated. If the field value contains "-", after verifying the start and end value range, it is identified as a "range" type and a time range description is generated; If the field value contains " / ", after verifying that the step size is a positive integer, it is identified as the "step" type, and a time step description is generated.

[0014] In some implementations, in step S3, the getNextExecutionTime method is called to match a preset execution rule; If the calculated time is less than or equal to the current time, it will automatically be postponed to the next cycle. Returns the exact timestamp of the next execution, or an estimated time of 1 minute for complex expressions.

[0015] In some implementations, this method is compiled into an ES6 module using the CronUtils utility class and directly imported into the front-end project; Call the getPresets method to predefine CRON expressions and generate a dropdown list for the user to select from. Bind the validate method to the form input field to validate user input in real time and provide immediate prompts when illegal characters are entered. After entering the expression, the format method is called to generate a readable description in real time, which is then displayed below the input box.

[0016] A CRON expression parsing and execution time calculation device, comprising: The expression validation module is used to validate the input CRON expression for multi-field format, legality, numerical range, and special characters. The structured parsing module is used to break down CRON expressions into structured data and generate natural language descriptions; The execution time calculation module is used to match preset execution rules, calculate and return the timestamp of the next execution.

[0017] The beneficial effects of this invention are: the method and apparatus realize the verification, parsing, readable description generation, execution time calculation and execution matching judgment of CRON expressions through an integrated utility class, thereby improving accuracy and user experience. Attached Figure Description

[0018] Figure 1 This is a flowchart illustrating the steps of a CRON expression parsing and execution time calculation method according to the present invention; Figure 2 This is one of the flowcharts of a CRON expression parsing and execution time calculation method of the present invention, which shows the complete processing logic from input to output, covering the stages of verification, parsing, description generation, and function expansion; Figure 3 This is the second flowchart of a CRON expression parsing and execution time calculation method according to the present invention, which shows the parsing logic of the field, including special character judgment, type recognition, and numerical verification. Figure 4 This is a structural diagram of a CRON expression parsing and execution time calculation device according to the present invention. Detailed Implementation

[0019] The present invention will now be described in further detail with reference to the accompanying drawings.

[0020] refer to Figures 1 to 4 A method for parsing and calculating the execution time of CRON expressions, comprising the following steps: S1: Expression validation: S11: Multi-field validation: Receive a CRON expression, determine if the CRON expression is not empty and is of string type, split the fields, and identify and validate 5, 6, or 7 fields; thus, all CRON expressions with 5 to 7 fields are validated. In step S11, the first field of field 5 is minutes, and the first field of fields 6 and 7 is seconds; the CRON expression is in Quartz or node-cron format.

[0021] S12: Full-dimensional verification; Validate the validity of characters, their numerical range, and special characters; The numerical ranges are 0-23 for hours and 1-12 for months, etc. Special characters include at least: numbers, *, / , -, , ?, L, W, #, etc., for example: L (last day), W (working day), # (week number), etc.

[0022] Therefore, by employing multi-field and full-dimensional validation, compatibility and accuracy are improved. Full-dimensional validation helps prevent illegal expressions from entering the system at the source, reducing the task execution exception rate by more than 90%. It adapts to the syntax specifications of different scenarios and is compatible with the expression formats of mainstream scheduling frameworks (Quartz, node-cron, etc.).

[0023] S2: Structured parsing: S21: Structured field parsing: The CRON expression is broken down into structured data, resulting in a structured parsing result; For example, a CRON expression is broken down into structured data such as seconds, minutes, hours, days, months, weeks, and years; the structured data includes field types, value lists, and original values; The structured parsing results should include at least the field type, value, and description.

[0024] S22: Natural Language Description Generation: Based on this structured data, a natural language description is generated, which is readable text, such as readable Chinese text. For example, "0 0 10 ? * 1-5" can be interpreted as "10 AM on weekdays".

[0025] For example, special character semantic parsing: it generates accurate descriptions for special characters such as L, -, / , , etc., such as "L" being parsed as "the last day of the month", "* / 5 * * * *" being parsed as "every 5 minutes", etc., thus covering more than 95% of common CRON syntax scenarios.

[0026] Therefore, structured field parsing facilitates flexible secondary processing; generating natural language descriptions facilitates understanding, allowing non-technical personnel to directly comprehend the meaning of the rules.

[0027] S3: Execution time calculation; Based on the parsing results, match the preset execution rules, calculate and return the timestamp of the next execution.

[0028] Step S1 also includes the following specific details: In step S11, the validate method is called. First, it is determined that the CRON expression is not empty and is of string type. After splitting the fields, the number of fields is verified to be 5, 6 or 7.

[0029] In step S12, the character validity of each field is validated using regular expressions.

[0030] Step S2 also includes the following specific details: In step S21, the parse method is called to automatically adapt to the number of fields. For example, the first field of field 5 is minutes, and the first field of fields 6 and 7 is seconds. Then, the parseField method is called to parse each field. In step S22, the generateDescription method is called to integrate the parsing results of each field and generate readable text describing the natural language.

[0031] The output of step S2 includes: returning structured parsing results (containing the type, value, and description of each field) and readable text, thus completing the basic processing.

[0032] Furthermore, in step S21: If the field value is "*" or "?", it is recognized as the "any" type and a fixed-time description is generated. For example, a description in the format of "per X", such as "per minute", "daily", "weekly", "monthly", "yearly", etc.

[0033] If a field value contains a "-", it is recognized as a "range" type after verifying the start and end value range, and a time range description is generated. For example, a "XY" format description, such as "hours 8-18", etc.

[0034] If the field value contains " / ", it will be recognized as a "step" type after verifying that the step size is a positive integer, and a time step description will be generated. For example, a description of "every X units" could be "every 5 minutes" or "interval of 5 minutes".

[0035] Step S3 specifically includes the following: S31: Precisely calculate the next execution time: For common CRON rules such as fixed time, step size, and range, it accurately calculates and returns the next execution timestamp, supports custom start time, and allows users to verify in advance whether the rule meets expectations; S32: Automatic execution frequency identification: Generate frequency descriptions such as "execution per second / per minute / per hour / per day / per month" with one click, quickly determine the task execution cycle, and eliminate the need to manually decompose expressions.

[0036] S33: Fault-tolerant time estimation: Provides reasonable execution time estimates for complex CRON expressions, ensuring that there are still reference time results in extreme scenarios and avoiding system anomalies caused by no return value.

[0037] Furthermore, in step S3, the getNextExecutionTime method is called to match the preset execution rules; Fault tolerance check: If the calculated time is less than or equal to the current time, it will be automatically postponed to the next cycle. For example, if the minute step calculation result exceeds 60, it will be postponed by 1 hour. Time output: Returns the exact timestamp of the next execution, or an estimated time of 1 minute for complex expressions.

[0038] This method compiles into an ES6 module using the CronUtils utility class and can be directly imported into the front-end project; First, call the getPresets method to predefine CRON expressions, such as "every hour on the hour", "8 a.m. every day", "6 p.m. on weekdays", etc., and generate a drop-down list for the user to select; Second, bind the validate method to the form input box to validate user input in real time and provide immediate prompts when illegal characters are entered; Third, after the expression is entered, the format method is called to generate a readable description in real time, which is displayed below the input box to improve the user experience.

[0039] This method is adapted to or specifically adapted to medical systems: it is particularly adapted to the scheduling needs of medical management systems such as CBTI (such as timed push follow-up reminders and generation of statistical reports), and the rule descriptions are in line with the cognitive habits of medical operations personnel.

[0040] This method is highly scalable: field configuration and parsing rules support custom extensions (such as adding custom time units and special character parsing logic), adapting to personalized scheduling scenarios.

[0041] The method is low-intrusive: the utility class is a static method encapsulation, which does not require modification of the existing system architecture and can be directly integrated into the existing scheduled task module, resulting in low transformation costs.

[0042] This method is lightweight and efficient in terms of performance.

[0043] No redundant calculations: The parsing process performs calculations on demand, only processing fields that actually exist in the expression, avoiding invalid calculations. The total time to parse 1000 CRON expressions is ≤50ms.

[0044] Low memory usage: The structured parsing results only store necessary information, and the memory usage of a single expression parsing result is ≤1KB, making it suitable for scenarios involving large-scale expression processing.

[0045] Application Example 1: CRON Expression Full Processing This application example is suitable for general software systems to handle CRON expressions in a typical scenario. Expression input: Receive the CRON expression string input by the user, such as "0 0 10 ? * 1-5"; Format validation: The validate method is called. First, it checks if the expression is not empty and is a string type. After splitting the fields, it verifies that there are 5-7 fields. Then, it uses regular expressions to validate the legality of characters in each field (only 0-9, *, / , -, , ?, L, W, #). Structured parsing: The `parse` method is called, automatically adapting to the number of fields (the first field of field 5 is minutes, and the first field of fields 6 / 7 is seconds), and the `parseField` method is called to parse each field individually. If the field value is "*" or "?", it is recognized as "any" type, and a description of "per X" is generated, such as "per minute" or "per day". If the field value contains "-", after verifying the start and end value range, it is identified as a "range" type and a "XY" description is generated, such as "hour 8-18". If the field value contains " / ", after verifying that the step size is a positive integer, it is identified as a "step" type and a description of "per X units" is generated, such as "every 5 minutes"; Readable description generation: The generateDescription method is called to integrate the parsing results of each field and generate a natural language description, such as "0 0 10 ? * 1-5" being parsed as "10 AM every day (working days)". Output: Returns a structured parsing result, which includes the field type, value, description, and readable text of the natural description, completing the basic processing.

[0046] Specifically: On the scheduled task configuration page of the medical CBTI system, the user enters "0 0 9 * * ?". After processing by this implementation method, the parsing result is output: Structured data: hour is of type specific, with a value of 9; minute / second is of type specific, with a value of 0; day / month is of type any; Readable description: "9:00 every day".

[0047] Application Example 2: Precise calculation of CRON expression execution time This application example is suitable for scenarios that require prior verification of CRON execution time. The specific steps are as follows: First, parse the expression using Example 1 to ensure its validity. Rule matching: Call the getNextExecutionTime method to match common execution rules: If it is "execute at a fixed time every day", such as "0 30 14 * * ?", then extract the hour 14, minute 30, and second 0, and calculate the next execution time based on the current time (if the current time has passed 14:30, then it will be 14:30 the next day). If it is "step-by-step execution", such as "* / 10 * * * * ?", then the minute step is extracted as 10, and the current minute is rounded up to a multiple of 10. For example, if the current minute is 23 minutes, the next execution time will be 30 minutes. Fault tolerance check: If the calculated time is less than or equal to the current time, it will be automatically postponed to the next cycle. If the minute step calculation result exceeds 60, it will be postponed by 1 hour. Time output: Returns the exact timestamp of the next execution, or an estimated time of 1 minute for complex expressions.

[0048] Specifically: In the automated operation and maintenance system, if you enter "* / 5 * * * * ?", the current time is 10:23:15. After processing by this implementation method, the next execution time will be returned as 10:25:00.

[0049] Application Example 3: Lightweight front-end adaptation This implementation method is suitable for lightweight front-end scenarios, such as Vue / React projects, and involves adapting and optimizing utility classes. The specific steps are as follows: Utility class import: Compile the CronUtils utility class into an ES6 module and import it directly into the front-end project without requiring back-end dependencies; Preset expression reuse: Call the getPresets method to predefine common CRON expressions, such as "every hour on the hour", "8 a.m. every day", and "6 p.m. on weekdays", and generate a drop-down list for users to select; Real-time validation feedback: Bind the validate method to the form input box to validate user input in real time and provide immediate prompts when illegal characters are entered; Visual description display: After entering an expression, the format method is called to generate a readable description in real time, which is displayed below the input box to improve the user experience.

[0050] Specifically: On the CBTI system's front-end scheduled task configuration page, when the user selects the preset option "Execute at 9 AM every day", the expression "0 0 9 * * ?" is automatically filled in, and the description "9:00 AM every day" is displayed in real time, eliminating the need for the user to manually enter complex expressions.

[0051] A CRON expression parsing and execution time calculation device, comprising: Expression validation module 1 is used to validate the input CRON expression for multi-field format, legality, numerical range, and special characters, i.e., to execute step S1 of the above method; The structured parsing module 2 is used to decompose the CRON expression into structured data and generate a natural language description, which is used to execute step S2 of the above method; The execution time calculation module 3 is used to match the preset execution rules, calculate and return the next execution timestamp, which is used to execute step S3 of the above method.

[0052] A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the above-described method for parsing and calculating the execution time of the CRON expression.

[0053] The above description only discloses some embodiments of the present invention. For those skilled in the art, various modifications and improvements can be made without departing from the inventive concept of the present invention, and these all fall within the scope of protection of the invention.

Claims

1. A method for parsing and calculating the execution time of a CRON expression, comprising: S1: Expression validation: S11: Receive CRON expression, determine if the CRON expression is not empty and is of string type, split the fields, and identify and verify fields 5, 6 or 7; S12: Verify the validity of characters, their numerical range, and special characters; S2: Structured parsing: S21: Decompose the CRON expression into structured data; S22: Based on this structured data, generate a natural language description; S3: Execution time calculation: Based on the parsing results, match the preset execution rules, calculate and return the timestamp of the next execution.

2. The method for calculating CRON expression parsing and execution time according to claim 1, characterized in that, In step S11, the first field of field 5 is minutes, and the first field of fields 6 and 7 is seconds; CRON expressions are in Quartz or node-cron format.

3. The method for calculating CRON expression parsing and execution time according to claim 2, characterized in that, In step S11, the validate method is called to determine that the CRON expression is not empty and is of string type. After splitting the fields, the number of fields to be validated is 5, 6 or 7. In step S12, the character validity of each field is validated using regular expressions.

4. The method for calculating CRON expression parsing and execution time according to claim 1, characterized in that, In step S21, a structured parsing result is generated based on the structured data; Structured data includes field types, value lists, and original values. In step S22, readable text describing the natural language is generated.

5. The method for calculating CRON expression parsing and execution time according to claim 4, characterized in that, In step S21, the parse method is called to automatically adapt to the number of fields and parse the field field by field by field. In step S22, the generateDescription method is called to integrate the parsing results of each field and generate readable text describing the natural language.

6. The method for calculating CRON expression parsing and execution time according to claim 1, characterized in that, In step S21, If the field value is "*" or "?", it is recognized as the "any" type, and a fixed-time description is generated. If the field value contains "-", after verifying the start and end value range, it is identified as a "range" type and a time range description is generated; If the field value contains " / ", after verifying that the step size is a positive integer, it is identified as the "step" type, and a time step description is generated.

7. The method for calculating CRON expression parsing and execution time according to claim 1, characterized in that, In step S3, the getNextExecutionTime method is called to match the preset execution rules; If the calculated time is less than or equal to the current time, it will automatically be postponed to the next cycle. Returns the exact timestamp of the next execution, or an estimated time of 1 minute for complex expressions.

8. The method for calculating CRON expression parsing and execution time according to claim 1, characterized in that, This method is compiled into an ES6 module using the CronUtils utility class and can be directly imported into the front-end project; Call the getPresets method to predefine CRON expressions and generate a dropdown list for the user to select from. Bind the validate method to the form input field to validate user input in real time and provide immediate prompts when illegal characters are entered. After entering the expression, the format method is called to generate a readable description in real time, which is then displayed below the input box.

9. A CRON expression parsing and execution time calculation device, characterized in that, include: The expression validation module is used to validate the input CRON expression for multi-field format, legality, numerical range, and special characters. The structured parsing module is used to break down CRON expressions into structured data and generate natural language descriptions; The execution time calculation module is used to match preset execution rules, calculate and return the timestamp of the next execution.