c語言time(0)轉換為當前時間
阿新 • • 發佈:2019-01-22
時間戳是指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總秒數
在C語言中可以通過time()函式獲取到當前的秒數 引數為0則函式返回值即為結果,若引數不為0則結果儲存在引數中
#include <time.h>
time_t time( time_t *time );
#include<time.h> #include<stdio.h> #include<windows.h> int main() { int h=0,m=0,s=0; int t=0; while(1) { t=time(0); //獲取總秒數 s=t%60; m=t%3600/60; //1h= 3600s不足1h的除60即為分鐘 h=(t%(24*3600)/3600+8)%24; //1天24h 得到當天小時數+8為東八區區時 避免出現大於24h的情況對24取餘 printf("%02d:%02d:%02d\r",h,m,s); // %02d是將數字按寬度為2,採用右對齊方式輸出,若資料位數不到2位,則左邊補0 \r到當前行最左 //while(t==time(0)); Sleep(1000);//程式執行較快 對時間進行控制 執行掛起1s } return 0; }