package com.lanyuanxiaoyao.service.scheduler.quartz.distribute.strategy;

import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import java.time.ZonedDateTime;

/**
 * 时间限制
 *
 * @author ZhangJiacheng
 * @date 2023-06-08
 */
public class DatetimeLimit implements AvailableStrategy {
    private static final CronParser PARSER = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));

    private final ExecutionTime execution;

    public DatetimeLimit(String cron) {
        this.execution = ExecutionTime.forCron(PARSER.parse(cron));
    }

    @Override
    public boolean available() {
        return execution.isMatch(ZonedDateTime.now());
    }
}
