⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wytimespec.cpp

📁 一个不错
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2003*/#define WYLIB_SOURCE#include "wytimespec.h"#include "wy_atdestroy.h"#include "wymutex.h"#include "wyonce.h"#include <limits>#include <ctime>#include <cmath>              // for modf#include <cstdlib>#include <new>//#undef _POSIX_TIMERS        // for check#ifndef _POSIX_TIMERS#include <sys/time.h>// Mutex dedicated for sycronizing system time access (gettimeofday)//static WyMutex wy__cmtx;#endif// [Syn] Test overflow //// [Ret] true: a+b overflows//      false: otherwise//template<typename T>static bool wy__add_ovf(T a, T b){ if(std::numeric_limits<T>::is_integer) {   return (b>=0)? (a>std::numeric_limits<T>::max()-b)                : (a<std::numeric_limits<T>::min()-b); } else {   // not expected to reach here   WY_TERMINATE(""); }};// [Syn] Test overflow //// [Ret] true: a-b overflows//      false: otherwise//template<typename T>static bool wy__sub_ovf(T a, T b){ if(std::numeric_limits<T>::is_integer) {   return (b>=0)? (a<std::numeric_limits<T>::min()+b)    // min-0                : (a>std::numeric_limits<T>::max()+b);   // 0-min } else {   // not expected to reach here   WY_TERMINATE(""); }};static const char WY__EToken('e');  // search for WY__EToken                                    // many are in string literalconst char WyTimeSpec::class_name[]="WyTimeSpec";class WyTimeSpec::_Assertion {  public:    _Assertion() {        // Wy_Second is time_t        //        if((std::numeric_limits<Wy_Second>::max()!=            std::numeric_limits<time_t>::max())||           (std::numeric_limits<Wy_Second>::min()!=            std::numeric_limits<time_t>::min())) {          WY_TERMINATE("");        }        // Wy_Second is representable by long        //        if((std::numeric_limits<Wy_Second>::max()>            std::numeric_limits<long>::max())||           (std::numeric_limits<Wy_Second>::min()<            std::numeric_limits<long>::min())) {          WY_TERMINATE("");        }        // Wy_Nano is long        //        if((std::numeric_limits<Wy_Nano>::max()!=            std::numeric_limits<signed long>::max())||           (std::numeric_limits<Wy_Nano>::max()!=            std::numeric_limits<signed long>::max())) {          WY_TERMINATE("");        }        // Default value of WyTimeSpec is 0.0        //        WyTimeSpec tmp;        if((tmp.second()!=0)||(tmp.nano()!=0)) {          WY_TERMINATE("");        }    };} static const wy__do_not_use_me__;WyRet WyTimeSpec::_normalize(long& sec, long& nsec) WY__TSPC(){ const std::ldiv_t fr=std::ldiv(nsec,_Giga); const Wy_Second ts=sec+fr.quot; if(wy__add_ovf(sec,fr.quot)) {   WY_RETURN(Wym_ERANGE);   // result not representable } // make sec/nano the same sign if(ts>0) {   if(fr.rem<0) {     sec=ts-1;     nsec=fr.rem+_Giga;     return(Ok);   }   // FALL_THROUGH } else if(ts<0) {   if(fr.rem>0) {     sec=ts+1;     nsec=fr.rem-_Giga;     return(Ok);   }   // FALL_THROUGH } sec=ts; nsec=fr.rem; return(Ok);};WyRet WyTimeSpec::_normalize(long long int& sec, long long int& nsec) WY__TSPC(){ const std::lldiv_t fr=std::lldiv(nsec,_Giga); const long long ts=sec+fr.quot; if(wy__add_ovf(sec,fr.quot)) {   WY_RETURN(Wym_ERANGE);   // result not representable } // make sec/nano the same sign if(ts>0) {   if(fr.rem<0) {     sec=ts-1;     nsec=fr.rem+_Giga;     return(Ok);   }   // FALL_THROUGH } else if(ts<0) {   if(fr.rem>0) {     sec=ts+1;     nsec=fr.rem-_Giga;     return(Ok);   }   // FALL_THROUGH } sec=ts; nsec=fr.rem; return(Ok);};// [Internal] Initialize object to contain the text indicated number//            The text is blen characters pointed by buf in the form://            [+-]ddd.ddd[(eE)[+-]ddd], radix is 10////            second()= converted from the text//            nano()  = ditto// [Ret] Ok//       Wym_EFAULT//       Wym_EBADMSG//       Wym_ERANGE//inline WyRet WyTimeSpec::_init(const char* buf,size_t blen) WY__TSPC(){ WyTimeSpec tm; const WyRet r( Wy::_strnum(tm,NULL,WyCSeg(buf,blen)) ); if(r!=Ok) {   WY_RETURN(r); } _tspc=tm._tspc; return(Ok);};WyTimeSpec::WyTimeSpec(Wy_Second sec, Wy_Nano nsec) WY__TSPC(Reply){ const WyRet r(_normalize(sec, nsec));  if(r!=Ok) {   WY_THROW( Reply(r) ); } _tspc.tv_sec=sec; _tspc.tv_nsec=nsec;};WyTimeSpec::WyTimeSpec(const WyCSeg& cseg) WY__TSPC(Reply){ const WyRet r( this->_init(cseg.begin(),cseg.size()) ); if(r!=Ok) {   WY_THROW( Reply(r) ); }};WyTimeSpec::WyTimeSpec(double fvalue) WY__TSPC(Reply){ double it,fr=std::modf(fvalue,&it); if((it> static_cast<double>( std::numeric_limits<Wy_Second>::max() ))||    (it< static_cast<double>( std::numeric_limits<Wy_Second>::min() ))) {   WY_THROW( Reply(Wym_ERANGE) ); } _tspc.tv_sec =static_cast<Wy_Second>(it); _tspc.tv_nsec=static_cast<Wy_Nano>(fr*WY_CONST_GIGA);};WyRet WyTimeSpec::reset(Wy_Second sec, Wy_Nano nano) WY__TSPC(){ const WyRet r( _normalize(sec, nano) );  if(r!=Ok) {   WY_RETURN(r); } _tspc.tv_sec=sec; _tspc.tv_nsec=nano; return(Ok);};WyRet WyTimeSpec::reset(const WyCSeg& cseg) WY__TSPC(){  WY_RETURN( this->_init(cseg.begin(),cseg.size()) );};/*void WyTimeSpec::reset(double fvalue) WY__TSPC(){   this->_init(fvalue);};void WyTimeSpec::reset(float fvalue) WY__TSPC(){   this->_init(fvalue);};*/WyTimeSpec& WyTimeSpec::operator =(const WyCSeg& cseg) WY__TSPC(Reply){  const WyRet r( this->_init(cseg.begin(),cseg.size()) );  if(r!=Ok) {    WY_THROW( Reply(r) );  }  return(*this);};const WyTimeSpec WyTimeSpec::operator -() const WY__TSPC(Reply){ if(wy__sub_ovf(std::time_t(0),this->_tspc.tv_sec)) {   WY_THROW( Reply(Wym_EMATHNEG) ); } return WyTimeSpec(-_tspc.tv_sec,-_tspc.tv_nsec);};WyTimeSpec & WyTimeSpec::operator +=(const WyTimeSpec &rhs) WY__TSPC(Reply){ if(wy__add_ovf(this->_tspc.tv_sec,rhs._tspc.tv_sec)) {   WY_THROW( Reply(Wym_ERANGE) );   // result not representable } WyTimeSpec v(*this); v._tspc.tv_sec+=rhs._tspc.tv_sec; v._tspc.tv_nsec+=rhs._tspc.tv_nsec; const WyRet r( _normalize(v._tspc.tv_sec,v._tspc.tv_nsec) ); if(r!=Ok) {   WY_THROW( Reply(r) ); } _tspc=v._tspc; return(*this);};WyTimeSpec & WyTimeSpec::operator -=(const WyTimeSpec &rhs) WY__TSPC(Reply){ if(wy__sub_ovf(this->_tspc.tv_sec,rhs._tspc.tv_sec)) {   WY_THROW( Reply(Wym_ERANGE) );   // result not representable } WyTimeSpec v(*this); v._tspc.tv_sec-=rhs._tspc.tv_sec; v._tspc.tv_nsec-=rhs._tspc.tv_nsec; const WyRet r( _normalize(v._tspc.tv_sec,v._tspc.tv_nsec) ); if(r!=Ok) {   WY_THROW( Reply(r) ); } _tspc=v._tspc; return(*this);};const WyTimeSpec WyTimeSpec::operator +(const WyTimeSpec &rhs) const WY__TSPC(Reply){ if(wy__add_ovf(this->_tspc.tv_sec,rhs._tspc.tv_sec)) {   WY_THROW( Reply(Wym_ERANGE) );   // result not representable } WyTimeSpec v(*this); v._tspc.tv_sec+=rhs._tspc.tv_sec; v._tspc.tv_nsec+=rhs._tspc.tv_nsec; const WyRet r( _normalize(v._tspc.tv_sec,v._tspc.tv_nsec) ); if(r!=Ok) {   WY_THROW( Reply(r) ); } return(v);};const WyTimeSpec WyTimeSpec::operator -(const WyTimeSpec &rhs) const WY__TSPC(Reply){ if(wy__sub_ovf(this->_tspc.tv_sec,rhs._tspc.tv_sec)) {   WY_THROW( Reply(Wym_ERANGE) );   // result not representable } WyTimeSpec v(*this); v._tspc.tv_sec-=rhs._tspc.tv_sec; v._tspc.tv_nsec-=rhs._tspc.tv_nsec; const WyRet r( _normalize(v._tspc.tv_sec,v._tspc.tv_nsec) ); if(r!=Ok) {   WY_THROW( Reply(r) ); } return(v);};bool WyTimeSpec::operator >(const WyTimeSpec &rhs) const WY__TSPC(){ if(_tspc.tv_sec>rhs._tspc.tv_sec) {   return true; } else if(_tspc.tv_sec<rhs._tspc.tv_sec) {   return false; } else {} if(_tspc.tv_nsec>rhs._tspc.tv_nsec) {   return true; } else {   return false; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -