【unix时间戳小示例】linux/unix系统获取unix时间戳
生活随笔
收集整理的這篇文章主要介紹了
【unix时间戳小示例】linux/unix系统获取unix时间戳
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
unix時間戳
unix時間戳是從1970年1月1日(UTC/GMT的午夜(0時0點0分))開始所經過的秒數,不考慮閏秒。
C 庫函數 - gmtime()
C 庫函數 struct tm *gmtime(const time_t *timer) 使用 timer 的值來填充 tm 結構,并用協調世界時(UTC)也被稱為格林尼治標準時間(GMT)表示。
C 庫函數 - mktime()
C 庫函數 time_t mktime(struct tm *timeptr) 把 timeptr 所指向的結構轉換為自 1970 年 1 月 1 日以來持續時間的秒數,發生錯誤時返回-1。
demo
#include <stdio.h> #include <time.h>int main () {time_t rawtime;struct tm *pttm;time(&rawtime);pttm = gmtime(&rawtime ); // 獲取CST/GMT時間printf("timestamp:%ld\n", mktime(pttm));return(0); }打印信息:
timestamp:1640013995引經據典
https://www.runoob.com/cprogramming/c-function-mktime.html
https://baike.baidu.com/item/unix%E6%97%B6%E9%97%B4%E6%88%B3/2078227?fr=aladdin
https://www.runoob.com/cprogramming/c-function-gmtime.html
總結
以上是生活随笔為你收集整理的【unix时间戳小示例】linux/unix系统获取unix时间戳的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021方便速食行业洞察报告
- 下一篇: 【快乐水题】686. 重复叠加字符串匹配