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

时间:2010-09-09 01:45:03

标签: c time

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

7 个答案:

答案 0 :(得分:92)

使用strftime()

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

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

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

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

    return 0;
}

对于毫秒部分,请看一下这个问题。 How to measure time in milliseconds using ANSI C?

答案 1 :(得分:22)

以上答案并未完全回答这个问题(特别是毫秒级部分)。我的解决方案是在strftime之前使用gettimeofday。注意避免将millisec舍入为&#34; 1000&#34;。这是基于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;
}

答案 2 :(得分:9)

time.h定义了一个strftime函数,该函数可以使用以下内容为您提供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;
}

但是这不会给你亚秒级的分辨率,因为time_t无法提供。它输出:

2010-09-09 10:08:34.000

如果你真的受到规范的限制,并且不想要白天和小时之间的空间,只需将它从格式字符串中删除即可。

答案 3 :(得分:3)

以下代码打印精确到微秒。我们所要做的就是在gettimeofday上使用strftimetv_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;
}

答案 4 :(得分:2)

您可以使用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);

答案 5 :(得分:2)

技巧:

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

答案 6 :(得分:1)

此页面上的所有解决方案都不适用于我,我将它们混合在一起并使其与Windows和Visual Studio 2019一起使用,这是方法:

#include <Windows.h>
#include <time.h> 
#include <chrono>

static int gettimeofday(struct timeval* tp, struct timezone* tzp) {
    namespace sc = std::chrono;
    sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
    sc::seconds s = sc::duration_cast<sc::seconds>(d);
    tp->tv_sec = s.count();
    tp->tv_usec = sc::duration_cast<sc::microseconds>(d - s).count();
    return 0;
}

static char* getFormattedTime() {
    static char buffer[26];

    // For Miliseconds
    int millisec;
    struct tm* tm_info;
    struct timeval tv;

    // For Time
    time_t rawtime;
    struct tm* timeinfo;

    gettimeofday(&tv, NULL);

    millisec = lrint(tv.tv_usec / 1000.0);
    if (millisec >= 1000) 
    {
        millisec -= 1000;
        tv.tv_sec++;
    }

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", timeinfo);
    sprintf_s(buffer, 26, "%s.%03d", buffer, millisec);

    return buffer;
}

结果:

2020:08:02 06:41:59.107

2020:08:02 06:41:59.196