Jidai – compilation time stamps

Jidai is a miniature single header library for generating compilation time stamps from the standard preprocessor defines __DATE__ and __TIME__.

Since those defines generate string literals the code includes constexpr versions of std::atoi and std::strcmp to convert the strings to integers.

The lib comes with two different functions.

One which returns the compilation time as plain struct with integer values for date and time.

constexpr CompilationTime make_compilation_time();

struct CompilationTime {
  uint8_t day{};
  uint8_t month{};
  uint16_t year{};
  uint8_t hour{};
  uint8_t minute{};
  uint8_t second{};
};

And one which returns a unix time stamp since an epoch passed in. The default here is the classic unix epoch 1/1/1970.

inline constexpr CompilationTime default_epoch{.day = 1, .month = 1, .year = 1970};

template<typename T = time_t>
constexpr T make_unix_compilation_time(CompilationTime const epoch = default_epoch);

The src/test folder contains a little test case which i checked against the unix time stamp converter on unixtimestamp.com.

#include <iostream>
#include "jidai.hpp"

int main() {
  constexpr auto ct{jidai::make_compilation_time()};
  constexpr auto ut{jidai::make_unix_compilation_time()};

  std::cout << "Test compiled at " << (int)ct.day << "/" 
            << (int)ct.month << "/" << (int)ct.year << " " 
            << (int)ct.hour << ":" << (int)ct.minute << ":" 
            << (int)ct.second << " which equals " << ut 
            << "seconds since 1/1/1970.\n";
}

This outputs:

Test compiled at 25/11/2018 14:37:52 which equals 1543156672 seconds since 1/1/1970.

Which is equal to what the online converter says: