log4cplus 2.0.8
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
43namespace log4cplus {
44
45namespace helpers {
46
47
48using std::time_t;
49using std::tm;
50namespace chrono = std::chrono;
51
52typedef chrono::system_clock Clock;
53typedef chrono::duration<long long, std::micro> Duration;
54typedef chrono::time_point<Clock, Duration> Time;
55
56
57template <typename FromDuration>
58inline
59Time
60time_cast (chrono::time_point<Clock, FromDuration> const & tp)
61{
62 return chrono::time_point_cast<Duration, Clock> (tp);
63}
64
65
66inline
67Time
69{
70 return time_cast (Clock::now ());
71}
72
73
74inline
75Time
76from_time_t (time_t t_time)
77{
78 return time_cast (Clock::from_time_t (t_time));
79}
80
81
82inline
83time_t
84to_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
101inline
102Time
103truncate_fractions (Time const & the_time)
104{
105 return from_time_t (to_time_t (the_time));
106}
107
108
109inline
110long
111microseconds_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
122inline
123Time
124time_from_parts (time_t tv_sec, long tv_usec)
125{
126 return from_time_t (tv_sec) + chrono::microseconds (tv_usec);
127}
128
129
136void gmTime (tm* t, Time const &);
137
144void 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