C++;时间戳。更新输出 我正在用C++编写一个程序,我希望在程序执行的开始和结束时打印时间。我在main()中使用了以下代码在开始时输出时间戳,但在程序结束时不会更新这些值

C++;时间戳。更新输出 我正在用C++编写一个程序,我希望在程序执行的开始和结束时打印时间。我在main()中使用了以下代码在开始时输出时间戳,但在程序结束时不会更新这些值,c++,C++,我目前正在按程序工作,但我想也许函数会对我有好处 int main(int argc, char **argv) { time_t now; struct tm *current; now = time(0); current = localtime(&now); cout <<"Examination began at: " << current->tm_hour <<":" << current->tm_

我目前正在按程序工作,但我想也许函数会对我有好处

int main(int argc, char **argv) {
  time_t now;
  struct tm *current;
  now = time(0);
  current = localtime(&now);
  cout <<"Examination began at: " << current->tm_hour <<":" << current->tm_min <<":" << current->tm_sec << endl;

  //program execution....

  cout <<"Examination ended at: " << current->tm_hour <<":" << current->tm_min <<":" << current->tm_sec << endl;

  cout << "PROGRAM END++++++++++++++++++++++++++++++++++" << endl;

  return 0;
}
int main(int argc,char**argv){
现在是时候了;
结构tm*电流;
现在=时间(0);
当前=本地时间(&now);

cout仅通过调用localtime()设置“current”time函数的值。在程序开始和结束时看到相同值的原因是您只调用了该函数一次。将“now”的值重置为时间(0),然后
“当前”设置为localtime的值(&now)程序执行后,您将看到所需的更新。

您可以通过使用诸如
clock
getrusage
times
之类的函数来获得更好的结果。阅读这些函数。

要获取退出时的时间,只需重复调用
time
localtime

int main(int argc, char **argv) {
  time_t now;
  struct tm *current;
  now = time(0);
  current = localtime(&now);
  cout <<"Examination began at: " << current->tm_hour <<":" << current->tm_min <<":" << current->tm_sec << endl;

  //program execution....

  now = time(0);
  current = localtime(&now);
  cout <<"Examination ended at: " << current->tm_hour <<":" << current->tm_min <<":" << current->tm_sec << endl;
  cout << "PROGRAM END++++++++++++++++++++++++++++++++++" << endl;

  return 0;
}
int main(int argc,char**argv){
现在是时候了;
结构tm*电流;
现在=时间(0);
当前=本地时间(&now);

谢谢,我看了。我发现在C++中找到日期/时间的信息是多么困难(IM移动java),它看起来像是简单函数的复杂分配,我读的大部分似乎没有提供任何答案。如果时间允许,我会查看时钟和时间来提高分辨率。
void write_timestamp(std::ostream& o, const char *when) {
  time_t now;
  struct tm *current;
  now = time(0);
  current = localtime(&now);
  o << when << ": " << current->tm_hour <<":" << current->tm_min <<":" << current->tm_sec << endl;
}

int main(int argc, char **argv) {
  write_timestamp(cout, "Examination began at");

  //program execution....

  write_timestamp(cout, "Examination ended at");
  cout << "PROGRAM END++++++++++++++++++++++++++++++++++" << endl;
  return 0;
}