大头壳

大头大头 下雨不愁 人家有伞 我有大头
posts - 1, comments - 6, trackbacks - 0, articles - 22
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

C语言里面的时间

Posted on 2008-03-28 10:16 王大头 阅读(221) 评论(0)  编辑 收藏 引用
int        clock_getres(clockid_t, struct timespec *);
int        clock_gettime(clockid_t
, struct timespec *);
int        clock_settime(clockid_t
, const struct timespec *);
char      
*ctime(const time_t *);
char      
*ctime_r(const time_t *, char *);
double     difftime(time_t, time_t);
struct tm 
*getdate(const char *);
struct tm 
*gmtime(const time_t *);
struct tm 
*gmtime_r(const time_t *, struct tm *);
struct tm 
*localtime(const time_t *);
struct tm 
*localtime_r(const time_t *, struct tm *);
time_t     
mktime(struct tm *);
int        nanosleep(
const struct timespec *, struct timespec *);
size_t     
strftime(char *, size_t, const char *, const struct tm *);
char      
*strptime(const char *, const char *, struct tm *);
time_t     
time(time_t *);
int        timer_create(clockid_t
, struct sigevent *, timer_t *);
int        timer_delete(timer_t);
int        timer_gettime(timer_t
, struct itimerspec *);
int        timer_getoverrun(timer_t);
int        timer_settime(timer_t
, int, const struct itimerspec *,
               struct itimerspec 
*);
void       tzset(void);





#include <stdio.h>
#
include <stdlib.h>
#
include <string.h>
#
include <sys/errno.h>
#
include <time.h>
struct  time_str
{
        char    tm_year[
5];
        char    tm_mon[
3];
        char    tm_day[
3];
        char    tm_hour[
3];
        char    tm_min[
3];
        char    tm_sec[
3];
};
int main(argc
,argv)
        int     argc;
        char
*   argv[];
{
        char    time_arg[
15];
        char    str[
100];
        time_t  time_curr;
        char    
*time_char;
        struct  tm      tmp_tm;
        struct  tm      
*ttt_tm;
        struct  time_str        tm_str;
        
if(argc!=2) {
                
fprintf(stderr,"Usage : %s time(YYYYMMDDHH24MISS) .\n",argv[0]);
                
exit(1);
        }
        memset(time_arg
,0,sizeof(time_arg));
        memset(
&tm_str,0,sizeof(tm_str));
        strncpy(time_arg
,argv[1],14);

        
/* struct tm */
        tmp_tm
.tm_year  =substr2i(time_arg,1,4)-1900;
        tmp_tm
.tm_mon   =substr2i(time_arg,5,2)-1;
        tmp_tm
.tm_mday  =substr2i(time_arg,7,2);
        tmp_tm
.tm_hour  =substr2i(time_arg,9,2);
        tmp_tm
.tm_min   =substr2i(time_arg,11,2);
        tmp_tm
.tm_sec   =substr2i(time_arg,13,2);
        
if(tmp_tm.tm_min>5) {
                tmp_tm
.tm_hour++;
                tmp_tm
.tm_min=0;
                tmp_tm
.tm_sec=0;
                
if(mktime(&tmp_tm)==-1) {
                        
fprintf(stderr,"mktime error :%s.\n",strerror(errno));
                }
        }
        
printf("convert date to :%04d%02d%02d%02d%02d%02d.\n",tmp_tm.tm_year+1900,tmp_tm.tm_mon+1,tmp_tm.tm_mday,tmp_tm.tm_hour,tmp_tm.tm_min,tmp_tm.tm_sec);

        
/* time and ctime */
        
if((time_curr=time(NULL))==-1) {
                
fprintf(stderr,"time error :%s.\n",strerror(errno));
                
exit(1);
        }
        
if((time_char=ctime(&time_curr))==NULL) {
                
fprintf(stderr,"ctime error :%s.\n",strerror(errno));
                
exit(1);
        }
        
printf("current time is :%s",time_char);

        
/* localtime and asctime */
        
if((ttt_tm=localtime(&time_curr))==NULL) {
                
fprintf(stderr,"localtime error :%s.\n",strerror(errno));
                
exit(1);
        }
        
if((time_char=asctime(ttt_tm))==NULL) {
                
fprintf(stderr,"asctime error :%s.\n",strerror(errno));
                
exit(1);
        }
        
printf("current time is :%s",time_char);

        
/* strftime */
        memset(str
,0,sizeof(str));
        
strftime(str,100,"%Y%m%d%H%M%S",ttt_tm);
        
printf("current time is :%s.\n",str);
        
return(0);
}
int substr2i(str
,pos,len)
        char    str[];
        int     
pos;
        int     len;
{
        char    tmp[
20];
        memset(tmp
,0,sizeof(tmp));
        strncpy(tmp
,&str[(pos-1)],len);
        
return(atoi(tmp));
}