新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 菜鳥自學(xué)ARM:linux下編程獲取系統(tǒng)時(shí)間和設(shè)置時(shí)間

菜鳥自學(xué)ARM:linux下編程獲取系統(tǒng)時(shí)間和設(shè)置時(shí)間

作者: 時(shí)間:2016-11-09 來源:網(wǎng)絡(luò) 收藏
獲取時(shí)間有幾種編程方法:

1

本文引用地址:http://m.butianyuan.cn/article/201611/317846.htm

#include
#include
main(){
time_t t;
struct tm *p;
time(&t);
p=gmtime(&t);
printf("Year :%dn",1900+p->tm_year);
printf("Month :%dn",1+p->tm_mon);
printf("Day :%dn",p->tm_mday);
printf("Hour :%dn",p->tm_hour);
printf("Minute:%dn",p->tm_min);
printf("Second:%dn",p->tm_sec);
printf("Weekday:%dn",p->tm_wday);
printf("Days :%dn",p->tm_yday);
printf("Isdst :%dn",p->tm_isdst);

}

2

#include
#include
#include

int main()
{
time_t p;
char s[30];
time(&p);
strcpy(s,ctime(&p)) ;
printf("%sn",s);

}

3

#include
#include
#include

int main()
{
time_t p;
char s[30];
struct gm *q;
time(&p);
q=gmtime(&p);
strcpy(s,asctime(q)) ;
printf("%sn",s);

}

設(shè)置時(shí)間

#include
#include
#include
#include
main()
{
time_t t;
struct tm *p;
int i;
struct timeval tv;
struct timezone tz;
tz.tz_minuteswest=0;
tz.tz_dsttime=0;
time(&p);
p=gmtime(&t);
printf("Change your time:n");
printf("Year:n");
scanf("%d",&i);
(*p).tm_year=i-1900;
printf("Month:n");
scanf("%d",&i);
(*p).tm_mon=i-1;
printf("Date:n");
scanf("%d",&i);
(*p).tm_mday=i;
printf("Hour:n");
scanf("%d",&i);
(*p).tm_hour=i;
printf("Minute:n");
scanf("%d",&i);
(*p).tm_min=i;
printf("Second:n");
scanf("%d",&i);
(*p).tm_sec=i;
t=mktime(p);
tv.tv_sec=t;
tv.tv_usec=0;
settimeofday (&tv , &tz);
printf("Done!n");
}



評(píng)論


技術(shù)專區(qū)

關(guān)閉