Как лучше всего распечатать время на C в формате 2009‐08‐10
18:17:54.811?
Как лучше всего распечатать время на C в формате 2009‐08‐10
18:17:54.811?
Ответы:
Используйте 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;
}
Что касается миллисекунд, взгляните на этот вопрос. Как измерить время в миллисекундах с помощью ANSI C?
time(&timer)скорее должна быть timer = time(NULL);, по крайней мере, для Linux. The tloc argument is obsolescent and should always be NULL in new code. When tloc is NULL, the call cannot fail.
Приведенные выше ответы не полностью отвечают на вопрос (в частности, на миллисекундную часть). Мое решение - использовать gettimeofday до strftime. Обратите внимание на осторожность, чтобы не округлять миллисекунды до «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;
}
gettimeofdayнедоступно для реализаций Windows
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
Если вы действительно ограничены спецификациями и не хотите промежуток между днем и часом, просто удалите его из строки формата.
Следующий код печатается с точностью до микросекунд. Все , что нужно сделать , это использовать gettimeofdayи strftimeна tv_secи Append 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;
}
Вы можете использовать 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);
трюк:
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);
Ни одно из решений на этой странице у меня не сработало, я перепутал их и заставил работать с 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