ACE-时间

来源:互联网 发布:13款英朗 导航 软件 编辑:程序博客网 时间:2024/06/10 03:58

ACE中关于时间的文件包括Date_Time.h、Time_Value.h、Time_Value_T.h、Time_Policy.h、Time_Policy_T.h
常用的是前两个,其中Date_Time.h是一个系统独立的日期函数,能够获取当前系统的时间,同时能够根据ACE_Time_Value指定的时间计算想用的日期,Time_Value.h包含ACE_Time_Value类,该类的主结构为一个秒,一个微妙,更可以理解为对timeval结构体的重新包装,并重定义相关操作符,常用的类函数有now:获取系统当前时间,其中需要注意的是to_relative_time,获取相对时间,是类变量给定的时间与系统当前时间的差。to_absolute_time,获取绝对时间,是类变量给定的时间与当前时间相加后得到的绝对时间。

示例:

#include <iostream>#include <ace/ACE.h>#include <ace/Init_ACE.h>#include <ace/Date_Time.h>#include <ace/Time_Value.h>#include <time.h>using namespace std;int main(){    //ACE初始化,必须    ACE::init();            //ACE_OS::gettimeofday() ACE的Time_Policy中最常用的获取当前系统时间的函数    ACE_Time_Value now = ACE_OS::gettimeofday();    cout<<"ACE_Time_Value now = ACE_OS::gettimeofday() now:"<<now<<endl;    cout<<"now.sec() : "<<now.sec()<<endl;    cout<<"now.usec() : "<<now.usec()<<endl;    cout<<endl;        //初始化类,并给秒和微妙赋值    ACE_Time_Value time1(10,10000);    ACE_Time_Value time2(11,11);    //类相加    ACE_Time_Value time3 = time1 + time2;    cout<<"time3.sec() : "<<time3.sec()<<endl;    cout<<"time3.usec() : "<<time3.usec()<<endl;        //now函数返回当前时间,类实例不改变    ACE_Time_Value getnow = time3.now();    cout<<"getnow.sec() : "<<getnow.sec()<<endl;    cout<<"getnow.usec() : "<<getnow.usec()<<endl;        //to_relative_time,返回实例给定的时间与当前是相减的值,即相对时间    sleep(1);    cout<<"to_relative_time : "<<getnow.to_relative_time()<<endl;        //to_absolute_time,实例给定的时间与当前时间相加的值    cout<<"to_absolute_time : "<<getnow.to_relative_time().to_absolute_time()<<endl;        //ACE中定义的与系统无关的日期类    ACE_Date_Time ac(getnow);    cout<<ac.year()<<"-"<<ac.month()<<"-"<<ac.day()<<endl;    cout<<ac.hour()<<":"<<ac.minute()<<":"<<ac.second()<<"."<<ac.microsec()<<endl;    cout<<ac.weekday()<<endl;        sleep(1);    //更新到最新的系统时间    ac.update();    cout<<ac.year()<<"-"<<ac.month()<<"-"<<ac.day()<<endl;    cout<<ac.hour()<<":"<<ac.minute()<<":"<<ac.second()<<"."<<ac.microsec()<<endl;    cout<<ac.weekday()<<endl;        //ACE关闭    ACE::fini();    return 0;};

makefile文件:

CC = g++OBJS = main.oTARGET = testLIBS = -L$(ACE_ROOT)/lib -lACEINCS = -I$(ACE_ROOT)$(TARGET) : $(OBJS)$(CC) -o $(TARGET) $(OBJS) $(LIBS)$(OBJS) : %.o : %.cpp$(CC) -g -c $< -o $@ $(INCS).PHONY:cleanclean:rm -f $(TARGET) $(OBJS)



附:Linux常用时间函数说明与测试程序

#include <iostream>#include <ctime>#include <sys/time.h> //timeval需要using namespace std;int main(){    time_t times;    times = time(NULL);    //time_t 保存的是从1970年1月1日开始到现在的秒数    cout<<"times:"<<times<<endl;    //ctime将time_t转换为人类可识别的日期形式,一般与date命令返回的内容相同    cout<<"ctime:"<<ctime(×)<<endl;    cout<<"sleep3"<<endl;    sleep(3);    time_t t2;    t2 = time(NULL);    //difftime 参数为两个time_t,返回相差秒数    cout<<"difftime:"<<difftime(t2, times)<<endl;        //    struct tm {        /* a broken-down time *///     int  tm_sec;     /* seconds after the minute: [0 - 60] *///     int  tm_min;     /* minutes after the hour: [0 - 59] *///     int  tm_hour;    /* hours after midnight: [0 - 23] *///     int  tm_mday;    /* day of the month: [1 - 31] *///     int  tm_mon;     /* months since January: [0 - 11] *///     int  tm_year;    /* years since 1900 *///     int  tm_wday;    /* days since Sunday: [0 - 6] *///     int  tm_yday;    /* days since January 1: [0 - 365] *///     int  tm_isdst;   /* daylight saving time flag: <0, 0, >0 */////  // 以下两个字段在有些版本中是存在的,使用时需要查看对应的头文件确认////  long int tm_gmtoff; /* Seconds east of UTC. *///  const char *tm_zone; /* Timezone abbreviation. */////    };         //gmtime转换time_t到struct tm结构,同时转换为UTC时间,中国为+8区,转化后时间需要+8    struct tm *tms = gmtime(&t2);    tms->tm_sec += 2;    //asctime将tm结构转换到字符串形式    cout<<"tms:"<<asctime(tms)<<endl;        //localtime将转换time_t到struct tm结构    struct tm *tm1 = localtime(&t2);    cout<<"tm1:"<<asctime(tm1)<<endl;        //mktime将tm结构转换到time_t结构    cout<<"mktime:"<<mktime(tm1)<<endl;        //获取微妙级别的时间//    struct timeval{//        time_t tv_sec;        /*** second ***///        susecond_t tv_usec;    /*** microsecond 微妙***///    }    struct timeval tv;    gettimeofday(&tv, NULL);    cout<<"tv.tv_sec:"<<tv.tv_sec<<endl;    cout<<"tv.tv_usec:"<<tv.tv_usec<<endl;    return 0;}



原创粉丝点击