C語言取得標準的時間格式
1. 先用NTP來同步時間
#apt-get install ntp
#ntpdate time.nist.gov (這是有權威性的NTP Server,我們透過它來做同步!!)
若出現Error:the NTP socket is in use, exiting
解法:
sudo service ntp stop
再update一次:ntpdate time.nist.gov
最後再sudo service ntp start
2. 寫C程式
#include <time.h>
struct tm *Tm; //一定要加上struct
time_t end; //變數end
logfile2=fopen("/wayne_code/udp_packets_eth1.txt","a"); //開檔
time(&end); //取得現在的時間
ptrnow=localtime(&end); //get local time 轉乘local time
fprintf(logfile2, "%s\n" , asctime(ptrnow));
//transform to string (再轉成Wed Jun 26 15:29:06 2013的格式) 並寫入file中
Output格式如下:
Wed Jun 26 15:28:56 2013
Wed Jun 26 15:28:58 2013
Wed Jun 26 15:29:00 2013
Wed Jun 26 15:29:02 2013
Wed Jun 26 15:29:04 2013
參考:http://cg2010studio.wordpress.com/2011/05/05/show-time/
http://stackoverflow.com/questions/11436063/c-enum-unknown-type-name