① c語言時間函數time_t
1、time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,time.h 定義如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( &rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime ( &rawtime ); //轉為當地時間,tm 時間結構
asctime() // 轉為標准ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1
2、time函數使用示例
#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));
return0;
}
② C語言 time()
頭文件time.h
@函數名稱: localtime
函數原型: struct tm *localtime(const time_t *timer)
函數功能: 返回一個以tm結構表達的機器時間信息
函數返回: 以tm結構表達的時間,結構tm定義如下:
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
參數說明: timer-使用time()函數獲得的機器時間
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}
@函數名稱: asctime
函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換為ASCII碼)
函數返回: 返回的時間字元串格式為:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到
所屬文件: <time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}
@函數名稱: ctime
函數原型: char *ctime(long time)
函數功能: 得到日歷時間
函數返回: 返回字元串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數time獲得
所屬文件: <time.h>
#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}
@函數名稱: difftime
函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位為秒
函數返回: 時間差,單位為秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
所屬文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}
@函數名稱: gmtime
函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息
所屬文件: <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}
@函數名稱: time
函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日歷時間或者設置日歷時間
函數返回: 機器日歷時間
參數說明: timer=NULL時得到機器日歷時間,timer=時間數值時,用於設置日歷時間,time_t是一個long類型
所屬文件: <time.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}
@函數名稱: tzset
函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
所屬文件: <time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}
③ C語言問題
1. sys/types.h
#define __need_timer_t
#define __need_clockid_t
#include <time.h>
2.time.h
typedef __time_t time_t;
# include <bits/types.h> /* This defines __time_t for us. */
3.bits/types.h
__STD_TYPE __TIME_T_TYPE __time_t; /* Seconds since the Epoch. */
# define __STD_TYPE __extension__ typedef
4.bits/typesizes.h
#define __TIME_T_TYPE __SLONGWORD_TYPE
5. bits/types.h
#define __SLONGWORD_TYPE long int
這里,基本就可以得出結論了:
__extension__ typedef long int time_t
則time_t類型的變數最大值為0x7fffffff
網上當來的,其實不必在意這些細節,記住是一個無符號長整形就可以,通俗點就是很大的一個正整數。表示從1970年1 月1日0點0分0秒到某一時刻的秒數即可。
④ C語言編寫 輸入 年 月 日 ,輸出這個日期的下一天的日期
利用C標准函數time,localtime,mktime來實現
先將輸入的年月日放到tm結構中
用mktime()函數生成一個整數
用這個整數+86400(24*60*60)
然後再轉換成日期就OK了
⑤ c語言time_t ,tm都是些什麼類型
下面是粘貼的哈,將就著看,time_t ---long tm是結構體。
typedef __kernel_time_t time_t;
typedef long __kernel_time_t;
struct tm {
164 /*
165 * the number of seconds after the minute, normally in the range
166 * 0 to 59, but can be up to 60 to allow for leap seconds
167 */
168 int tm_sec;
169 /* the number of minutes after the hour, in the range 0 to 59*/
170 int tm_min;
171 /* the number of hours past midnight, in the range 0 to 23 */
172 int tm_hour;
173 /* the day of the month, in the range 1 to 31 */
174 int tm_mday;
175 /* the number of months since January, in the range 0 to 11 */
176 int tm_mon;
177 /* the number of years since 1900 */
178 long tm_year;
179 /* the number of days since Sunday, in the range 0 to 6 */
180 int tm_wday;
181 /* the number of days since January 1, in the range 0 to 365 */
182 int tm_yday;
183 }
⑥ 如何用C語言編寫一個顯示時間的函數,要求時間顯示精度到毫秒級別。
#include <cstdio>
#include <ctime>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printTime() {
struct tm t; //tm結構指針
time_t now; //聲明time_t類型變數
time(&now); //獲取系統日期和時間
localtime_s(&t, &now); //獲取當地日期和時間
//格式化輸出本地時間
printf("年-月-日-時-分-秒:%d-%d-%d %d:%d:%d ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
}
int main(int argc, char** argv) {
printTime();
}
⑦ c語言程序解答(在線等)
根據題意:
1、項目序號應為唯一值,用自增變數填充。
2、時間使用struct tm結構體(考慮如需時間運算,可使用相關函數)。
3、自定義結構類型SIINFO,分別實現插入鏈表和列印鏈表兩個功能。
4、由於這個演示程序執行完就結束程序了。所以鏈表我沒有寫free釋放內存,如你後期擴充代碼,自己寫釋放(除程序結束,malloc申請內存不會自動釋放)。
下面是演示代碼:
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#define MS 4//最大類型個數
#define MN 20//名稱最大字元長度
char stypes[MS][10]={"速度型","力量型","耐力型","對抗型"};//項目類別,自行擴展,對應修改MS常量
typedef struct SportsItemInfo//定義一個體育項目結構類型
{
int id;//項目序號,從數據結構考慮,該項不能重復,應設為自增從0開始(實際開發,這個值由資料庫分配)。
char name[MN+1];//項目名稱
int stype;//項目類(對應stypes行下標)
int n;//參賽人數
struct tm sDate;//競賽時間
struct SportsItemInfo *next;
}SIINFO;
SIINFO *insert2List(SIINFO *p_sHead);//插入新數據,首次插入參數傳NULL。參數:鏈表頭節點地址(不是首節點)。成功返回頭節點,失敗返回NULL。
void selectFList(SIINFO *p_sHead);//查詢鏈表
int main()
{
char c;
SIINFO *p_sHead=NULL,*stemp=NULL;
printf("新增體育項目: ");
while(1)
{
stemp=insert2List(p_sHead);
if(!stemp)
{
printf("ERROR! ");
return 1;
}
p_sHead=stemp;
printf("是否繼續輸入(Y/N):");
c=0;
while(c!='Y' && c!='N')scanf("%c",&c);
if(c=='N') break;
}
selectFList(p_sHead);
return 0;
}
void selectFList(SIINFO *pht)
{
int i=0;
if(pht)
{
printf(" 輸出鏈表信息: ");
while(pht->next)
{
printf("-----------項目%d--------- ",++i);
printf("項目序號:%d ",pht->next->id);
printf("項目名稱:%s ",pht->next->name);
printf("項目類別:%s ",stypes[pht->next->stype]);
printf("參賽人數:%d ",pht->next->n);
printf("參賽時間:%04d-%02d-%02d %02d:%02d:%02d ",pht->next->sDate.tm_year+1900,pht->next->sDate.tm_mon+1,pht->next->sDate.tm_mday,pht->next->sDate.tm_hour,pht->next->sDate.tm_min,pht->next->sDate.tm_sec);
printf("-------------------------- ");
pht=pht->next;
}
}
}
SIINFO *insert2List(SIINFO *p_sHead)
{
static int x=0;
static SIINFO *p_sTail=NULL;
int i;
SIINFO *p_new=NULL;
if(!p_sHead){
p_sHead=(SIINFO*)malloc(sizeof(SIINFO));
if(!p_sHead)
return NULL;
p_sHead->next=NULL;
p_sTail=NULL;
}
p_new=(SIINFO*)malloc(sizeof(SIINFO));
if(!p_new)
return NULL;
p_new->next=NULL;
p_new->id=x++;
printf("-------------------------- ");
printf("項目名稱:"),scanf("%s",p_new->name);
for(i=0,printf("項目類(");i<MS-1;printf("%d、%s,",i,stypes[i]),i++);
printf("%d、%s):",i,stypes[i]);
p_new->stype=-1;
while(p_new->stype<0 || p_new->stype>MS-1)scanf("%d",&p_new->stype);
printf("參賽人數:"),scanf("%d",&p_new->n);
printf("參賽時間(輸入格式:年-月-日 時:分:秒):");
scanf("%d-%d-%d %d:%d:%d",&p_new->sDate.tm_year,&p_new->sDate.tm_mon,&p_new->sDate.tm_mday,&p_new->sDate.tm_hour,&p_new->sDate.tm_min,&p_new->sDate.tm_sec);
p_new->sDate.tm_mon--;//tm結構的月份是從0開始對應1月
p_new->sDate.tm_year=p_new->sDate.tm_year-1900;//tm結構的年份是實際年份-1900
if(!p_sHead->next)
p_sHead->next=p_new;
else
p_sTail->next=p_new;
p_sTail=p_new;
printf("-------------------------- ");
return p_sHead;
}
⑧ C語言中的時間
以前實際上用過,很想對C語言中的時間函數了解多一點,趁著這個寒假,查了些資料,大概把我現在能用到的關於時間的操作在此記錄下來。通過幾個函數來熟悉C語言中對時間的操作。(註:以下程序均在VS2010上編譯通過。)①time()函數。可以通過time()函數來獲得日歷時間。其原型為: time_t time(time_t *timer);一般參數為空,返回值類型time_t是一個長整型數,函數將返回現在的日歷時間,即從一個時間點(所有不同版本的Visual C++都是從1970年1月1日0時0分0秒)到現在的經過的秒數。例子程序:#include<stdio.h>#include<time.h>void main(){ time_t lt; lt=time(NULL); printf("1970年1月1日0時0分0秒到現在經歷了%ld秒%A",lt);}運行結果(結果與程序運行的時間有關,貼出我此時運行出的結果):1970年1月1日0時0分0秒到現在經歷了1326975564秒請按任意鍵繼續. . .②clock()函數。C語言中的計時函數。函數原型為: clock_t clock(void);clock()函數返回從「開啟這個程序進程\」到「程序中調用clock()函數」時之間的CPU時鍾計時單元數。返回值類型clock_t也是一個長整型數。在time.h頭文件中定義了一個符號常量CLOCKS_PER_SEC,表示一秒鍾會有多少個計時單元。所以通過簡單的數學知識,可以知道在程序中用clock()/CLOCKS_PER_SEC來表示程序從開始到調用clock()函數時用了多少秒。例子程序:#include<stdio.h>#include<time.h>void main(){ clock_t lt; for(int i=0;i<1000000000;i++); lt=clock(); printf("循環執行1000000000個空操作需要%f秒%A",(double)lt/CLOCKS_PER_SEC);}運行結果(在不同的機器上運行的結果可能不一樣,下面是在我自己的筆記本上運行的結果):循環執行1000000000個空操作需要3.484000秒請按任意鍵繼續. . .③使用C庫函數來顯示日期和時間。首先要介紹一下C語言中的一個日期的結構體類型,tm類型。其在time.h中的定義如下:#ifndef _TM_DEFINEDstruct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; };#define _TM_DEFINED#endif然後可以介紹有關的函數了。time.h提供了兩種不同的函數將日歷時間(一個長整型數)轉換成我們平時看到的把年月日時分秒分開的時間格式: struct tm *gmtime(const time_t *timer); struct tm *localtime(const time_t *timer);其中gmtime()函數是將日歷時間轉換為世界標准時間(即格林尼治時間),並返回一個tm結構體來保存這個時間,而localtime()函數是將日歷時間轉換為本地時間(在中國地區獲得的本地時間會比世界標准時間晚8個小時)。例子程序:#include<stdio.h>#include<time.h>void main(){ struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf("現在北京時間是%d點%A",local->tm_hour); local=gmtime(&t); printf("世界標准時間是%d點%A",local->tm_hour);}運行結果(運行結果與運行的時間有關,我是在早上9點多鍾運行這個程序的):現在北京時間是9點世界標准時間是1點請按任意鍵繼續. . .這樣子我們就可以完全才輸出此刻的年月日時分秒了,當然需要逐個來輸出。其實C庫函數還提供了一個很有用的以固定的時間格式來輸出年月日時分秒的函數。這兩個函數原型如下: char *asctime(const struct tm *timeptr); char *ctime(const time_t *timer);asctime()函數是通過tm結構來生成具有固定格式的保存時間信息的字元串,而ctime()是通過日歷時間來生成時間字元串。這樣下面的例子程序就容易理解了:#include<stdio.h>#include<time.h>void main(){ struct tm *local; time_t t; t=time(NULL); local=localtime(&t); printf(asctime(local)); local=gmtime(&t); printf(asctime(local)); printf(ctime(&t));}運行結果(我是在早上9點多運行這個程序的):Fri Jan 20 09:55:56 2012Fri Jan 20 01:55:56 2012Fri Jan 20 09:55:56 2012請按任意鍵繼續. . .C語言還可以以我們規定的各種形式來規定輸出時間的格式。要用到時可以查閱相關的資料,限於篇幅,就介紹到這里。④這里介紹計算持續的時間長度的函數。之前已經介紹了使用clock()函數的例子,它可以精確到毫秒級。其實我們也可以使用difftime()函數,但它只精確到秒。該函數的定義如下: double difftime(time_t time1,time_t time0);例子程序:#include<stdio.h>#include<time.h>#include<stdlib.h>void main(){ time_t start,end; start=time(NULL); for(int i=0;i<1000000000;i++); end=time(NULL); printf("這個循-環用了%f秒%A",difftime(end,start));}運行結果:這個循環用了3.000000秒請按任意鍵繼續. . .⑤最後介紹mktime()函數。原型如下: time_t mktime(struct tm *timer);可以使用函數將用tm結構表示的時間轉換為日歷時間。其返回值就是轉換後的日歷時間。這樣我們就可以先制定一個分解時間,然後對這個時間進行操作。下面的例子用來計算2012年1月20日是星期幾:#include<time.h>#include<stdio.h>#include<stdlib.h>int main(void){ struct tm t; time_t t_of_day; t.tm_year=2012-1900; t.tm_mon=0; t.tm_mday=20; t.tm_hour=0; t.tm_min=12; t.tm_sec=1; t.tm_isdst=1; t_of_day=mktime(&t); printf(ctime(&t_of_day)); return 0;}運行結果:Fri Jan 20 00:12:01 2012請按任意鍵繼續. . .
⑨ C語言中的常用的幾種系統時間結構體類型
在C語言涉及中經常需要定時觸發事件,涉及到獲取系統時間,其結構體類型有多種。Unix/Linux系統下有以下幾種時間結構:
1、time_t 類型:長整型,一般用來表示從1970-01-01 00:00:00時以來的秒數,精確度:秒;由函數time()獲取;
該類型定義在頭文件 /usr/include/sys/time.h 中:
#define _TIME_T
typedef long time_t;
#endif
函數定義:time_t time(time_t* lpt);
如:time_t time = time(NULL);
2、struct timeb 結構:它有兩個主要成員,一個是秒,另一個是毫秒;精確度:毫秒(10E-3秒);
由函數ftime()獲取struct timeb結構的時間;其定義如下:
struct timeb
{
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};
#include <sys/timeb.h>
int ftime(struct timeb* tp);
調用成功返回0;調用失敗返回-1;
3、struct timeval 結構,它有兩個成員;一個是秒,另一個表示微秒,精確度:微秒(10E-6);
由函數gettime0fday()獲取;
struct timeval結構定義為:
struct timeval
{
long tv_sec;
long tv_usec;
}
讀取struct timeval結構數據的函數說明:
#include <sys/time.h>
int gettimeofday(struct timeval* tv,struct timezone* tz);
該函數會提取系統當前時間,並把時間分為秒和微秒兩部分填充到結構struct timeval中;同時把當地的時區信
息填充到結構struct timezone中;
返回值:成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存
取許可權。
struct timezone結構的定義為:
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
}
上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
4、struct timespec 結構:它是POSIX.4標準定義的一個時間結構,精確度:納秒(10E-9秒);
由函數gethrestime()或gethrestime_lasttick()獲取當前系統struct timespec結構的時間;其定義如下:
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
typedef struct timespec timespec_t;
該結構定義在頭頭文件 /usr/include/sys/time_impl.h 中;
extern void gethrestime(timespec_t*);
extern void gethrestime_lasttick(timespec_t*);
5、clock_t 類型:由函數clock()獲取;
#include <time.h>
clock_t clock(void);
該函數以微秒的方式返回CPU的時間;
類型 clock_t 定義在頭文件/usr/include/sys/types.h中:
#ifndef _CLOCK_T
#define _CLOCK_T
typedef long clock_t;
#endif
6、struct tm 結構:由函數gmtime()解析time_t得到
struct tm*gmtime(const time_t*timep);
函數說明:gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後
將結果由結構tm返回。
結構tm的定義為
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒數,正常范圍為0-59,但允許至61秒
int tm_min 代表目前分數,范圍0-59
int tm_hour 從午夜算起的時數,范圍為0-23
int tm_mday 目前月份的日數,范圍01-31
int tm_mon 代表目前月份,從一月算起,范圍從0-11
int tm_year 從1900 年算起至今的年數
int tm_wday 一星期的日數,從星期一算起,范圍為0-6
int tm_yday 從今年1月1日算起至今的天數,范圍為0-365
int tm_isdst 日光節約時間的旗標
此函數返回的時間日期未經時區轉換,而是UTC時間。
返回值:返回結構tm代表目前UTC 時間
7、Unix對時間單位的定義:
#define SEC 1 // 秒
#define MILLISEC 1000 // 毫秒
#define MICROSEC 1000000 // 微秒
#define NANOSEC 1000000000 // 納秒
8、時間格式化函數:
size_t strftime(char *str,size_t max,char *fmt,struct tm *tp); strftime有點像sprintf,其格式由fmt來指定。
%a : 本第幾天名稱,縮寫
%A : 本第幾天名稱,全稱
%b : 月份名稱,縮寫
%B : 月份名稱,全稱
%c : 與ctime/asctime格式相同
%d : 本月第幾日名稱,由零算起
%H : 當天第幾個小時,24小時制,由零算起
%I : 當天第幾個小時,12小時制,由零算起
%j : 當年第幾天,由零算起
%m : 當年第幾月,由零算起
%M : 該小時的第幾分,由零算起
%p : AM或PM
%S : 該分鍾的第幾秒,由零算起
%U : 當年第幾,由第一個日開始計算
%W : 當年第幾,由第一個一開始計算
%w : 當第幾日,由零算起
%x : 當地日期
%X : 當地時間
%y : 兩位數的年份
%Y : 四位數的年份
%Z : 時區名稱的縮寫
%% : %符號
char * strptime(char *s,char *fmt,struct tm *tp); 如同scanf一樣,解譯字串成為tm格式
%h : 與%b及%B同
%c : 讀取%x及%X格式
%C : 讀取%C格式
%e : 與%d同
%D : 讀取%m/%d/%y格式
%k : 與%H同
%l : 與%I同
%r : 讀取"%I:%M:%S %p"格式
%R : 讀取"%H:%M"格式
%T : 讀取"%H:%M:%S"格式
%y : 讀取兩位數年份
%Y : 讀取四位數年份
希望可以幫到你,謝謝!
⑩ C語言中如何把時間變數賦值到一個專門存放時間的數組裡面
C語言中有專門儲存時間的變數結構體 struct tm,在time.h頭文件中。如果要把時間轉換成字元數組,使用asctime函數即可。
1、asctime函數:
原型:char* asctime (const struct tm * timeptr);
功能:把timeptr指向的tm結構體中儲存的時間轉換為字元串;
頭文件:time.h;
返回值:一個固定格式的字元串。字元串格式為:Www Mmm dd hh:mm:ss yyyy。其中Www為星期,Mmm為月份,dd為日,hh為時,mm為分,ss為秒,yyyy為年份。
2、常式:
#include<time.h>
#include<stdio.h>
intmain(){
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);//使用localtime函數把秒數時間rawtime轉換為本地時間以tm結構體保存,並把tm結構體地址儲存到timeinfo當中
printf("當前日期為:%s",asctime(timeinfo));//使用asctime函數把tm結構體中儲存的時間轉換為字元串,並輸出
return0;
}