package xtools.boot.core.utils;

import xtools.base.exception.CommonException;
import xtools.boot.api.enums.BootError;
import xtools.core.StringUtils;
import xtools.core.enums.TimePattern;
import xtools.core.extend.CheckUtils;
import xtools.core.time.InstantUtils;

import java.io.Serializable;
import java.time.Instant;

/**
 * <p>Title : TimeUtils</p>
 * <p>Description : TimeUtils</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/1/6 14:20
 */
public class TimeUtils implements Serializable {

    /**
     * 时间分隔符
     */
    private static final String T = "T";

    /**
     * 时间转换
     *
     * @param time 时间
     * @return 时间
     */
    public static Instant toInstant(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        if (time.contains(T)) {
            return Instant.parse(time);
        }
        if (CheckUtils.pattern(TimePattern.YMDHMS.regex(), time)) {
            return InstantUtils.parse(time, TimePattern.YMDHMS);
        } else if (CheckUtils.pattern(TimePattern.YMD.regex(), time)) {
            return InstantUtils.parse(time, TimePattern.YMD);
        } else {
            throw CommonException.create(BootError.TIME_CONVERT, time);
        }
    }

}
