显示当前时间的N种方法
法一:
#include <time.h>
#include <stdio.h>
int main() {
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
printf(“%s”, asctime(timenow));
return 0;
}
法二:
#include <time.h>
#include <stdio.h>
int main() {
char timebuf[100];
time_t t;
time (&t);
strftime(timebuf, sizeof(timebuf),
“%Y 年 %m 月 %d 日 %H:%M:%S”, localtime(&t));
printf(“%sn”, timebuf);
return 0;
}
法三:
#include <time.h>
#include <stdio.h>
int main() {
time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(<)); /*english format output*/
printf(asctime(localtime(<)));/*tranfer to tm*/
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
return 0;
}
法四:
#include <time.h>
#include <stdio.h>
int main() {
time_t sec = time(NULL);
struct tm t = *localtime(&sec);
printf( “%02d:%02d:%02dn “, t.tm_hour, t.tm_min, t.tm_sec);
return 0;
}
法五:
#include <time.h>
#include <stdio.h>
int main() {
struct tm *newtime;
long ltime;
time(<ime);
/* Obtain coordinated universal time*/
newtime = gmtime(<ime);
printf(“Coordinated universal time is %sn “, asctime(newtime));
return 0;
}