time.c
上传用户:sdtbys
上传日期:2009-12-06
资源大小:13k
文件大小:2k
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <unistd.h>
- char *GetTime();
- char *GetFmtTime();
- int timeoutopen(int timeout);
- int timeoutclose();
- static char times[30];
- char *GetTime()
- {
- time_t t;
- struct tm tm;
- memset(times,0,sizeof(times));
- t=time(NULL);
- memcpy(&tm,localtime(&t),sizeof(struct tm));
- sprintf(times,"%04d%02d%02d%02d%02d%02d",
- tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
- tm.tm_hour,tm.tm_min,tm.tm_sec);
- return(times);
- }
- char *GetFmtTime()
- {
- time_t t;
- struct tm tm;
- memset(times,0,sizeof(times));
- t=time(NULL);
- memcpy(&tm,localtime(&t),sizeof(struct tm));
- sprintf(times,"%04d/%02d/%02d %02d:%02d:%02d",
- tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
- tm.tm_hour,tm.tm_min,tm.tm_sec);
- return(times);
- }
- static void (*alrm_old)(int);
- void timeout_p(int v_sig)
- {
- return;
- }
- int timeoutopen(int timeout)
- {
- alrm_old= signal(SIGALRM,timeout_p);
- alarm(timeout);
- }
- int timeoutclose()
- { int timeleft;
- timeleft=alarm(0);
- signal(SIGALRM,alrm_old);
- return(timeleft);
- }
- int TimeOut(int fd,int timeout)
- {
- struct timeval t;
- fd_set fdset;
- t.tv_sec=timeout;
- t.tv_usec=0;
-
- FD_ZERO(&fdset);
- FD_SET(fd,&fdset);
- select(fd+1,&fdset,NULL,NULL,&t);
-
- if (FD_ISSET(fd,&fdset))
- {
- ShowMsg("fd %d can been read ! n",fd);
- return(0);
- }
- else
- {
- ShowMsg("fd %d can not been read now! timeout=%d n",fd,timeout);
- return(1);
- }
- }
- /*main()
- { int i,j;
- char c[50];
- i=getpid();j=getppid();
- printf("pid=%d ppid=%dn",i,j);
- getHostTime(c);
- printf("time=%sn",c);
- }*/
- /*
- main()
- {
- printf("time=%sn",GetTime());
- printf("fmttime=%sn",GetFmtTime());
- }
- */
- /*main()
- { char buf[30]; int timeleft;
- timeoutopen(30);
- scanf("%s",buf);
- timeleft=timeoutclose();
- printf("timeleft=%dn",timeleft);
- }*/
- /*
- char MsgLog[30]="./test.log";
- main()
- {
- if (TimeOut(0,5)) printf("timeout n");
- else printf("can readn");
- }*/
-