獲得毫秒級別的當前時間


ftime()函數取得目前的時間和日期。

相關函數:time, ctime, gettimeofday
表頭文件:#include <sys/timeb.h>
函數定義:int ftime(struct timeb *tp);
函數說明:ftime()將目前日期由tp所指的結構返回。tp結構定義:

struct   timeb{
       time_t   time;                      /* 為1970-01-01至今的秒數*/
       unsigned   short   millitm;   /* 千分之一秒即毫秒 */
       short   timezonel;               /* 為目前時區和Greenwich相差的時間,單位為分鍾 */
       short   dstflag;                   /* 為日光節約時間的修正狀態,如果為非0代表啟用日光節約時間修正 */
};

示例:

#include <sys/timeb.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>

main()
{
      struct timeb tp;
      char ti[32];
      ftime(&tp);
      printf("%lld\n",tp.time);/*UTC時間1970到現在的秒數*/
      strftime(ti, sizeof(ti), "%Y-%m-%d %H:%M:%S", localtime(&tp.time));
      printf("%s %03u\n", ti,tp.millitm);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM