Fundamentals 8 min read

How to Embed Build Timestamps in Embedded Binaries for Reliable Version Tracking

This guide explains why embedding compile‑time information in embedded binaries is essential for version verification, demonstrates how to use __DATE__ and __TIME__ macros, and provides three practical methods—including touch commands and pre‑build actions—to ensure the timestamp updates on every build.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Embed Build Timestamps in Embedded Binaries for Reliable Version Tracking

Problem Background

In embedded development, the final binary (bin) must correspond exactly to the version that passed testing. Developers often forget to update version variables after code changes, making it impossible to tell which source revision produced a given binary. Embedding the compilation date and time in the binary provides an immutable indicator that the code was rebuilt.

Getting Date and Time

The predefined macros __DATE__ and __TIME__ expand to strings such as "Dec 27 2017" and "15:06:19". By concatenating them with a version string you can create a human‑readable build identifier:

// Example of __DATE__ string: "Dec 27 2017"
// Example of __TIME__ string: "15:06:19"
const char *BuildInfo = "Version: " VERSION " " __DATE__ " " __TIME__;

Function to Parse Build Date

The following function extracts numeric year, month, day, hour, minute and second from the macros and prints a formatted timestamp. It works on both Windows and POSIX toolchains.

unsigned int mk_Build_Date(void)
{
    int year = 0, month = 0, day = 0;
    int hour = 0, minute = 0, seconds = 0;
    char m[4] = {0};

    sscanf(__DATE__, "%3s %2d %4d", m, &day, &year);
    for (month = 0; month < 12; month++)
    {
        if (strcmp(m, short_char_months[month]) == 0)
        {
            break;
        }
    }
    sscanf(__TIME__, "%2d:%2d:%2d", &hour, &minute, &seconds);

#ifdef SHORT_DATA_CHAR__
    printf("[null]  ** Build at:\t%04u-%02u-%02u %02u:%02u:%02u
",
           year, month, day, hour, minute, seconds);
#else
    printf("[null]  ** Build at:\t%04u-%02u-%02u %02u:%02u:%02u
",
           year, month, day, hour, minute, seconds);
#endif
    DEBUG("buildDate: %s %s
", __DATE__, __TIME__);
    return 0;
}

Adding this function to the project makes the compiled binary contain the exact build time.

Ensuring Timestamp Updates on Every Build

Three practical approaches are described:

Modify the source file’s timestamp so the compiler recompiles it. On Windows with IAR you can invoke a Cygwin touch command from a pre‑build step: cmd /c "touch /cygdrive/d/test.c" Delete the object file before compilation to force a rebuild. Example pre‑build command: cmd /c "del $OBJ_DIR\test.o" Tell the compiler to always rebuild the file . MDK (Keil) provides a setting that forces recompilation of a specific source file on every build, eliminating the need for external scripts.

These methods guarantee that the __DATE__ and __TIME__ macros generate fresh values each time the project is built, even when no source changes occur.

Testing Result

After applying one of the methods, the program prints a timestamp such as 2023-07-15 14:23:08, confirming that the binary now carries the correct build time.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CembeddedVersioningBuild TimestampIARPre-build
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.