录音的时候,你编译源 [英] Recording the time when you compile a source

查看:126
本文介绍了录音的时候,你编译源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源文件。当我编译code,我想的可执行文件记住当它被建立。我想知道,如果它是可能的。例如:

  INT的main(){
    time_t的T =? //时值这一行编译
    //打印出的t值一定的格式。
    返回ŧ
 }


解决方案

您可以使用 __ TIME __ __ DATE __ 宏以获得时preprocessor跑了。这是一个字符串,所以哟需要<一个href=\"http://stackoverflow.com/questions/321793/date-time-conversion-string-re$p$psentation-to-time-t\">convert到一个 time_t的 从那里。

一个简单的例子我放在一起:

 的#include&LT;&time.h中GT;
#包括LT&;&iostream的GT;
#包括LT&;&了cassert GT;time_t的build_time(){
  静态为const char *内置= __DATE____TIME__;
  结构TM吨;
  为const char * RET = strptime(建%B%D%Y%H:%M:%S,&amp; T公司);
  断言(RET);
  返回mktime(&amp; T公司);
}诠释主(){
  性病::法院LT&;&LT; build_time()&所述;&下;的std :: ENDL;
}

我是有点担心如何互动与不同的语言环境,所以我不得不在最近的C标准咋一看,发现下面这段文字:


  

__ __ DATE 的preprocessing翻译单元的翻译日期:字符串文字的形式嗯月日年,其中​​的
  月份的名称相同所产生的那些 asctime
  功能和dd的第一个字符是空格字符,如果
  值小于10。如果没有转换的日期不可用,一个
  实现定义的有效日期应提供。


asctime 是相当明确表示:


  

...换了个的缩写是
         月,月,月,月,月,君,月,月,月,十月,
         月和月...


%B 的strptime()说:


  

%B或%B或%H


  
  

根据当前区域月份名称后,在略
     形式或全名。


所以,你需要注意,这是使什么的语言环境会在运行时被设定为一个假设。

(理论上你可以写一个 constexpr 函数或两个这样做,在C ++ 11的编译时间,但是这是不平凡的,至少可以说!)

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 
 } 

解决方案

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!)

这篇关于录音的时候,你编译源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆