C语言 如何以格式打印时间:2009-08-10 18:17:54.811

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3673226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:24:54  来源:igfitidea点击:

How to print time in format: 2009‐08‐10 18:17:54.811

ctime

提问by Dominic Bou-Samra

What's the best method to print out time in C in the format 2009‐08‐10 ?18:17:54.811?

以 C 格式打印时间的最佳方法是2009‐08‐10 ?18:17:54.811什么?

回答by Hamid Nazari

Use strftime().

使用strftime()

#include <stdio.h>
#include <time.h>

int main()
{
    time_t timer;
    char buffer[26];
    struct tm* tm_info;

    timer = time(NULL);
    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    puts(buffer);

    return 0;
}

For milliseconds part, have a look at this question. How to measure time in milliseconds using ANSI C?

对于毫秒部分,看看这个问题。如何使用 ANSI C 以毫秒为单位测量时间?

回答by Chris

The above answers do not fully answer the question (specifically the millisec part). My solution to this is to use gettimeofday before strftime. Note the care to avoid rounding millisec to "1000". This is based on Hamid Nazari's answer.

以上答案并没有完全回答这个问题(特别是毫秒部分)。我对此的解决方案是在 strftime 之前使用 gettimeofday。请注意避免将毫秒舍入为“1000”。这是基于 Hamid Nazari 的回答。

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>

int main() {
  char buffer[26];
  int millisec;
  struct tm* tm_info;
  struct timeval tv;

  gettimeofday(&tv, NULL);

  millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
  if (millisec>=1000) { // Allow for rounding up to nearest second
    millisec -=1000;
    tv.tv_sec++;
  }

  tm_info = localtime(&tv.tv_sec);

  strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
  printf("%s.%03d\n", buffer, millisec);

  return 0;
}

回答by paxdiablo

time.hdefines a strftimefunction which can give you a textual representation of a time_tusing something like:

time.h定义了一个strftime函数,它可以使用以下内容为您提供 a 的文本表示time_t

#include <stdio.h>
#include <time.h>
int main (void) {
    char buff[100];
    time_t now = time (0);
    strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now));
    printf ("%s\n", buff);
    return 0;
}

but that won't give you sub-second resolution since that's not available from a time_t. It outputs:

但这不会为您提供亚秒级分辨率,因为time_t. 它输出:

2010-09-09 10:08:34.000

If you're really constrained by the specs and do not want the space between the day and hour, just remove it from the format string.

如果您真的受到规范的限制并且不希望在日期和小时之间有空格,只需将其从格式字符串中删除即可。

回答by Nataraj

Following code prints with microsecond precision. All we have to do is use gettimeofdayand strftimeon tv_secand append tv_usecto the constructed string.

以下代码以微秒精度打印。我们所要做的就是使用gettimeofdayand strftimeontv_sec并附tv_usec加到构造的字符串。

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main(void) {
    struct timeval tmnow;
    struct tm *tm;
    char buf[30], usec_buf[6];
    gettimeofday(&tmnow, NULL);
    tm = localtime(&tmnow.tv_sec);
    strftime(buf,30,"%Y:%m:%dT%H:%M:%S", tm);
    strcat(buf,".");
    sprintf(usec_buf,"%dZ",(int)tmnow.tv_usec);
    strcat(buf,usec_buf);
    printf("%s",buf);
    return 0;
}

回答by Андрей Мельников

trick:

诡计:

    int time_len = 0, n;
    struct tm *tm_info;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tm_info = localtime(&tv.tv_sec);
    time_len+=strftime(log_buff, sizeof log_buff, "%y%m%d %H:%M:%S", tm_info);
    time_len+=snprintf(log_buff+time_len,sizeof log_buff-time_len,".%03ld ",tv.tv_usec/1000);

回答by Hyman Kelly

You could use strftime, but struct tmdoesn't have resolution for parts of seconds. I'm not sure if that's absolutely required for your purposes.

您可以使用strftime,但struct tm在几秒钟内没有分辨率。我不确定这是否绝对需要您的目的。

struct tm tm;
/* Set tm to the correct time */
char s[20]; /* strlen("2009-08-10 18:17:54") + 1 */
strftime(s, 20, "%F %H:%M:%S", &tm);