อะไรคือวิธีที่ดีที่สุดที่จะพิมพ์ออกเวลาใน 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 สังเกตการดูแลเพื่อหลีกเลี่ยงการปัดเศษมิลลิวินาทีเป็น "1,000" นี่เป็นไปตามคำตอบของ 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;
}
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
และผนวก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