ACE_Date_Time、ACE_Time_Value、ACE_OS::gettimeofday() 5.0

这两个类的设计意图(重要)

ACE_Time_Value的设计理念是:他是个整数,这个整数可以进行加减运算,比较运算
    (1)这个整数表示时间轴上的一个点,时间轴的原点为1970年1月1日8点整
    (2)这个整数的值表示离开原点的秒数
    (3)两个整数相加会得到另一个整数,这个整数代表离开1970年更远的一个时间
    (4)在这个理念的基础上下列操作符变的有意义:a<b a>b a<=b a>=b a==b a!=b a+b a-b a+=b a-=b
    (5)其中进行a==b等操作符运算时,只比较到前面的秒,不比较后面的微秒部分(主要是方便开发人员使用)
ACE_Date_Time的设计理念:只是借助ACE_Time_Value来获取时间的年月日时分秒,别无它用

类型转换和初始化

ACE_Time_Value    ------>     ACE_Date_Time                                                    explicit ACE_Date_Time (const ACE_Time_Value& timevalue);

ACE_Time_Value    ------>    long                                                                            ACE_OS::gettimeofday().get_msec()

long    ------>    ACE_Time_Value                                                                            void ACE_Time_Value ::set_msec (const ACE_UINT64 &ms);


测试用例
#include "ace/Log_Msg.h"
#include "ace/OS.h"
#include "ace/Date_Time.h"
#include "ace/Time_Value.h"
#include <iostream>
#include <string>
using namespace std;

//print ace_date_time
void print_ace_date(const ACE_Date_Time& date)
{
	ACE_DEBUG((LM_DEBUG,"%d-%d-%d %d:%d:%d:%d\n",
		date.year(),
		date.month(),
		date.day(),
		date.hour(),
		date.minute(),
		date.second(),
		date.microsec()));
}
void print_ace_time(const string& name,const ACE_Time_Value& t)
{
	cout<<name<<"="<<t<<"秒"<<endl;
}

int
	ACE_TMAIN (int, ACE_TCHAR *[])
{
	cout<<"ACE_Time_Value从秒的角度看就是时间轴上的两个整数点,距离就是时间间隔:"<<endl;
	ACE_Time_Value one_second(1),half_second(0,1000 * 500+1),zero_time;//init
	print_ace_time("one_second",one_second);
	print_ace_time("half_second",half_second);
	print_ace_time("zero_time",zero_time);
	print_ace_time("one_second-zero_time",one_second-zero_time);
	print_ace_time("zero_time-one_second",zero_time-one_second);

	ACE_Time_Value seven_days_time(3600*24*7);
	cout<<"跨度七天的时间段:ACE_Time_Value seven_days_time(3600*24*7)"<<endl;
	print_ace_time("seven_days_time",seven_days_time);

	cout<<"当前时间: ACE_OS::gettimeofday()"<<endl;
	ACE_Time_Value now = ACE_OS::gettimeofday();
	print_ace_time("now",now);

	cout<<"时间比较:"<<endl;

	if (one_second > half_second)
	{
		cout<<"one_second > half_second"<<endl;
	}
	if (half_second > zero_time)
	{
		cout<<"half_second > zero_time"<<endl;
	}
	
	ACE_Time_Value t1(0,1000*500+1),t2(0,1000*500+2);
	if (half_second == ACE_Time_Value(0,1000*500+2))
	{
		cout<<t1<<" == "<<t2<<endl;
	}
	else if (t1 < t2)
	{
		cout<<t1<<" < "<<t2<<endl;
	}
	else if (t1 > t2)
	{
		cout<<t1<<" > "<<t2<<endl;
	} 
	else
	{
		cout<<t1<<" $$$ "<<t2<<endl;
	}
	
	cout<<"时间的原点:";
	print_ace_date(ACE_Date_Time(zero_time));
	cout<<"从原点开始七天后的时间:";
	print_ace_date(ACE_Date_Time(seven_days_time));

	return 0;
}


官网介绍:http://www.dre.vanderbilt.edu/Doxygen/6.0.1/html/libace-doc/a00559.html

ACE_Time_Value使用注意事项 http://blog.csdn.net/stephenxu111/article/details/6457659

ACE_Time_Value使用注意事项(续) http://blog.csdn.net/stephenxu111/article/details/9053945

ACE官方测试用例

很好的说明了具体用法

//=============================================================================
/**
 *  @file    Time_Value_Test.cpp
 *
 *  $Id: Time_Value_Test.cpp 95763 2012-05-16 06:43:51Z johnnyw $
 *
 *    This is a simple test of ACE_Time_Value.  No command line arguments
 *    are needed to run the test.
 *
 *  @author Prashant Jain <pjain@cs.wustl.edu> and David Levine <levine@cs.wustl.edu>
 */
//=============================================================================

// Note, for this test the config.h file *must* come first!
#include "ace/config-all.h"

#include "test_config.h"
#include "ace/ACE.h"
#include "ace/Time_Value.h"

#include "ace/Numeric_Limits.h"

#ifdef ACE_HAS_CPP98_IOSTREAMS
#include <sstream>
#endif

int
run_main (int, ACE_TCHAR *[])
{
  int ret = 0;

  ACE_START_TEST (ACE_TEXT ("Time_Value_Test"));

  ACE_Time_Value tv1;//0秒
  ACE_Time_Value tv2 (2);//2秒
  ACE_Time_Value tv3 (100U);//100秒
  ACE_Time_Value tv4 (1, 1000000);//2秒
  ACE_Time_Value tv5 (2UL);//2秒
  ACE_Time_Value tv6 (1, -1000000);//0秒
  ACE_Time_Value tv7 (static_cast<long> (2.0));//2秒

  // Beware!  2.5 gets truncated to 2!
  // NOTE:  this is intended to show what happens with
  // ACE_Time_Value (2.5).  Some compilers, such as g++ 2.7.2.3,
  // actually warn about it without the case.
  ACE_Time_Value tv8 (static_cast <long> (2.5));//2秒

  ACE_Time_Value first;
  ACE_Time_Value last(ACE_Time_Value::max_time);
  first = last;
  ACE_TEST_ASSERT (first == last);

  // Test assignment operator, tv9 and tv6 must be the same after this
  ACE_Time_Value tv9;
  tv9 = tv6;

  ACE_TEST_ASSERT (tv1 == ACE_Time_Value (0));
  ACE_TEST_ASSERT (tv2 < tv3);
  ACE_TEST_ASSERT (tv2 <= tv2);
  ACE_TEST_ASSERT (tv2 >= tv4);
  ACE_TEST_ASSERT (tv5 >= tv6);
  ACE_TEST_ASSERT (tv2 == ACE_Time_Value (1, 1000000));//tv2 (2);//2秒  == ACE_Time_Value (1, 1000000))
  ACE_TEST_ASSERT (tv5 == tv4);
  ACE_TEST_ASSERT (tv2 == tv4);
  ACE_TEST_ASSERT (tv1 != tv2);
  ACE_TEST_ASSERT (tv6 == tv1);
  ACE_TEST_ASSERT (tv5 == tv7);
  ACE_TEST_ASSERT (tv7 == tv8); // That's right!  See above . . .
  ACE_TEST_ASSERT (tv9 == tv6);

  ACE_Time_Value tv10 (1);

  ACE_TEST_ASSERT (tv10.sec() == 1);

  // test multiplication by double
  // test simple multiplication
  tv1.set (1, 1);
  tv2.set (2, 2);
  tv1 *= 2.0;
  ACE_TEST_ASSERT (tv1 == tv2);
  tv1.set (1, 1);
  tv2.set (static_cast<time_t> (-2), static_cast<suseconds_t> (-2));
  tv1 *= -2.0;
  ACE_TEST_ASSERT (tv1 == tv2);

  // test usec shift
  tv1.set (1, 999999);
  tv2.set (19, 999990);
  tv1 *= 10.0;
  ACE_TEST_ASSERT ( tv1 == tv2);
  tv1.set (1, 999999);
  tv2.set (static_cast<time_t> (-19), -999990);
  tv1 *= -10.0;
  ACE_TEST_ASSERT (tv1 == tv2);

  // Test correct msec() convert; also checks for compile error reported in
  // Bugzilla 3336.
  ACE_Time_Value msec_test (42, 555000);
  ACE_UINT64 ms = 0;
  msec_test.msec (ms);
  if (ms != 42555)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("msec test failed: %Q should be 42555\n"),
                ms));
  ms = 0;
  ms = msec_test.get_msec ();
  if (ms != 42555)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("get_msec test failed: %Q should be 42555\n"),
                ms));
  ACE_Time_Value const msec_test2 (42, 555000);
  ms = 0;
  msec_test2.msec (ms);
  if (ms != 42555)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("msec const test failed: %Q should be 42555\n"),
                ms));

  // Test setting from ACE_UINT64
  ms = 42555;
  ACE_Time_Value msec_test3;
  msec_test3.set_msec (ms);
  if (msec_test3.sec () != 42)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("set_msec test failed: %d secs should be 42\n"),
                msec_test3.sec ()));
  if (msec_test3.usec () != 555000)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("set_msec test failed: %d usecs should be 555000\n"),
                msec_test3.usec ()));

#ifdef ACE_HAS_CPP98_IOSTREAMS
  std::ostringstream ost;
  ost << ACE_Time_Value(1);
  ACE_TEST_ASSERT( ost.str() == "1" );
  ost.str("");
  ost << ACE_Time_Value(1,1);
  ACE_TEST_ASSERT( ost.str() == "1.000001" );
  ost.str("");
  ost << ACE_Time_Value(static_cast<time_t>(-1),-1);
  ACE_TEST_ASSERT( ost.str() == "-1.000001" );
  ost.str("");
  ost << ACE_Time_Value(0,1);
  ACE_TEST_ASSERT( ost.str() == "0.000001" );
  ost.str("");
  ost << ACE_Time_Value(0,-1);
  ACE_TEST_ASSERT( ost.str() == "-0.000001" );
  ost.str("");
  ost << ACE_Time_Value();
  ACE_TEST_ASSERT( ost.str() == "0" );
#endif

  ACE_END_TEST;

  return ret;
}





类型转换和初始化

ACE_Time_Value    ------>     ACE_Date_Time                                                    explicit ACE_Date_Time (const ACE_Time_Value& timevalue);

ACE_Time_Value    ------>    long                                                                            ACE_OS::gettimeofday().get_msec()

long    ------>    ACE_Time_Value                                                                            void ACE_Time_Value ::set_msec (const ACE_UINT64 &ms);

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
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值