㈠ c語言怎麼獲取時間戳
DWORD tm = ::GetTickCount();
……
#ifndef _TIME32_T_DEFINED
typedef _W64 long __time32_t; /* 32-bit time value */
#define _TIME32_T_DEFINED
#endif /* _TIME32_T_DEFINED */
#ifndef _TIME64_T_DEFINED
#if _INTEGRAL_MAX_BITS >= 64
typedef __int64 __time64_t; /* 64-bit time value */
#endif /* _INTEGRAL_MAX_BITS >= 64 */
#define _TIME64_T_DEFINED
#endif /* _TIME64_T_DEFINED */
#ifndef _TIME_T_DEFINED
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t; /* time value */
#else /* _USE_32BIT_TIME_T */
typedef __time64_t time_t; /* time value */
#endif /* _USE_32BIT_TIME_T */
#define _TIME_T_DEFINED /* avoid multiple def's of time_t */
#endif /* _TIME_T_DEFINED */
……
㈡ 問在C語言里怎麼獲取當前時間和日期
#include <time.h> 要添加這個頭文件。
time_t rawtime;
struct tm * target_time;
time ( &rawtime ); //獲取當前時間,存rawtime里
target_time = localtime ( &rawtime ); //獲取當地時間
利用struct tm,你可以按需取出年月日時分秒星期幾等數值。
---------------------
你的問題:
time_t now;
long int dt=3600; // 時間長度,秒數
now = time (NULL); //獲取當前時間
printf("%s ",ctime(&now)); //直接列印時間
now=now+dt;
printf("%s ",ctime(&now)); // 直接列印加dt後的時間
(當然,你也可以用 ctime(&now) 返回的字元串 通過 MFC 的方法顯示)
㈢ c語言如何獲取系統時間並將時間儲存在字元串里
#include
#include
void
main()
{
time_t
ltime;
struct
tm
*today;
time(
<ime
);
today
=
localtime(
<ime
);
printf("%04d-%02d-%02d
%02d:%02d:%02d\n",1900+today->tm_year,today->tm_mon+1,today->tm_mday,today->tm_hour,today->tm_min,today->tm_sec);
}
先調用time獲得當前時間,這是個從1970-1-1午夜0點開始的秒數,然後調用localtime將該時間專為本地時間就可以列印了。其中tm_year需要加上1900,tm_mon需要加上1,看printf你就明白了。
㈣ C語言中 如何獲取系統時間
#include<time.h>
int main()
{
time_t timep;
struct tm *p;
time (&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec); /*獲取當前秒*/
printf("%d ",p->tm_min); /*獲取當前分*/
printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d ",p->tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/
}
拓展資料:
C語言是一門通用計算機編程語言,廣泛應用於底層開發。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。
C語言是一門面向過程的計算機編程語言,與C++,Java等面向對象的編程語言有所不同。
其編譯器主要有Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C等。
㈤ 如何使用C語言程序獲取指定日期的UNIX時間戳
C/C++ code #include #include int main(void) { time_t tick; struct tm tm; char s[100]; tick = time(NULL); tm = *localtime(&tick); strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm); printf("%d: %s\n", (int)tick, s); return 0; }
㈥ C語言怎麼獲取今天0點的時間戳
time_t now;
time(&now);
tm *temptm;
temptm = localtime(&now);
temptm->tm_hour = 0;
temptm->tm_min =0;
temptm->tm_sec = 0;
printf("%d%d%d %d:%d:%d",
temptm->tm_year +1900,
temptm->tm_mon +1,
temptm->tm_mday,
temptm->tm_hour,
temptm->tm_min,
temptm->tm_sec
);
㈦ C語言如何調用系統時間
方法一,#include<time.h>
int main()
{
time_t timep;
struct tm *p;
time (&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec); /*獲取當前秒*/
printf("%d ",p->tm_min); /*獲取當前分*/
printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d ",p->tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/
}
方法二.#include<stdio.h>
#include<time.h>
intmain()
{
time_tt
structtm*lt;time(&t);//獲取Unix時間戳。
lt=localtime(&t);//轉為時間結構。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,
lt->tm_hour,lt->tm_min,lt->tm_sec);//輸出結果
return0;}
(7)c語言程序里時間戳獲取擴展閱讀
1、CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鍾
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數
_timeb定義在SYSTIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( &timebuffer );
㈧ 請問在C語言里怎麼獲取當前時間和日期(精確到毫秒)
你試試下面的程序:
#include
#include
#include
int
main()
{
clock_t
start,
finish;
double
elapsed_time;
start=clock();
//do
sonething
...
finish=clock();
elapsed_time
=
finish-start;
}
我在vc++
6.0下運行,可以得到以毫秒為單位的計時
㈨ 用C語言獲取本地的時區
getlocaltime裡面是沒有時區信息的。
你可以這樣:
time_ttime_utc=0;
structtm*p_tm_time;
inttime_zone=0;
p_tm_time=localtime(&time_utc);//轉成當地時間
time_zone=(p_tm_time->tm_hour>12)?(p_tm_time->tm_hour-=24):p_tm_time->tm_hour;
把0時間轉為當地時間,得到的是帶時區的結果。
㈩ 如何用C語言獲取當前系統時間
需要利用C語言的時間函數time和localtime,具體說明如下:
一、函數介面介紹:
1、time函數。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結構體,一般為長整型。
這個函數會獲取當前時間,並返回。 如果參數__timer非空,會存儲相同值到__timer指向的內存中。
time函數返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。
由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函數了。
2、localtime函數。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結構體,包含了年月日時分秒等信息。
這種結構是適合用來輸出的。
二、參考代碼:
#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//獲取Unix時間戳。
lt=localtime(&t);//轉為時間結構。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//輸出結果
return0;
}
注意事項:
struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt->tm_year+1900。