菜鳥自學(xué)ARM:linux下編程獲取系統(tǒng)時(shí)間和設(shè)置時(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)論