您的位置 首页 产品

菜鸟自学ARM:linux下编程获取体系时刻和设置时刻

获取时间有几种编程方法:1#includestdio.h>#includetime.h>main(){time_tt;structtm*p;time(t);p=gmtime(t);prin

获取时刻有几种编程办法:

1

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

}

2

#include
#include
#include

int main()
{
time_t p;
char s[30];
time(&p);
strcpy(s,ctime(&p)) ;
printf(“%s\n”,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(“%s\n”,s);

}

设置时刻

#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”);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/xinpin/chanpin/277017.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部