Get the current system time with C under Linux

  
 

#include <time.h> time_t time(time_t calptr); Returns the calendar time, which is the number of seconds since 00:00:00 on January 1, 1970, International Standard Time. Then call char *ctime(const time_t calptr) ; convert to a string representation

#include <stdio.h> #include <time.h> int main () { time_t timep; time (& ;timep); printf( "%s ",ctime(&timep)); }


Use localtime to directly decompose the year, month, day, hour, minute, and second: struct tm *ptm ; long ts; int y,m,d,h,n,s;

ts = time(NULL); ptm = localtime(&ts);

y = ptm-> ; tm_year+1900; //year m = ptm-> tm_mon+1; //month d = ptm-> tm_mday; //day h = ptm-> tm_hour; //time n = ptm-> tm_min ; //minutes s = ptm-> tm_sec; //second

Copyright © Windows knowledge All Rights Reserved