本文目錄一覽:
c語言中獲取當前時間的代碼,求解釋!!
(1)
time_t time ( time_t * timer );
The function returns this value, and if the argument is not a null pointer,
the value is also set to the object pointed by timer.
(2)
struct tm * localtime ( const time_t * timer );
Uses the time pointed by timer to fill a tm
structure with the values that represent the corresponding local time.
(3)
struct tm {
int tm_sec; /* seconds after the minute – [0,59] */
int tm_min; /* minutes after the hour – [0,59] */
int tm_hour; /* hours since midnight – [0,23] */
int tm_mday; /* day of the month – [1,31] */
int tm_mon; /* months since January – [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday – [0,6] */
int tm_yday; /* days since January 1 – [0,365] */
int tm_isdst; /* daylight savings time flag */
};
( 4 )
int atoi( const char *str );
Convert a string to integer.
你代碼中的if語句就是拿當前時間records中第i個時間比較,如果當前時間的小時不大於records[i]的小時,且分鐘小於records[i]中的分鐘,則返回1(應該是沒超出),否則返回0(超出)。
懂了嗎,寶貝?
c語言時鐘代碼
#includegraphics.h /* 引入graphic.h */
#includemath.h /* 引入math.h */
#includedos.h /* 引入dos.h */
#define pi 3.1415926 /*定義pi=3.14159*/
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y) /*定義……*/
void init() /*初始化程序*/
{int i,l,x1,x2,y1,y2; /*定義……*/
setbkcolor(1); /*設置顏色*/
circle(300,240,200); /*作園*/
circle(300,240,205);
circle(300,240,5);
for(i=0;i60;i++) /*循環(算時間)*/
{if(i%5==0) l=15;
else l=5;
x1=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y;
int gd=VGA,gm=2;
unsigned char h,m,s; /*定義*/
struct time t[1];
initgraph(gd,gm,”d:\\tc”);
init();
setwritemode(1);
gettime(t);
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec; /*定義時分秒*/
setcolor(7); /*設置顏色*/
d(150,h,30);
setcolor(14);
d(170,m,6);
setcolor(4);
d(190,s,6);
while(!kbhit()) /*獲取鍵盤相應*/
{while(t[0].ti_sec==s)
gettime(t); /*C語言中得到時間的函數*/
sound(400); /*計算時間……*/
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if (t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if (t[0].ti_hour!=h)
{ setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
}
getch(); /*設置空格後退出*/
closegraph();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鐘~
加油哦~
用c語言寫一個動態時間表要源代碼
用到的數據結構:
time_t是一個long類型 代表機器時間,可由time( )函數獲得。
日曆時間用一個(char *) 類型的字符串表示。格式為:星期 月 日 小時:分:秒 年\n\0
可由函數ctime( ) asctime( ) 得到。
以tm結構表達的時間,結構tm定義如下:
struct tm { 可由函數localtime( ), gmtime( )得到
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; };
//系統日期
struct date {
int da_year; /* Year – 1980 */
char da_day; /* Day of the month */
char da_mon; /* Month (1 = Jan) */ };
//系統時間
struct time {
unsigned char ti_min; /* Minutes */
unsigned char ti_hour; /* Hours */
unsigned char ti_hund; /* Hundredths of seconds */
unsigned char ti_sec; /* Seconds */ };
用到的函數:
char * asctime(struct tm * ptr) 將tm結構的時間轉化為日曆時間。
char *ctime(long time) 將機器時間轉化為日曆時間。
struct tm *gmtime(time_t *time) 將機器時間轉化為tm時間
當ptr為空時,得到機器時間;非空時,設置機器時間。
time_t time(time_t *ptr) 得到或設置機器時間
double difftime(time_t time2, time_t time1) 得到兩次機器時間差,單位為秒
long dostounix(struct data *d, struct time *t) 將DOS的日期和時間格式轉換成UNIX標準
(機器時間(用到dos.h庫).void unixtodos(long utime, struct date *d, struct time *t)
將UNIX格式的時間和日期轉換成DOS格式(由time 和date兩部分組成,只能由機器時間得到,並且用到dos.h庫)
void getdate(struct date *d) 得到系統日期,d 存放得到的日期信息
void setdate(struct date *d)
void gettime(struct date *t) 得到系統時間 d 存放得到的時間信息
void settime(struct date *t)
C語言的標準庫函數包括一系列日期和時間處理函數,它們都在頭文件中說明。下面列出了這些函數。
在頭文件中定義了三種類型:time_t,struct tm和clock_t。
在中說明的C語言時間函數
time_t time(time_t *timer);
double difftime(time_t time1,time_t time2);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);
time_t mktime(struct tm *timeptr);
clock_t clock(void);
下面是我從網上收集到的時間函數集
asctime(將時間和日期以字符串格式表示)
相關函數
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char * asctime(const struct tm * timeptr);
函數說明
asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。
此函數已經由時區轉換成當地時間,字符串格式為:”Wed Jun 30 21:49:08 1993\n”
返回值
若再調用相關的時間日期函數,此字符串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
附加說明
返回一字符串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (timep);
printf(“%s”,asctime(gmtime(timep)));
}
執行
Sat Oct 28 02:10:06 2000
ctime(將時間和日期以字符串格式表示)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char *ctime(const time_t *timep);
函數說明
ctime ()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。
此函數已經由時區轉換成當地時間,字符串格式為”Wed Jun 30 21 :49 :08 1993\n”。若再調用相關的時間日期函數,此字符串可能會被破壞。
返回值
返回一字符串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (timep);
printf(“%s”,ctime(timep));
}
執行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的時間)
相關函數
time,ctime,ftime,settimeofday
表頭文件
#i nclude
#i nclude
定義函數
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數說明
gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。
timeval結構定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 結構定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時間差了多少分鐘*/
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 /*澳洲(1986年以後)*/
返回值
成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存取權限。
範例
#i nclude
#i nclude
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (tv , tz);
printf(“tv_sec; %d\n”, tv,.tv_sec) ;
printf(“tv_usec; %d\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
執行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前時間和日期)
相關函數
time,asctime,ctime,localtime
表頭文件
#i nclude
定義函數
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 時間
範例
#i nclude
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(timep);
p=gmtime(timep);
printf(“%d%d%d”,(1900+p-tm_year), (1+p-tm_mon),p-tm_mday);
printf(“%s%d;%d;%d\n”, wday[p-tm_wday], p-tm_hour, p-tm_min, p-tm_sec);
}
執行
2000/10/28 Sat 8:15:38
localtime(取得當地目前時間和日期)
相關函數
time, asctime, ctime, gmtime
表頭文件
#i nclude
定義函數
struct tm *localtime(const time_t * timep);
函數說明
localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義請參考gmtime()。此函
數返回的時間日期已經轉換成當地時區。
返回值
返回結構tm代表目前的當地時間。
範例
#i nclude
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(timep);
p=localtime(timep); /*取得當地時間*/
printf (“%d%d%d “, (1900+p-tm_year),( l+p-tm_mon), p-tm_mday);
printf(“%s%d:%d:%d\n”, wday[p-tm_wday],p-tm_hour, p-tm_min, p-tm_sec);
}
執行
2000/10/28 Sat 11:12:22
mktime(將時間結構數據轉換成經過的秒數)
相關函數
原創文章,作者:KNHI,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/133497.html