ORE Studio 0.0.4
Loading...
Searching...
No Matches
cron_expression.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#pragma once
21
22#include <chrono>
23#include <expected>
24#include <string>
25#include <string_view>
26
27namespace ores::scheduler::domain {
28
44class cron_expression final {
45public:
54
61 [[nodiscard]] static std::expected<cron_expression, std::string>
62 from_string(std::string_view expr);
63
67 [[nodiscard]] const std::string& to_string() const noexcept;
68
74 [[nodiscard]] std::chrono::system_clock::time_point
75 next_occurrence(std::chrono::system_clock::time_point after
76 = std::chrono::system_clock::now()) const;
77
78 bool operator==(const cron_expression& other) const noexcept = default;
79
80private:
81 explicit cron_expression(std::string validated_expr);
82
83 std::string expr_;
84};
85
86} // namespace ores::scheduler::domain
Strongly-typed, validated cron expression.
Definition cron_expression.hpp:44
const std::string & to_string() const noexcept
The validated cron string, suitable for pg_cron's cron.schedule().
Definition cron_expression.cpp:61
static std::expected< cron_expression, std::string > from_string(std::string_view expr)
Parse and validate a cron expression string.
Definition cron_expression.cpp:48
std::chrono::system_clock::time_point next_occurrence(std::chrono::system_clock::time_point after=std::chrono::system_clock::now()) const
Compute the next occurrence after the given time point.
Definition cron_expression.cpp:66
cron_expression()
Default constructor. Creates an expression that runs every minute.
Definition cron_expression.cpp:41