briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.58k stars 1.28k forks source link

Feature request: Next elapse (systemd-analyze calendar) #3095

Open shaedrich opened 4 weeks ago

shaedrich commented 4 weeks ago

It would be nice to have something like systemd-analyze calendar in Carbon

grafik grafik

Idea: https://chaos.social/@rixx/113381352980372544

Docs: https://www.man7.org/linux/man-pages/man1/systemd-analyze.1.html#:~:text=Example%2012.%20Show%20leap%20days%20in%20the%20near%20future

kylekatarnls commented 3 weeks ago

Hello,

As a first sight, this sound like a big enough project to deserve its own package.

Then any kind of package can be integrated in Carbon using macros.

With this use-case it would look like:

// This class could come from a dedicate package
class CalendarAnalyzer
{
    public function analyze(
        DateTimeImmutable $now,
        string $pattern,
        int $iterations = 1,
    ): DateTimeImmutable {
        // Here comes the actual algorithm which can be completely agnostic from Carbon
        return new DateTimeImmutable('...');
    }
}

// Then when bootstrapping the app, we can turn it into a Carbon macro:
Carbon::macro('analyzeCalendar', static function (string $pattern, int $iterations = 1) {
    $analyzer = new CalendarAnalyzer();

    return Carbon::instance($analyzer->analyze(self::this(), $pattern, $iterations));
});

// Then later in your app, you can use it right from Carbon:
Carbon::analyzeCalendar('Tue *-12-25');
Carbon::parse('2026-01-01')->analyzeCalendar('*-02-29', 3);