ACE-时间

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.o

TARGET = test

LIBS = -L$(ACE_ROOT)/lib -lACE
INCS = -I$(ACE_ROOT)

$(TARGET) : $(OBJS)
	$(CC) -o $(TARGET) $(OBJS) $(LIBS)

$(OBJS) : %.o : %.cpp
	$(CC) -g -c $< -o $@ $(INCS)

.PHONY:clean
clean:
	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;
}



static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值