1 // Module: Log4CPLUS
2 // File: timehelper.h
3 // Created: 6/2003
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef _LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
17 #define _LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/tstring.h>
21
22 #ifdef TM_IN_SYS_TIME
23 #include <sys/time.h>
24 #endif
25
26 #include <time.h>
27
28
29 namespace log4cplus {
30 namespace helpers {
31
32 /**
33 * This class represents a Epoch time with microsecond accuracy.
34 */
35 class LOG4CPLUS_EXPORT Time {
36 public:
37 Time();
38 Time(long tv_sec, long tv_usec);
39 Time(time_t time);
40
41 /**
42 * Returns the current time using the <code>gettimeofday()</code>
43 * method if it is available on the current platform. (Not on
44 * WIN32.)
45 */
46 static Time gettimeofday();
47
48 // Methods
49 /**
50 * Returns <i>seconds</i> value.
51 */
52 long sec() const { return tv_sec; }
53
54 /**
55 * Returns <i>microseconds</i> value.
56 */
57 long usec() const { return tv_usec; }
58
59 /**
60 * Sets the <i>seconds</i> value.
61 */
62 void sec(long s) { tv_sec = s; }
63
64 /**
65 * Sets the <i>microseconds</i> value.
66 */
67 void usec(long us) { tv_usec = us; }
68
69 /**
70 * Sets this Time using the <code>mktime</code> function.
71 */
72 int setTime(struct tm* t);
73
74 /**
75 * Returns this Time as a <code>time_t></code> value.
76 */
77 time_t getTime() const;
78
79 /**
80 * Populates <code>tm</code> using the <code>gmtime()</code>
81 * function.
82 */
83 void gmtime(struct tm* t) const;
84
85 /**
86 * Populates <code>tm</code> using the <code>localtime()</code>
87 * function.
88 */
89 void localtime(struct tm* t) const;
90
91 /**
92 * Returns a string with a "formatted time" specified by
93 * <code>fmt</code>. It used the <code>strftime()</code>
94 * function to do this.
95 * <p>
96 * Look at your platform's <code>strftime()</code> documentation
97 * for the formatting options available.
98 * <p>
99 * The following additional options are provided:<br>
100 * <code>%q</code> - 3 character field that provides milliseconds
101 * <code>%Q</code> - 7 character field that provides fractional
102 * milliseconds.
103 */
104 log4cplus::tstring getFormattedTime(const log4cplus::tstring& fmt,
105 bool use_gmtime = false) const;
106
107 // Operators
108 Time& operator+=(const Time& rhs);
109 Time& operator-=(const Time& rhs);
110 Time& operator/=(long rhs);
111 Time& operator*=(long rhs);
112 bool operator==(const Time& rhs) { return tv_sec == rhs.tv_sec &&
113 tv_usec == rhs.tv_usec; }
114 bool operator!=(const Time& rhs) { return !(*this == rhs); }
115
116 private:
117 // Data
118 long tv_sec; /* seconds */
119 long tv_usec; /* microseconds */
120 };
121
122 }
123 }
124
125
126 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator+
127 (const log4cplus::helpers::Time& lhs,
128 const log4cplus::helpers::Time& rhs);
129 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator-
130 (const log4cplus::helpers::Time& lhs,
131 const log4cplus::helpers::Time& rhs);
132 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator/
133 (const log4cplus::helpers::Time& lhs,
134 long rhs);
135 LOG4CPLUS_EXPORT const log4cplus::helpers::Time operator*
136 (const log4cplus::helpers::Time& lhs,
137 long rhs);
138
139 LOG4CPLUS_EXPORT bool operator<(const log4cplus::helpers::Time& lhs,
140 const log4cplus::helpers::Time& rhs);
141 LOG4CPLUS_EXPORT bool operator<=(const log4cplus::helpers::Time& lhs,
142 const log4cplus::helpers::Time& rhs);
143
144 LOG4CPLUS_EXPORT bool operator>(const log4cplus::helpers::Time& lhs,
145 const log4cplus::helpers::Time& rhs);
146 LOG4CPLUS_EXPORT bool operator>=(const log4cplus::helpers::Time& lhs,
147 const log4cplus::helpers::Time& rhs);
148
149 LOG4CPLUS_EXPORT bool operator==(const log4cplus::helpers::Time& lhs,
150 const log4cplus::helpers::Time& rhs);
151 LOG4CPLUS_EXPORT bool operator!=(const log4cplus::helpers::Time& lhs,
152 const log4cplus::helpers::Time& rhs);
153
154 #endif // _LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1