AuROXTL/Include/auROXTL/auBuildTime.hpp

123 lines
5.6 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuBuildTime.hpp
Date: 2023-2-22
Author: Reece
Credit: https://stackoverflow.com/a/44271643
***/
// extracts 1..4 characters from a string and interprets it as a decimal value
2023-05-02 19:21:33 +00:00
#define __AU_CONV_STR2DEC_1(str, i) (str[i]>'0'?str[i]-'0':0)
#define __AU_CONV_STR2DEC_2(str, i) (__AU_CONV_STR2DEC_1(str, i)*10 + str[i+1]-'0')
#define __AU_CONV_STR2DEC_3(str, i) (__AU_CONV_STR2DEC_2(str, i)*10 + str[i+2]-'0')
#define __AU_CONV_STR2DEC_4(str, i) (__AU_CONV_STR2DEC_3(str, i)*10 + str[i+3]-'0')
// Some definitions for calculation
2023-05-02 19:21:33 +00:00
#define __AU_SEC_PER_MIN 60UL
#define __AU_SEC_PER_HOUR 3600UL
#define __AU_SEC_PER_DAY 86400UL
#define __AU_SEC_PER_YEAR (__AU_SEC_PER_DAY*365)
#define __AU_UNIX_START_YEAR 1970UL
// Custom "glue logic" to convert the month name to a usable number
2023-05-02 19:21:33 +00:00
#define __AU_GET_MONTH(str, i) (str[i]=='J' && str[i+1]=='a' && str[i+2]=='n' ? 1 : \
str[i]=='F' && str[i+1]=='e' && str[i+2]=='b' ? 2 : \
str[i]=='M' && str[i+1]=='a' && str[i+2]=='r' ? 3 : \
str[i]=='A' && str[i+1]=='p' && str[i+2]=='r' ? 4 : \
str[i]=='M' && str[i+1]=='a' && str[i+2]=='y' ? 5 : \
str[i]=='J' && str[i+1]=='u' && str[i+2]=='n' ? 6 : \
str[i]=='J' && str[i+1]=='u' && str[i+2]=='l' ? 7 : \
str[i]=='A' && str[i+1]=='u' && str[i+2]=='g' ? 8 : \
str[i]=='S' && str[i+1]=='e' && str[i+2]=='p' ? 9 : \
str[i]=='O' && str[i+1]=='c' && str[i+2]=='t' ? 10 : \
str[i]=='N' && str[i+1]=='o' && str[i+2]=='v' ? 11 : \
str[i]=='D' && str[i+1]=='e' && str[i+2]=='c' ? 12 : 0)
2023-05-02 19:21:33 +00:00
#define __AU_GET_MONTH2DAYS(month) ((month == 1 ? 0 : 31 + \
(month == 2 ? 0 : 28 + \
(month == 3 ? 0 : 31 + \
(month == 4 ? 0 : 30 + \
(month == 5 ? 0 : 31 + \
(month == 6 ? 0 : 30 + \
(month == 7 ? 0 : 31 + \
(month == 8 ? 0 : 31 + \
(month == 9 ? 0 : 30 + \
(month == 10 ? 0 : 31 + \
(month == 11 ? 0 : 30))))))))))))
2023-05-02 19:21:33 +00:00
#define __AU_TIME_SECONDS__ __AU_CONV_STR2DEC_2(__TIME__, 6)
#define __AU_TIME_MINUTES__ __AU_CONV_STR2DEC_2(__TIME__, 3)
#define __AU_TIME_HOURS__ __AU_CONV_STR2DEC_2(__TIME__, 0)
#define __AU_TIME_DAYS__ __AU_CONV_STR2DEC_2(__DATE__, 4)
#define __AU_TIME_MONTH__ __AU_GET_MONTH(__DATE__, 0)
#define __AU_TIME_YEARS__ __AU_CONV_STR2DEC_4(__DATE__, 7)
// Days in February
2023-05-02 19:21:33 +00:00
#define __AU_UNIX_TIMESTAMP_FDAY(year) \
(((year) % 400) == 0UL ? 29UL : \
(((year) % 100) == 0UL ? 28UL : \
(((year) % 4) == 0UL ? 29UL : \
28UL)))
// Days in the year
2023-05-02 19:21:33 +00:00
#define __AU_UNIX_TIMESTAMP_YDAY(year, month, day) \
( \
/* January */ day \
/* February */ + (month >= 2 ? 31UL : 0UL) \
2023-05-02 19:21:33 +00:00
/* March */ + (month >= 3 ? __AU_UNIX_TIMESTAMP_FDAY(year) : 0UL) \
/* April */ + (month >= 4 ? 31UL : 0UL) \
/* May */ + (month >= 5 ? 30UL : 0UL) \
/* June */ + (month >= 6 ? 31UL : 0UL) \
/* July */ + (month >= 7 ? 30UL : 0UL) \
/* August */ + (month >= 8 ? 31UL : 0UL) \
/* September */+ (month >= 9 ? 31UL : 0UL) \
/* October */ + (month >= 10 ? 30UL : 0UL) \
/* November */ + (month >= 11 ? 31UL : 0UL) \
/* December */ + (month >= 12 ? 30UL : 0UL) \
)
// get the UNIX timestamp from a digits representation
2023-05-02 19:21:33 +00:00
#define __AU_UNIX_TIMESTAMP(year, month, day, hour, minute, second) \
( /* time */ second \
2023-05-02 19:21:33 +00:00
+ minute *__AU_SEC_PER_MIN \
+ hour *__AU_SEC_PER_HOUR \
+ /* year day (month + day) */ (__AU_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) *__AU_SEC_PER_DAY \
+ /* year */ (year - 1970UL) *__AU_SEC_PER_YEAR \
+ ((year - 1969UL) / 4UL) *__AU_SEC_PER_DAY \
- ((year - 1901UL) / 100UL) *__AU_SEC_PER_DAY \
+ ((year - 1601UL) / 400UL) *__AU_SEC_PER_DAY \
)
static constexpr AuUInt64 AuGetBuildTimeUnix()
{
2023-05-02 19:21:33 +00:00
return AuUInt64(__AU_UNIX_TIMESTAMP(__AU_TIME_YEARS__, __AU_TIME_MONTH__, __AU_TIME_DAYS__, __AU_TIME_HOURS__, __AU_TIME_MINUTES__, __AU_TIME_SECONDS__)) * 1000ull;
}
static constexpr AuUInt64 AuGetBuildTime()
{
2023-05-02 19:21:33 +00:00
return AuGetBuildTimeUnix() - 999'080'100'000ull;
}
2023-05-02 19:21:33 +00:00
#undef __AU_TIME_SECONDS__
#undef __AU_TIME_MINUTES__
#undef __AU_TIME_HOURS__
#undef __AU_TIME_DAYS__
#undef __AU_TIME_MONTH__
#undef __AU_TIME_YEARS__
#undef __AU_GET_MONTH
#undef __AU_GET_MONTH2DAYS
#undef __AU_CONV_STR2DEC_1
#undef __AU_CONV_STR2DEC_2
#undef __AU_CONV_STR2DEC_3
#undef __AU_CONV_STR2DEC_4
#undef __AU_SEC_PER_MIN
#undef __AU_SEC_PER_HOUR
#undef __AU_SEC_PER_DAY
#undef __AU_SEC_PER_YEAR
#undef __AU_UNIX_START_YEAR
#undef __AU_UNIX_TIMESTAMP_FDAY
#undef __AU_UNIX_TIMESTAMP
#define AURORA_BUILD_TIME AuGetBuildTime()