Chinaunix

标题: 请问linux下C编程有没有获取系统当前时间的函数? [打印本页]

作者: ``````    时间: 2006-03-31 10:04
标题: 请问linux下C编程有没有获取系统当前时间的函数?
请各位高手赐教:
    linux下的C编程中获取当前系统时间的函数是哪个?
    另还有一个问题:如何修改linux系统的IP,也是用C语言实现。
    小弟初学linux编程,很多不懂,请高手们指教!
多谢了!
作者: mq110    时间: 2006-03-31 15:21
man gettimeofday

改IP的话.用ioctl的方式.


  1. int set_dev_ip(char *ip,char *ethname)
  2. {
  3.     struct ifreq ifreq;
  4.     struct protoent *pro;
  5.     struct sockaddr_in ipaddr;
  6.     int sockfd;

  7.     pro=getprotobyname("tcp");
  8.     if((sockfd=socket(AF_INET,SOCK_STREAM,pro->p_proto))<0)
  9.     {
  10.         perror("socket");
  11.         return -1;
  12.     }
  13.     strcpy(ifreq.ifr_name,ethname);
  14.     if(ioctl(sockfd,SIOCGIFADDR,&ifreq)<0)
  15.     {
  16.         perror("ioctl");
  17.         return -2;
  18.     }
  19.     bcopy(&ifreq.ifr_addr,&ipaddr,sizeof(ifreq.ifr_addr));
  20.     ipaddr.sin_addr.s_addr=inet_addr(ip);
  21.     bcopy(&ipaddr,&ifreq.ifr_addr,sizeof(ipaddr));
  22.     if(ioctl(sockfd,SIOCSIFADDR,&ifreq)<0)
  23.     {
  24.         perror("ioctl");
  25.         return -3;
  26.     }
  27.     return 0;
  28. }
复制代码



自己改吧.~
作者: handi    时间: 2006-03-31 17:11
系统时钟还是系统运行时间?
作者: adm1ner    时间: 2006-03-31 23:08
time_t  tp;
char str;
time(&tp);     获得1970年1月1日到现在的秒数,一个很大的值
str=ctime(&tp);  将秒数转为可以阅读的字符串
作者: x2    时间: 2006-04-01 15:52
可以用 localtime 函数分别获取年月日时分秒的数值。

  1. #include <time.h>
  2. #include <stdio.h>

  3. int main()
  4. {
  5.   time_t now;
  6.   
  7.   now = time(0);
  8.   tm *tnow = localtime(&now);
  9.   printf("%d %d %d %d %d %d\n", 1900+tnow->tm_year,
  10.          tnow->tm_mon+1,
  11.          tnow->tm_mday,
  12.          tnow->tm_hour,
  13.          tnow->tm_min,
  14.          tnow->tm_sec);
  15. }
复制代码

[ 本帖最后由 x2 于 2006-4-1 16:32 编辑 ]
作者: x2    时间: 2006-04-01 16:17
参考资料:
http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html
作者: adm1ner    时间: 2006-04-01 17:17
lz的名字好强




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2