Line data Source code
1 : /// TimeStamp.hpp
2 : ///
3 : /// @author Paul Grenz
4 : ///
5 : /// The TimeStamp class is the object which wraps a timeval struct. The class
6 : /// facilitates subtracting two DataTimes to get an interval (a difference).
7 : /// The class can also be used to transport a time and date in a platform
8 : /// independant way.
9 : ///
10 : ////////////////////////////////////////////////////////////////////////////////
11 :
12 : #ifndef PCF_TIME_STAMP_HPP
13 : #define PCF_TIME_STAMP_HPP
14 :
15 : #include <string>
16 : #ifdef WIN32
17 : #include <winsock2.h>
18 : #else
19 : #include <iosfwd>
20 : #include <sys/time.h>
21 : #endif
22 :
23 : ////////////////////////////////////////////////////////////////////////////////
24 :
25 : namespace pcf
26 : {
27 : class TimeStamp
28 : {
29 : // Constructor/destructor/copy constructor.
30 : public:
31 : TimeStamp();
32 : /// Standard constructor from a timeval.
33 : TimeStamp( const timeval &tv );
34 : /// Standard constructor from a TimeStamp.
35 : TimeStamp( const TimeStamp &ts );
36 : /// Standard constructor from a number of milliseconds.
37 : TimeStamp( const int &nMillis );
38 : /// Standard constructor from a year, month, day, hour (24), min, sec.
39 : TimeStamp( const int &nYear, const int &nMonth, const int &nDay,
40 : const int &nHour, const int &nMinute, const int &nSecond );
41 : virtual ~TimeStamp();
42 :
43 : // Operators
44 : public:
45 : const TimeStamp &operator= ( const TimeStamp &tsRhs );
46 : const TimeStamp &operator= ( const timeval &tv );
47 : const TimeStamp &operator= ( const int &nMillis );
48 : TimeStamp operator- ( const TimeStamp &tsRhs ) const;
49 : TimeStamp operator+ ( const TimeStamp &tsRhs ) const;
50 : bool operator> ( const TimeStamp &tsRhs ) const;
51 : bool operator>= ( const TimeStamp &tsRhs ) const;
52 : bool operator< ( const TimeStamp &tsRhs ) const;
53 : bool operator<= ( const TimeStamp &tsRhs ) const;
54 : bool operator==( const TimeStamp &tsRhs ) const;
55 :
56 : // Methods
57 : public:
58 : /// Decrement the time stamp one day.
59 : void decrementDay();
60 : /// Returns the number of days that have elapsed since this
61 : /// object was created, or between this time stamp and ts.
62 : double elapsedDays( const TimeStamp &ts = TimeStamp::now() ) const;
63 : /// Returns the number of milliseconds that have elapsed since this
64 : /// object was created, or between this time stamp and ts.
65 : double elapsedMillis( const TimeStamp &ts = TimeStamp::now() ) const;
66 : /// Returns a flag indicating if the specified number of milliseconds
67 : /// have elapsed since this timestamp was set. Resets the timestamp to
68 : /// the current time if the interval has passsed.
69 : bool intervalElapsedMillis( const int iInterval );
70 : /// Sets the internal time from a ISO 8601 formatted string.
71 : void fromFormattedIso8601Str( const std::string &szIso8601 );
72 : /// Sets the internal time from a Modified Julian Day number.
73 : void fromMJD( const double &xMJD );
74 : /// Generates the hour of the day (0-23) from the current time.
75 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
76 : unsigned int getDayHour() const;
77 : /// Returns the number of days since epoch (1970 Jan 1).
78 : double getDays() const;
79 : /// Returns the date and time formatted as "Sun Jun 24 19:38:12.234 2007".
80 : std::string getFormattedStr() const;
81 : /// Creates an iso-formatted date similar to:
82 : /// YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.451243Z)
83 : /// Based on the current time.
84 : std::string getFormattedIso8601Str() const;
85 : /// Creates an iso-formatted date similar to: 20091230 based on the
86 : /// currently stored date.
87 : std::string getFormattedIsoDateStr() const;
88 : /// Creates an iso-formatted time similar to: 193812 based on the
89 : /// currently stored time.
90 : std::string getFormattedIsoTimeStr() const;
91 : /// Generates the minute of the hour (0-59) from the current time.
92 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
93 : unsigned int getHourMinute() const;
94 : /// Returns the number of microseconds since epoch (1970 Jan 1).
95 : double getMicros() const;
96 : /// Returns, as a string, the number of microseconds since epoch (1970 Jan 1).
97 : std::string getMicrosStr() const;
98 : /// Returns the number of milliseconds since epoch (1970 Jan 1).
99 : double getMillis() const;
100 : /// Returns, as a string, the number of milliseconds since epoch (1970 Jan 1).
101 : std::string getMillisStr() const;
102 : /// Generates the second of the minute (0-60) from the current time.
103 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
104 : unsigned int getMinuteSecond() const;
105 : /// Returns the Modified Julian Day number (days since 1858 Nov 17).
106 : double getMJD() const;
107 : /// Generates the year's month number (1-12) from the current time.
108 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
109 : unsigned int getYearMonth() const;
110 : /// Generates the day of the month (1-31) from the current time.
111 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
112 : unsigned int getMonthDay() const;
113 : /// Returns the month number given the name.
114 : static int getMonthNumber( const std::string &szMonth );
115 : /// Returns the 3-letter weekday name given the number (1 to 7) of the weekday.
116 : static std::string getWeekdayName( const int &nWeekdayNum );
117 : /// Returns the 3-letter month name given the number (1 to 12) of the month.
118 : static std::string getMonthName( const int &nMonthNum );
119 : /// Generates the millisecond of the second (0-999) from the current time.
120 : unsigned int getSecondMillisecond() const;
121 : /// Returns the microsecond part of the time val struct.
122 0 : inline int getTimeValMicros() const
123 : {
124 0 : return m_tvCurr.tv_usec;
125 : }
126 : /// Returns the seconds part of the time val struct.
127 0 : inline int getTimeValSecs() const
128 : {
129 0 : return m_tvCurr.tv_sec;
130 : }
131 : /// Returns the underlying timeval struct.
132 : const timeval &getTimeVal() const;
133 : /// Increment the time stamp one day.
134 : void incrementDay();
135 : /// Fetches the current time and date from the system.
136 : static TimeStamp now();
137 : /// Generates the year (1900+) from the current time.
138 : /// If there is an error, numeric_limits<unsigned int>::max() is returned.
139 : unsigned int getYear() const;
140 :
141 : private:
142 : /// 'timegm' is nonstandard at this time, so our own version is implemented.
143 : ::time_t local_timegm( ::tm *tmCurr );
144 :
145 : // Variables
146 : private:
147 : /// The info about the stored time.
148 : timeval m_tvCurr;
149 :
150 : }; // Class TimeStamp
151 : } // Namespace pcf
152 :
153 : std::ostream &operator<<( std::ostream &strmOut, const pcf::TimeStamp &tsRhs );
154 :
155 : ////////////////////////////////////////////////////////////////////////////////
156 :
157 : #endif // PCF_TIME_STAMP_HPP
|