programing

시각 인쇄 방법: 2009 08 08 10 10 18 : 17 : 54 . 811

bestcode 2022. 8. 14. 12:03
반응형

시각 인쇄 방법: 2009 08 08 10 10 18 : 17 : 54 . 811

시간을 C로 인쇄하는 가장 좋은 방법은 무엇입니까?2009‐08‐10 
18:17:54.811?

위의 답변은 질문에 완전히 답하지 않습니다(특히 밀리섹 부품).이에 대한 나의 해결책은 strftime 전에 get of day를 사용하는 것입니다.밀리섹을 "1000"으로 반올림하지 않도록 주의하십시오.이것은 하미드 나자리의 대답에 바탕을 두고 있다.

#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;
}

를 사용합니다.

#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;
}

밀리초 단위의 부품에 대해서는, 이 질문을 참조해당 질문은 다음과 같습니다.ANSI C를 사용하여 밀리초 단위로 시간을 측정하는 방법은 무엇입니까?

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

단, 1초 미만의 해상도는 얻을 수 없습니다.time_t출력:

2010-09-09 10:08:34.000

사양에 얽매여 낮과 시간 사이에 공백이 필요 없는 경우 형식 문자열에서 삭제하기만 하면 됩니다.

다음의 코드는 마이크로초의 정밀도로 인쇄됩니다.우리가 해야 할 일은 단지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;
}

트릭:

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

사용할 수 있습니다.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);

이 페이지의 솔루션 중 어느 것도 효과가 없었습니다.이것들을 섞어서 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

언급URL : https://stackoverflow.com/questions/3673226/how-to-print-time-in-format-2009-08-10-181754-811

반응형