#ifndef DEVICE_CTRL_DATE_UTIL_H
#define DEVICE_CTRL_DATE_UTIL_H
#include <chrono>
#include <string>
#define Ymd_ "%F"
#define Ymd "%Y%m%d"
#define HMS_ "%T"
#define HMS "%H%M%S"
#define YmdHMS_ "%F %T"
#define YmdHMS "%Y%m%d%H%M%S"

namespace zsy::DateUtil
{
    using TimePoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;

    inline std::string format(const std::string &pattern = YmdHMS_, TimePoint dt = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()))
    {
        std::time_t t = std::chrono::system_clock::to_time_t(dt);
        tm local_tm{};
        localtime_s(&local_tm, &t);
        char buffer[32];
        std::strftime(buffer, sizeof(buffer), pattern.c_str(), &local_tm);
        return {buffer};
    }

    template<typename T = std::chrono::milliseconds>
        requires std::is_base_of_v < std::chrono::duration < typename T::rep

    ,
    typename T::period
    >
    ,
    T
    >
    uint64_t timestamp()
    {
        const auto now = std::chrono::system_clock::now();
        std::chrono::sys_time<T> tz_time = std::chrono::floor<T>(now + std::chrono::hours(8));
        return tz_time.time_since_epoch().count();
    }
}
#endif // DEVICE_CTRL_DATE_UTIL_H