#include "BuildInfo.h"
#include <cstdio>
#include <cstring>

std::string getBuildInfoString() {
    // Actual compilation time from compiler built-ins.
    // __DATE__ -> "Mmm dd yyyy", __TIME__ -> "HH:MM:SS"
    const char* date = __DATE__;
    const char* time = __TIME__;

    static const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    char mon[4] = {0};
    int day = 0, year = 0, hh = 0, mm = 0, ss = 0;
    std::sscanf(date, "%3s %d %d", mon, &day, &year);
    std::sscanf(time, "%d:%d:%d", &hh, &mm, &ss);

    int mi = 1;
    for (int i = 0; i < 12; ++i)
        if (std::strcmp(mon, months[i]) == 0) { mi = i + 1; break; }

    char buf[64];
    std::snprintf(buf, sizeof(buf), "%04d-%02d-%02d__%02d-%02d-%02d",
                  year, mi, day, hh, mm, ss);

    std::string s = std::string("build ") + CHROMAFLOCK_GIT_HASH
                    + " (" + CHROMAFLOCK_GIT_BRANCH + ")";
    if (CHROMAFLOCK_GIT_DIRTY)
        s += " *";
    s += "   built " + std::string(buf);
    return s;
}
