/src/log4cplus/include/log4cplus/helpers/timehelper.h
Line | Count | Source |
1 | | // -*- C++ -*- |
2 | | // Module: Log4CPLUS |
3 | | // File: timehelper.h |
4 | | // Created: 6/2003 |
5 | | // Author: Tad E. Smith |
6 | | // |
7 | | // |
8 | | // Copyright 2003-2017 Tad E. Smith |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
11 | | // you may not use this file except in compliance with the License. |
12 | | // You may obtain a copy of the License at |
13 | | // |
14 | | // http://www.apache.org/licenses/LICENSE-2.0 |
15 | | // |
16 | | // Unless required by applicable law or agreed to in writing, software |
17 | | // distributed under the License is distributed on an "AS IS" BASIS, |
18 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | | // See the License for the specific language governing permissions and |
20 | | // limitations under the License. |
21 | | |
22 | | /** @file */ |
23 | | |
24 | | #ifndef LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ |
25 | | #define LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ |
26 | | |
27 | | #include <log4cplus/config.hxx> |
28 | | |
29 | | #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) |
30 | | #pragma once |
31 | | #endif |
32 | | |
33 | | #include <log4cplus/tstring.h> |
34 | | |
35 | | #if defined (LOG4CPLUS_HAVE_TIME_H) |
36 | | #include <time.h> |
37 | | #endif |
38 | | |
39 | | #include <ctime> |
40 | | #include <chrono> |
41 | | |
42 | | |
43 | | namespace log4cplus { |
44 | | |
45 | | namespace helpers { |
46 | | |
47 | | |
48 | | using std::time_t; |
49 | | using std::tm; |
50 | | namespace chrono = std::chrono; |
51 | | |
52 | | typedef chrono::system_clock Clock; |
53 | | typedef chrono::duration<long long, std::micro> Duration; |
54 | | typedef chrono::time_point<Clock, Duration> Time; |
55 | | |
56 | | |
57 | | template <typename FromDuration> |
58 | | inline |
59 | | Time |
60 | | time_cast (chrono::time_point<Clock, FromDuration> const & tp) |
61 | 3.03M | { |
62 | 3.03M | return chrono::time_point_cast<Duration, Clock> (tp); |
63 | 3.03M | } |
64 | | |
65 | | |
66 | | inline |
67 | | Time |
68 | | now () |
69 | 607k | { |
70 | 607k | return time_cast (Clock::now ()); |
71 | 607k | } |
72 | | |
73 | | |
74 | | inline |
75 | | Time |
76 | | from_time_t (time_t t_time) |
77 | 2.43M | { |
78 | 2.43M | return time_cast (Clock::from_time_t (t_time)); |
79 | 2.43M | } |
80 | | |
81 | | |
82 | | inline |
83 | | time_t |
84 | | to_time_t (Time const & the_time) |
85 | 1.82M | { |
86 | | // This is based on <http://stackoverflow.com/a/17395137/341065>. It is |
87 | | // possible that to_time_t() returns rounded time and we want truncation. |
88 | | |
89 | 1.82M | time_t time = Clock::to_time_t (the_time); |
90 | 1.82M | auto const rounded_time = from_time_t (time); |
91 | 1.82M | if (rounded_time > the_time) |
92 | 0 | --time; |
93 | | |
94 | 1.82M | return time; |
95 | 1.82M | } |
96 | | |
97 | | |
98 | | LOG4CPLUS_EXPORT Time from_struct_tm (tm * t); |
99 | | |
100 | | |
101 | | inline |
102 | | Time |
103 | | truncate_fractions (Time const & the_time) |
104 | 0 | { |
105 | 0 | return from_time_t (to_time_t (the_time)); |
106 | 0 | } |
107 | | |
108 | | |
109 | | inline |
110 | | long |
111 | | microseconds_part (Time const & the_time) |
112 | 607k | { |
113 | 607k | static_assert ((std::ratio_equal<Duration::period, std::micro>::value), |
114 | 607k | "microseconds"); |
115 | | |
116 | | // This is based on <http://stackoverflow.com/a/17395137/341065> |
117 | 607k | return static_cast<long>( |
118 | 607k | (the_time - from_time_t (to_time_t (the_time))).count ()); |
119 | 607k | } |
120 | | |
121 | | |
122 | | inline |
123 | | Time |
124 | | time_from_parts (time_t tv_sec, long tv_usec) |
125 | 0 | { |
126 | 0 | return from_time_t (tv_sec) + chrono::microseconds (tv_usec); |
127 | 0 | } |
128 | | |
129 | | |
130 | | /** |
131 | | * Populates <code>tm</code> using the <code>gmtime()</code> |
132 | | * function. |
133 | | */ |
134 | | |
135 | | LOG4CPLUS_EXPORT |
136 | | void gmTime (tm* t, Time const &); |
137 | | |
138 | | /** |
139 | | * Populates <code>tm</code> using the <code>localtime()</code> |
140 | | * function. |
141 | | */ |
142 | | |
143 | | LOG4CPLUS_EXPORT |
144 | | void localTime (tm* t, Time const &); |
145 | | |
146 | | /** |
147 | | * Returns a string with a "formatted time" specified by |
148 | | * <code>fmt</code>. It used the <code>strftime()</code> |
149 | | * function to do this. |
150 | | * |
151 | | * Look at your platform's <code>strftime()</code> documentation |
152 | | * for the formatting options available. |
153 | | * |
154 | | * The following additional options are provided:<br> |
155 | | * <code>%q</code> - 3 character field that provides milliseconds |
156 | | * <code>%Q</code> - 7 character field that provides fractional |
157 | | * milliseconds. |
158 | | */ |
159 | | LOG4CPLUS_EXPORT |
160 | | log4cplus::tstring getFormattedTime (log4cplus::tstring const & fmt, |
161 | | Time const & the_time, bool use_gmtime = false); |
162 | | |
163 | | |
164 | | } // namespace helpers |
165 | | |
166 | | } // namespace log4cplus |
167 | | |
168 | | |
169 | | #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ |