// -*- C++ -*- // Module: Log4CPLUS // File: timehelper.h // Created: 6/2003 // Author: Tad E. Smith // // // Copyright 2003-2017 Tad E. Smith // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** @file */ #ifndef LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ #define LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_ #include #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) #pragma once #endif #include #if defined (LOG4CPLUS_HAVE_TIME_H) #include #endif #include #include namespace log4cplus { namespace helpers { using std::time_t; using std::tm; namespace chrono = std::chrono; typedef chrono::system_clock Clock; typedef chrono::duration Duration; typedef chrono::time_point Time; template inline Time time_cast (chrono::time_point const & tp) { return chrono::time_point_cast (tp); } inline Time now () { return time_cast (Clock::now ()); } inline Time from_time_t (time_t t_time) { return time_cast (Clock::from_time_t (t_time)); } inline time_t to_time_t (Time const & the_time) { // This is based on . It is // possible that to_time_t() returns rounded time and we want truncation. time_t time = Clock::to_time_t (the_time); auto const rounded_time = from_time_t (time); if (rounded_time > the_time) --time; return time; } LOG4CPLUS_EXPORT Time from_struct_tm (tm * t); inline Time truncate_fractions (Time const & the_time) { return from_time_t (to_time_t (the_time)); } inline long microseconds_part (Time const & the_time) { static_assert ((std::ratio_equal::value), "microseconds"); // This is based on return static_cast( (the_time - from_time_t (to_time_t (the_time))).count ()); } inline Time time_from_parts (time_t tv_sec, long tv_usec) { return from_time_t (tv_sec) + chrono::microseconds (tv_usec); } /** * Populates tm using the gmtime() * function. */ LOG4CPLUS_EXPORT void gmTime (tm* t, Time const &); /** * Populates tm using the localtime() * function. */ LOG4CPLUS_EXPORT void localTime (tm* t, Time const &); /** * Returns a string with a "formatted time" specified by * fmt. It used the strftime() * function to do this. * * Look at your platform's strftime() documentation * for the formatting options available. * * The following additional options are provided:
* %q - 3 character field that provides milliseconds * %Q - 7 character field that provides fractional * milliseconds. */ LOG4CPLUS_EXPORT log4cplus::tstring getFormattedTime (log4cplus::tstring const & fmt, Time const & the_time, bool use_gmtime = false); } // namespace helpers } // namespace log4cplus #endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_