C 語言的標準時間封裝

2021-10-22 05:01:12 字數 2433 閱讀 8362

c語言處理時間有很多函式和結構體型別完成,並且涉及到時間戳,標準時間,本地時間的轉換。學習起來感覺頭大,外加上小寫並且簡寫函式名稱,就難以理解。

**中解釋

#include #include #include #include typedef time_t timestamp; //舊的時間戳型別 給他其個新名字

typedef struct tm sctime; //舊的時間結構 臨時改個名 或許也不需要typedef struct datetime //新的時間結構體型別

tdatetime;

typedef struct  //新的時間型別 除了秒外的

time;

timestamp gettimestamp( ) //獲得時間戳

timestamp stamp;

time (&stamp);     //這個函式在c裡面就是獲得時間戳的

return stamp;

};time gettime() //返回秒後面的更小的精度;

tdatetime timestamptodatetime(timestamp *ptimestamp) //時間戳轉日期格式;

tdatetime getdatetimeutc() //獲得utc日期格式;

tdatetime getdatetimelocal(int timeextent) //獲得本地日期格式;

datetimetostr(const tdatetime datetime,char *pstrdatetime) //日期轉字串

;    char lctemp[4] =;

char lcseparator1[2] = ; //分隔符必須是字串 後面要跟\0

char lcseparator2[2] = ; //用字元變數無法使用strcat()

char lcseparator3[2] = ;

itoa(datetime.year,lctemp,10);   //整數轉字串以10進製表現

strcat(strdatetime,lctemp); //字元拼接函式

strcat(strdatetime,lcseparator1);

if (datetime.month < 10 ) //個位數前面置0

itoa(datetime.month,lctemp,10);

strcat(strdatetime,lctemp);

strcat(strdatetime,lcseparator1);

if(datetime.day < 10)

itoa(datetime.day,lctemp,10);

strcat(strdatetime,lctemp);

strcat(strdatetime,lcseparator2);

if(datetime.hour < 10)

itoa(datetime.hour,lctemp,10);

strcat(strdatetime,lctemp);

strcat(strdatetime,lcseparator3);

if(datetime.minute < 10)

itoa(datetime.minute,lctemp,10);

strcat(strdatetime,lctemp);

strcat(strdatetime,lcseparator3);

if(datetime.second < 10)

itoa(datetime.second,lctemp,10);

strcat(strdatetime,lctemp);

int i=0;

for ( i = 0 ;i < 25; i++) //迴圈賦值給返回變數

(pstrdatetime[i]=strdatetime[i]

);};int main()

當然**寫得不夠高效,大量使用值傳和本地變數。

utc timestamp :1615730198

utc datetime  2021-3-14 13:56:38

local datetime str:2021-03-14 21:56:38

the time ismillisecond.microsecond.nanosecond:171.678.500

how day of year today? 73

how week today ? 0

--------------------------------

process exited after 0.01856 seconds with return value 0

請按任意鍵繼續. . .

UTC標準時間

世界的每個地區都有自己的本地時間,在internet及無線電通訊時,時間的統一非常重要!整個地球分為二十四時區,每個時區都有自己的本地時間。在國際無線電通訊中,為統一而普遍使用乙個標準時間,稱為通用協調時 utc,universal time coordinated utc與格林尼治平均時 gmt,...

世界標準時間(UTC) 常識

世界標準時間 utc,coordinated universal time 是當今民用時間的基礎。它使用一天 24 小時時間制,並結合了地球的自轉時間與原子鐘的高精度度量。utc 由兩部分構成 世界時間 ut 產生於1884年的華盛頓子午線會議。它是如今我們使用的24小時時區系統的基礎。那時,格林威...

ISO8601標準時間格式

國際標準化組織的國際標準iso8601是日期和時間的表示方法,全稱為 資料儲存和交換形式 資訊交換 日期和時間的表示方法 目前是第二版iso8601 2000以替代第一版iso8601 1988。日期表示法 年由4位數組成,以公曆公元1年為0001年,以西元前1年為0000年,西元前2年為 0001...