NCBI C++ ToolKit
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
CTimeSpan Class Reference

Search Toolkit Book for CTimeSpan

CTimeSpan. More...

#include <ncbitime.hpp>

List of all members.

Public Types

enum  ESmartStringPrecision {
  eSSP_Year, eSSP_Month, eSSP_Day, eSSP_Hour,
  eSSP_Minute, eSSP_Second, eSSP_Millisecond, eSSP_Microsecond,
  eSSP_Nanosecond, eSSP_Precision1, eSSP_Precision2, eSSP_Precision3,
  eSSP_Precision4, eSSP_Precision5, eSSP_Precision6, eSSP_Precision7,
  eSSP_Default = eSSP_Day
}
 Precision for span "smart" string. Used in AsSmartString() method. More...
enum  ESmartStringZeroMode { eSSZ_SkipZero, eSSZ_NoSkipZero, eSSZ_Default = eSSZ_SkipZero }
 Which format use to zero time span output. More...

Public Member Functions

 CTimeSpan (void)
 Default constructor.
 CTimeSpan (long days, long hours, long minutes, long seconds, long nanoseconds=0)
 Constructor.
 CTimeSpan (long seconds, long nanoseconds=0)
 Constructor.
 CTimeSpan (double seconds)
 Constructor.
 CTimeSpan (const string &str, const CTimeFormat &fmt=kEmptyStr)
 Explicit conversion constructor for string representation of time span.
 CTimeSpan (const CTimeSpan &t)
 Copy constructor.
CTimeSpanoperator= (const CTimeSpan &t)
 Assignment operator.
CTimeSpanoperator= (const string &str)
 Assignment operator.
CTimeSpanClear (void)
 Make the time span "empty",.
ESign GetSign (void) const
 Get sign of time span.
string AsString (const CTimeFormat &format=kEmptyStr) const
 Transform time span to string.
 operator string (void) const
 Return span time as string using the format returned by GetFormat().
string AsSmartString (ESmartStringPrecision precision=eSSP_Default, ERound rounding=eTrunc, ESmartStringZeroMode zero_mode=eSSZ_Default) const
 Transform time span to "smart" string.
long GetCompleteDays (void) const
 Get number of complete days.
long GetCompleteHours (void) const
 Get number of complete hours.
long GetCompleteMinutes (void) const
 Get number of complete minutes.
long GetCompleteSeconds (void) const
 Get number of complete seconds.
long GetNanoSecondsAfterSecond (void) const
 Get number of nanoseconds.
double GetAsDouble (void) const
 Return time span as number of seconds.
bool IsEmpty (void) const
 Return TRUE is an object keep zero time span.
void Set (long seconds, long nanoseconds=0)
 Set time span in seconds and nanoseconds.
void Set (double seconds)
 Set time span from number of seconds (fractional value).
CTimeSpanoperator+= (const CTimeSpan &t)
CTimeSpan operator+ (const CTimeSpan &t) const
CTimeSpanoperator-= (const CTimeSpan &t)
 Operator to subtract time span.
CTimeSpan operator- (const CTimeSpan &t) const
 Operator to subtract time span.
const CTimeSpan operator- (void) const
 Unary operator "-" (minus) to change time span sign.
void Invert (void)
 Invert time span. Changes time span sign.
bool operator== (const CTimeSpan &t) const
 Operator to test equality of time span.
bool operator!= (const CTimeSpan &t) const
 Operator to test in-equality of time span.
bool operator> (const CTimeSpan &t) const
 Operator to test if time span is greater.
bool operator< (const CTimeSpan &t) const
 Operator to test if time span is less.
bool operator>= (const CTimeSpan &t) const
 Operator to test if time span is greater or equal.
bool operator<= (const CTimeSpan &t) const
 Operator to test if time span is less or equal.

Static Public Member Functions

static void SetFormat (const CTimeFormat &format)
 Set the current time span format.
static CTimeFormat GetFormat (void)
 Get the current time span format.

Private Member Functions

int x_Hour (void) const
 Get hour.
int x_Minute (void) const
 Get minute.
int x_Second (void) const
 Get second.
void x_Init (const string &str, const CTimeFormat &format)
 Helper method to set time value from string "str" using "format".
void x_Normalize (void)
 Helper method to normalize stored time value.

Private Attributes

long m_Sec
 Seconds part of the time span.
long m_NanoSec
 Nanoseconds after the second.

Detailed Description

CTimeSpan.

Defines a class to represents a relative time span. Time span can be both positive and negative.

Throw exception of type CTimeException on errors.

Definition at line 1158 of file ncbitime.hpp.


Member Enumeration Documentation

Precision for span "smart" string. Used in AsSmartString() method.

Enumerator:
eSSP_Year 

Round to years.

eSSP_Month 

Round to months.

eSSP_Day 

Round to days.

eSSP_Hour 

Round to hours.

eSSP_Minute 

Round to minutes.

eSSP_Second 

Round to seconds.

eSSP_Millisecond 

Round to milliseconds.

eSSP_Microsecond 

Round to microseconds.

eSSP_Nanosecond 

Do not round at all (accurate time span)

eSSP_Precision1 
eSSP_Precision2 
eSSP_Precision3 
eSSP_Precision4 
eSSP_Precision5 
eSSP_Precision6 
eSSP_Precision7 
eSSP_Default 

Default precision level.

Definition at line 1273 of file ncbitime.hpp.

Which format use to zero time span output.

Enumerator:
eSSZ_SkipZero 

Skip zero valued.

eSSZ_NoSkipZero 

Print zero valued.

eSSZ_Default 

Definition at line 1298 of file ncbitime.hpp.


Constructor & Destructor Documentation

CTimeSpan::CTimeSpan ( void  ) [inline]

Default constructor.

Definition at line 2220 of file ncbitime.hpp.

Referenced by AsSmartString().

CTimeSpan::CTimeSpan ( long  days,
long  hours,
long  minutes,
long  seconds,
long  nanoseconds = 0 
)

Constructor.

Construct time span given the number of days, hours, minutes, seconds, nanoseconds parts of a time span value.

Parameters:
daysDay part of time. Note day starts from 1.
hoursHour part of time.
minutesMinute part of time.
secondsSecond part of time.
nanosecondsNanosecond part of time.

Definition at line 2033 of file ncbitime.cpp.

References NStr::Int8ToString(), kNanoSecondsPerSecond, m_NanoSec, m_Sec, NCBI_THROW, and x_Normalize().

CTimeSpan::CTimeSpan ( long  seconds,
long  nanoseconds = 0 
) [inline, explicit]

Constructor.

Construct time span given the number of seconds and nanoseconds.

Parameters:
secondsSecond part of time.
nanosecondsNanosecond part of time.

Definition at line 2227 of file ncbitime.hpp.

CTimeSpan::CTimeSpan ( double  seconds) [inline, explicit]

Constructor.

Construct time span from number of seconds. Please, use this constructor as rarely as possible, because after doing some arithmetical operations and conversion with it, the time span can differ at some nanoseconds from expected value.

Parameters:
secondsSecond part of time. The fractional part is used to compute nanoseconds.

Definition at line 2233 of file ncbitime.hpp.

CTimeSpan::CTimeSpan ( const string &  str,
const CTimeFormat fmt = kEmptyStr 
) [explicit]

Explicit conversion constructor for string representation of time span.

Construct time span object from string representation of time.

Parameters:
strString representation of time span in format "fmt".
fmtFormat in which "str" is presented. Default value of kEmptyStr, implies the "-S.n" format.

Definition at line 2053 of file ncbitime.cpp.

References GetFormat(), CTimeFormat::IsEmpty(), and x_Init().

CTimeSpan::CTimeSpan ( const CTimeSpan t) [inline]

Copy constructor.

Definition at line 2239 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.


Member Function Documentation

string CTimeSpan::AsSmartString ( ESmartStringPrecision  precision = eSSP_Default,
ERound  rounding = eTrunc,
ESmartStringZeroMode  zero_mode = eSSZ_Default 
) const

Transform time span to "smart" string.

Parameters:
precisionEnum value describing how many parts of time span should be returned. Values from eSSP_Year to eSSP_Nanosecond apparently describe part of time span which will be last in output string. Floating precision levels eSSP_PrecisionN say that maximum 'N' parts of time span will be put to output string. The parts counting begin from first non-zero value.
roundingRounding mode. By default time span will be truncated at last value
zero_modeMode to print or skip zero parts of time span which should be printed but have 0 value. Trailing and leading zeros will be never printed.
Returns:
A string representation of time span.
See also:
AsString, ESmartStringPrecision, ERound, ESmartStringZeroMode

Definition at line 2273 of file ncbitime.cpp.

References CTimeSpan(), eNegative, eRound, eSSP_Day, eSSP_Hour, eSSP_Microsecond, eSSP_Millisecond, eSSP_Minute, eSSP_Month, eSSP_Nanosecond, eSSP_Precision1, eSSP_Second, eSSP_Year, eSSZ_SkipZero, GetCompleteDays(), GetNanoSecondsAfterSecond(), GetSign(), Invert(), kMicroSecondsPerSecond, kNanoSecondsPerSecond, NStr::LongToString(), x_Hour(), x_Minute(), and x_Second().

Referenced by CStopWatch::AsSmartString().

string CTimeSpan::AsString ( const CTimeFormat format = kEmptyStr) const
CTimeSpan & CTimeSpan::Clear ( void  ) [inline]

Make the time span "empty",.

Definition at line 2246 of file ncbitime.hpp.

Referenced by x_Init().

double CTimeSpan::GetAsDouble ( void  ) const [inline]

Return time span as number of seconds.

Returns:
Return representative of time span as type double. The fractional part represents nanoseconds part of time span. The double representation of the time span is aproximate.

Definition at line 2289 of file ncbitime.hpp.

References kNanoSecondsPerSecond.

Referenced by CThreadPool_Impl::AddTask(), CRequestRateControl::Reset(), s_ProgressCallback(), and CThreadPool_Impl::x_WaitForPredicate().

long CTimeSpan::GetCompleteDays ( void  ) const [inline]

Get number of complete days.

Definition at line 2274 of file ncbitime.hpp.

Referenced by AsSmartString(), and AsString().

long CTimeSpan::GetCompleteHours ( void  ) const [inline]

Get number of complete hours.

Definition at line 2277 of file ncbitime.hpp.

Referenced by AsString().

long CTimeSpan::GetCompleteMinutes ( void  ) const [inline]

Get number of complete minutes.

Definition at line 2280 of file ncbitime.hpp.

Referenced by AsString().

long CTimeSpan::GetCompleteSeconds ( void  ) const [inline]
CTimeFormat CTimeSpan::GetFormat ( void  ) [static]

Get the current time span format.

The default format is: "-S.n".

Returns:
An object describing the time format.
See also:
CTimeFormat, SetFormat, AsString

Definition at line 2192 of file ncbitime.cpp.

References format, CStaticTls< TValue >::GetValue(), kDefaultFormatSpan, and CTimeFormat::SetFormat().

Referenced by AsString(), and CTimeSpan().

long CTimeSpan::GetNanoSecondsAfterSecond ( void  ) const [inline]
ESign CTimeSpan::GetSign ( void  ) const [inline]
void CTimeSpan::Invert ( void  ) [inline]

Invert time span. Changes time span sign.

Definition at line 2368 of file ncbitime.hpp.

Referenced by AsSmartString(), and x_Init().

bool CTimeSpan::IsEmpty ( void  ) const [inline]

Return TRUE is an object keep zero time span.

Definition at line 2303 of file ncbitime.hpp.

CTimeSpan::operator string ( void  ) const [inline]

Return span time as string using the format returned by GetFormat().

Definition at line 2324 of file ncbitime.hpp.

bool CTimeSpan::operator!= ( const CTimeSpan t) const [inline]

Operator to test in-equality of time span.

Definition at line 2381 of file ncbitime.hpp.

CTimeSpan CTimeSpan::operator+ ( const CTimeSpan t) const [inline]

Definition at line 2336 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

CTimeSpan & CTimeSpan::operator+= ( const CTimeSpan t) [inline]

Definition at line 2327 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

CTimeSpan CTimeSpan::operator- ( const CTimeSpan t) const [inline]

Operator to subtract time span.

Definition at line 2352 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

const CTimeSpan CTimeSpan::operator- ( void  ) const [inline]

Unary operator "-" (minus) to change time span sign.

Definition at line 2359 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

CTimeSpan & CTimeSpan::operator-= ( const CTimeSpan t) [inline]

Operator to subtract time span.

Definition at line 2343 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

bool CTimeSpan::operator< ( const CTimeSpan t) const [inline]

Operator to test if time span is less.

Definition at line 2397 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

bool CTimeSpan::operator<= ( const CTimeSpan t) const [inline]

Operator to test if time span is less or equal.

Definition at line 2412 of file ncbitime.hpp.

CTimeSpan & CTimeSpan::operator= ( const CTimeSpan t) [inline]

Assignment operator.

Definition at line 2309 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

CTimeSpan & CTimeSpan::operator= ( const string &  str) [inline]

Assignment operator.

Definition at line 2317 of file ncbitime.hpp.

References GetFormat(), and CException::x_Init().

bool CTimeSpan::operator== ( const CTimeSpan t) const [inline]

Operator to test equality of time span.

Definition at line 2375 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

bool CTimeSpan::operator> ( const CTimeSpan t) const [inline]

Operator to test if time span is greater.

Definition at line 2387 of file ncbitime.hpp.

References m_NanoSec, and m_Sec.

bool CTimeSpan::operator>= ( const CTimeSpan t) const [inline]

Operator to test if time span is greater or equal.

Definition at line 2406 of file ncbitime.hpp.

void CTimeSpan::Set ( long  seconds,
long  nanoseconds = 0 
) [inline]

Set time span in seconds and nanoseconds.

Definition at line 2295 of file ncbitime.hpp.

References kNanoSecondsPerSecond.

void CTimeSpan::Set ( double  seconds)

Set time span from number of seconds (fractional value).

Definition at line 1924 of file ncbitime.cpp.

References NStr::DoubleToString(), kNanoSecondsPerSecond, m_NanoSec, m_Sec, NCBI_THROW, and x_Normalize().

void CTimeSpan::SetFormat ( const CTimeFormat format) [static]

Set the current time span format.

The default format is: "-S.n".

Parameters:
formatAn object contains string of letters describing the time format and its type. The format letters have the following meanings:
  • - = add minus for negative time spans
  • d = number of whole days
  • H = total whole number of hours stored in the time span
  • h = hours, "H" modulo 24 (-23 - 23)
  • M = total whole number of minutes stored in the time span
  • m = minutes, "M" modulo 60 (-59 - 59)
  • S = total whole number of seconds stored in the time span
  • s = seconds, "S" modulo 60 (-59 - 59)
  • N = total whole number of nanoseconds stored in the time span
  • n = nanoseconds (-999999999 - 999999999)
See also:
CTimeFormat, GetFormat, AsString

Definition at line 2183 of file ncbitime.cpp.

References s_TlsFormatCleanup(), and CStaticTls< TValue >::SetValue().

int CTimeSpan::x_Hour ( void  ) const [inline, private]

Get hour.

Hours since midnight = -23..23

Definition at line 2265 of file ncbitime.hpp.

Referenced by AsSmartString(), and AsString().

void CTimeSpan::x_Init ( const string &  str,
const CTimeFormat format 
) [private]
int CTimeSpan::x_Minute ( void  ) const [inline, private]

Get minute.

Minutes after the hour = -59..59

Definition at line 2268 of file ncbitime.hpp.

Referenced by AsSmartString(), and AsString().

void CTimeSpan::x_Normalize ( void  ) [private]

Helper method to normalize stored time value.

Definition at line 2168 of file ncbitime.cpp.

References kNanoSecondsPerSecond, m_NanoSec, and m_Sec.

Referenced by CTimeSpan(), Set(), and x_Init().

int CTimeSpan::x_Second ( void  ) const [inline, private]

Get second.

Seconds after the minute = -59..59

Definition at line 2271 of file ncbitime.hpp.

Referenced by AsSmartString(), and AsString().


Member Data Documentation

long CTimeSpan::m_NanoSec [private]

Nanoseconds after the second.

Definition at line 1437 of file ncbitime.hpp.

Referenced by CTimeSpan(), operator+(), operator+=(), operator-(), operator-=(), operator<(), operator=(), operator==(), operator>(), Set(), x_Init(), and x_Normalize().

long CTimeSpan::m_Sec [private]

Seconds part of the time span.

Definition at line 1436 of file ncbitime.hpp.

Referenced by CTimeSpan(), operator+(), operator+=(), operator-(), operator-=(), operator<(), operator=(), operator==(), operator>(), Set(), x_Init(), and x_Normalize().


The documentation for this class was generated from the following files:
Modified on Wed May 23 12:53:41 2012 by modify_doxy.py rev. 337098