log4cplus  2.1.0
timehelper.h
Go to the documentation of this file.
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 
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 {
62  return chrono::time_point_cast<Duration, Clock> (tp);
63 }
64 
65 
66 inline
67 Time
68 now ()
69 {
70  return time_cast (Clock::now ());
71 }
72 
73 
74 inline
75 Time
76 from_time_t (time_t t_time)
77 {
78  return time_cast (Clock::from_time_t (t_time));
79 }
80 
81 
82 inline
83 time_t
84 to_time_t (Time const & the_time)
85 {
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  time_t time = Clock::to_time_t (the_time);
90  auto const rounded_time = from_time_t (time);
91  if (rounded_time > the_time)
92  --time;
93 
94  return time;
95 }
96 
97 
99 
100 
101 inline
102 Time
103 truncate_fractions (Time const & the_time)
104 {
105  return from_time_t (to_time_t (the_time));
106 }
107 
108 
109 inline
110 long
111 microseconds_part (Time const & the_time)
112 {
113  static_assert ((std::ratio_equal<Duration::period, std::micro>::value),
114  "microseconds");
115 
116  // This is based on <http://stackoverflow.com/a/17395137/341065>
117  return static_cast<long>(
118  (the_time - from_time_t (to_time_t (the_time))).count ());
119 }
120 
121 
122 inline
123 Time
124 time_from_parts (time_t tv_sec, long tv_usec)
125 {
126  return from_time_t (tv_sec) + chrono::microseconds (tv_usec);
127 }
128 
129 
136 void gmTime (tm* t, Time const &);
137 
144 void localTime (tm* t, Time const &);
145 
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_
Time time_from_parts(time_t tv_sec, long tv_usec)
Definition: timehelper.h:124
LOG4CPLUS_EXPORT log4cplus::tstring getFormattedTime(log4cplus::tstring const &fmt, Time const &the_time, bool use_gmtime=false)
Returns a string with a "formatted time" specified by fmt.
long microseconds_part(Time const &the_time)
Definition: timehelper.h:111
time_t to_time_t(Time const &the_time)
Definition: timehelper.h:84
LOG4CPLUS_EXPORT void gmTime(tm *t, Time const &)
Populates tm using the gmtime() function.
LOG4CPLUS_EXPORT void localTime(tm *t, Time const &)
Populates tm using the localtime() function.
chrono::duration< long long, std::micro > Duration
Definition: timehelper.h:53
chrono::system_clock Clock
Definition: timehelper.h:52
LOG4CPLUS_EXPORT Time from_struct_tm(tm *t)
Time time_cast(chrono::time_point< Clock, FromDuration > const &tp)
Definition: timehelper.h:60
Time from_time_t(time_t t_time)
Definition: timehelper.h:76
chrono::time_point< Clock, Duration > Time
Definition: timehelper.h:54
Time truncate_fractions(Time const &the_time)
Definition: timehelper.h:103
std::basic_string< tchar > tstring
Definition: tstring.h:39
#define LOG4CPLUS_EXPORT
Definition: win32.h:141