C 時間戳 時間相關函式

2021-09-12 12:33:39 字數 1394 閱讀 6753

【時間戳】unix時間戳(unix timestamp),或稱unix時間(unix time)、posix時間(posix time),是一種時間表示方式,定義為從格林威治時間2023年01月01日00時00分00秒起至現在的總秒數

1. 函式模型

/* return the current time and put it in *timer if timer is not null.  */

extern time_t time (time_t *__timer) __throw;

2. 函式標頭檔案

#include

3. 獲取時間戳

time_ m_time = time(null);

或者time_t  m_time;

time(&m_time);

4. 時間塊結構體tm包括了年、月、日、時、分、秒等變數

struct tm ;

5. 把時間戳轉化成時間結構體(localtime()函式是將時間轉化為本地時間。)

struct tm * localtime(const time_t * timer);

struct tm * gmtime(const time_t *timer); //將時間戳轉化為世界標準時間(即格林尼治時間)比北京時間晚8個小時

6. 時間轉為字串

char * ctime(const time_t *timer);//固定格式

size_t strftime(char *strdest,size_t maxsize,const char *format,

const struct tm *timeptr

);//自定義格式

例:structtm*ptr;

time_ttimestamp;

charstr[80];

timestamp=time(null);

ptr =localtime(timestamp);

strftime(str, 100,"%y年%m月%d日 %h:%m:%s", ptr);

printf(str);//2023年06月04日 15:45:28秒

時間戳 相關問題

遇到的問題 資料庫中的時間定義的是timestamp 時間戳型別 在從資料庫中獲取該時間字段 通過model.addattribute 物件名 獲取的物件 傳遞到前台顯示後會出現 例子 1993 01 11 11 33 33.0 多出乙個.0資訊 目前我的解決辦法就是將該物件的時間在後台重新格式化後...

PHP 時間戳及時間函式

一 php 時間戳 php 時間戳 unix 時間戳 timestamp 是 php 中關於時間日期乙個很重要的概念,它表示從 1970年1月1日 00 00 00 到當前時間的秒數之和。php提供了內建函式 time 來取得伺服器當前時間的時間戳。時間差時間戳雖然看起來好像不太直觀,但我們在對時間...

C 獲取時間戳,時間戳與時間格式互轉

最近專案中用到獲取時間的功能,現在把用到的分享一下。1.時間戳轉為 00 00時區 日期格式 yyyy mm dd hh mm ss 時間戳轉為 00 00時區 日期格式 yyyy mm dd hh mm ss public static datetime getutcdatetime string...