Created
July 10, 2017 05:01
-
-
Save namazso/6a3e709e0f767a903db56a808c4d6596 to your computer and use it in GitHub Desktop.
Get the compilation time in unix time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace cx | |
{ | |
constexpr bool cx_strcmp_part(const char* const a, const char* const b) | |
{ | |
return *b ? (*a == *b && cx_strcmp_part(a + 1, b + 1)) : true; | |
} | |
constexpr unsigned month_str_to_int(const char* const str) | |
{ | |
return | |
cx_strcmp_part(str, "Jan") ? 1 : | |
cx_strcmp_part(str, "Feb") ? 2 : | |
cx_strcmp_part(str, "Mar") ? 3 : | |
cx_strcmp_part(str, "Apr") ? 4 : | |
cx_strcmp_part(str, "May") ? 5 : | |
cx_strcmp_part(str, "Jun") ? 6 : | |
cx_strcmp_part(str, "Jul") ? 7 : | |
cx_strcmp_part(str, "Aug") ? 8 : | |
cx_strcmp_part(str, "Sep") ? 9 : | |
cx_strcmp_part(str, "Oct") ? 10 : | |
cx_strcmp_part(str, "Nov") ? 11 : 12; | |
} | |
constexpr unsigned char_to_int(const unsigned char c) | |
{ | |
return (c == ' ' ? '0' : c) - '0'; | |
} | |
constexpr unsigned twochar_to_int(const char* const str) | |
{ | |
return (char_to_int(str[0])) * 10 + char_to_int(str[1]); | |
} | |
constexpr unsigned year_str_to_int(const char* const str) | |
{ | |
return (char_to_int(str[0])) * 1000 + (char_to_int(str[1])) * 100 + (char_to_int(str[2])) * 10 + char_to_int(str[3]); | |
} | |
constexpr unsigned days_from_civil(int y, int m, int d) | |
{ | |
const int yc = y - (m <= 2 ? 1 : 0); | |
const int era = (yc >= 0 ? yc : yc - 399) / 400; | |
const unsigned yoe = static_cast<unsigned>(yc - era * 400); // [0, 399] | |
const unsigned doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365] | |
const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096] | |
return era * 146097 + int(doe) - 719468; | |
} | |
constexpr unsigned date_str_to_days(const char* const str) | |
{ | |
return days_from_civil(year_str_to_int(str + 7), month_str_to_int(str), twochar_to_int(str + 4)); | |
} | |
constexpr unsigned time_str_to_second(const char* const str) | |
{ | |
return (twochar_to_int(str) * 60 + twochar_to_int(str + 3)) * 60 + twochar_to_int(str + 6); | |
} | |
constexpr uint64_t unix_time() | |
{ | |
return uint64_t(date_str_to_days(__DATE__)) * 24 * 60 * 60 + time_str_to_second(__TIME__); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment