首页
社区
课程
招聘
一个秒转换时间的c++小类,ntp可以用到,做时间转换可以参考
2007-3-15 22:39 11275

一个秒转换时间的c++小类,ntp可以用到,做时间转换可以参考

2007-3-15 22:39
11275
如果需要用时间加密也可以参考
struct zxptime
{
        zxptime();
        UINT iyear,imonth,iday;
        UINT ihour,imin,isec;
        void GetTime70(UINT tt);从1970年开始
        void GetTime900(UINT tt);从1900年开始
        void print(char *buff);
       
};

zxptime::zxptime()
{
        iyear=imonth=iday=0;
        ihour=imin=isec=0;
}
void zxptime::GetTime70(UINT tt)
{
        UINT myear,mday,oday;
        UINT  msec;
        UINT ttmonth[12]={
                31,28,31,30,31,30,
                31,31,30,31,30,31
        };
        mday=tt/(24*3600);
        msec=tt-mday*(24*3600);
        myear=mday/365;
        oday=mday-myear*365-(myear+1)/4;
        int y, i=0;
        UINT iimoth;
        y=(1970+myear);
        iimoth=((y%4==0)&&(y%100!=0))||(y%400==0);
        for(i=0;i<12;i++)
        {
                if((y=oday-ttmonth[i])>0)
                        oday-=ttmonth[i]+(i==1?iimoth:0);
                else
                        break;
               
        }
        iday=oday+1;
        imonth=i+1;
        iyear=1970+myear;
        ihour=msec/3600;
        imin=(msec-ihour*3600)/60;
        ihour+=8;//中国8时区
        isec=msec%60;
       
};
void zxptime::GetTime900(UINT tt)
{
        UINT myear,mday,oday;
        UINT  msec;
        UINT ttmonth[12]={
                31,28,31,30,31,30,
                        31,31,30,31,30,31
        };
        mday=tt/(24*3600);
        msec=tt-mday*(24*3600);
        myear=mday/365;
        oday=mday-myear*365-(myear)/4;
        int y, i=0;
        UINT iimoth;
        y=(1900+myear);
        iimoth=((y%4==0)&&(y%100!=0))||(y%400==0);
        for(i=0;i<12;i++)
        {
                if((y=oday-ttmonth[i])>0)
                        oday-=ttmonth[i]+(i==1?iimoth:0);
                else
                        break;
               
        }
        iday=oday+1;
        imonth=i+1;
        iyear=1900+myear;
        ihour=msec/3600;
        imin=(msec-ihour*3600)/60;
        ihour+=8; //中国8时区
        isec=msec%60;
       
};
void  zxptime::print(char *buff)
{
        sprintf(buff,"%02d-%02d-%02d %02d:%02d:%02d ",iyear,imonth,iday,
                ihour,imin,isec);
       
        //printf("%s\n",buff);
       
};

[培训]内核驱动高级班,冲击BAT一流互联网大厂工 作,每周日13:00-18:00直播授课

收藏
点赞7
打赏
分享
最新回复 (4)
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
alexmayer 2007-4-4 10:44
2
0
及时雨,我正需要,支持……
雪    币: 217
活跃值: (30)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
foolpanda 2007-8-13 21:46
3
0
有点故障,完善修改如下:
void zxptime::GetTime70(UINT tt,int timea)
{
        UINT myear,mday,oday;
        UINT  msec;
        UINT ttmonth[12]={
                31,28,31,30,31,30,
                31,31,30,31,30,31
        };
        tt+=(timea*3600);
       
        mday=tt/(24*3600);
        idayvale=mday;
        msec=tt-mday*(24*3600);
        myear=mday/365;
        oday=mday-myear*365-(myear+1)/4;
        int y, i=0;
        UINT iimoth;
        y=(1970+myear);
        iimoth=((y%4==0)&&(y%100!=0))||(y%400==0);
        for(i=0;i<12;i++)
        {
                if((y=oday-ttmonth[i])>=0)
                        oday-=ttmonth[i]+(i==1?iimoth:0);
                else
                        break;
               
        }
        iday=oday+1;
        imonth=i+1;
        iyear=1970+myear;
        ihour=msec/3600;
        imin=(msec-ihour*3600)/60;
        isec=msec%60;
       
};
void zxptime::GetTime900(UINT tt,int timea)
{
        UINT myear,mday,oday;
        UINT  msec;
        UINT ttmonth[12]={
                31,28,31,30,31,30,
                        31,31,30,31,30,31
        };
        tt+=(timea*3600);// 中国8时区
        mday=tt/(24*3600);
        idayvale=mday-(70*365)-70/4;
        msec=tt-mday*(24*3600);
        myear=mday/365;
        oday=mday-myear*365-(myear)/4;
        int y, i=0;
        UINT iimoth;
        y=(1900+myear);
        iimoth=((y%4==0)&&(y%100!=0))||(y%400==0);
        for(i=0;i<12;i++)
        {
                if((y=oday-ttmonth[i])>=0)
                        oday-=ttmonth[i]+(i==1?iimoth:0);
                else
                        break;
               
        }
        iday=oday+1;
        imonth=i+1;
        iyear=1900+myear;
        ihour=msec/3600;
        imin=(msec-ihour*3600)/60;
        isec=msec%60;
       
};
雪    币: 217
活跃值: (30)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
foolpanda 2007-8-16 23:22
4
0
以前的代码每月1日会出问题,修改后的就没有这个问题了。
雪    币: 1852
活跃值: (504)
能力值: (RANK:1010 )
在线值:
发帖
回帖
粉丝
北极星2003 25 2007-8-17 14:48
5
0
回想当初开始练习写程序的时候,算法之类的没什么问题,最怕的就是时间转化的问题,闰年闰月的,搞不清楚,这大概是脑子里无法修正的BUG
游客
登录 | 注册 方可回帖
返回