Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:37:51+00:00 2026-06-03T06:37:51+00:00

I have a source file. When I compile the code, I want the executable

  • 0

I have a source file. When I compile the code, I want the executable file to remember when it was built. I am wondering if it is possible. For example:

 int main(){
    time_t t = ???  // Time when this line is compiled
    //print out value of t in certain format. 
    return t 
 } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:37:53+00:00Added an answer on June 3, 2026 at 6:37 am

    You can use the __TIME__ and __DATE__ macros to get the time the preprocessor ran at. It’s a string, so yo need to convert it to a time_t from there.

    A quick example I put together:

    #include <time.h>
    #include <iostream>
    #include <cassert>
    
    time_t build_time() {
      static const char *built = __DATE__" "__TIME__;  
      struct tm t;
      const char *ret = strptime(built, "%b %d %Y %H:%M:%S", &t);
      assert(ret);
      return mktime(&t);
    }
    
    int main() {
      std::cout << build_time() << std::endl;
    }
    

    I was a little worried about how this interacted with different locales, so I had a quick look in a recent C standard and found the following passage:

    __DATE__ The date of translation of the preprocessing translation unit: a character string literal of the form “Mmm dd yyyy”, where the
    names of the months are the same as those generated by the asctime
    function, and the first character of dd is a space character if the
    value is less than 10. If the date of translation is not available, an
    implementation-defined valid date shall be supplied.

    asctime is quite clear that:

    … The abbreviations for the months are
    “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”,
    “Nov”, and “Dec” …

    But %b of strptime() says:

    %b or %B or %h

    The month name according to the current locale, in abbreviated
    form or the full name.

    So you need to be aware that this is making an assumption about what the locale will be set to at run time.

    (You could in theory write a constexpr function or two to do that at compile time in C++11, but that’s non-trivial to say the least!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .pyc file with no corresponding Python source code. I want to
I have a simple C program (one source file) which I want to compile
Let's say I have a source file with many preprocessor directives. Is it possible
I have a line number of a Java source file and want to get
I have a file Employee.as with the following source code. I am unable to
I have a project on Lazarus that I want to compile a source using
I have many source/text file, say file.cpp or file.txt . Now, I want to
I have an application. I have the source code (in C). I can compile
I have a .cpp source file that contains both C and C++ code. The
Possible Duplicate: Compiling C code for .NET I have a bunch of C source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.