LibTime – A library for time management and manipulation using the C++ Standard Library's chrono utilities.
#include "LibTime.h"
// Time utilities
uint32_t millis();
// Current Time
std::string nowUtc();
TimePoint nowTp();
std::string nowToStr(const char *pFmt = NULL);
// Time conversion
std::string tpToStr(const TimePoint &tp, const char *pFmt = NULL);
TimePoint strToTp(const std::string &str, const char *pFmt = NULL);
// Time difference
size_t tpDiffSec(const TimePoint &tpEnd, const TimePoint &tpStart);
size_t tpDiffMs(const TimePoint &tpEnd, const TimePoint &tpStart);LibTime provides a set of functions for time management and formatting, making use of the C++ Standard Library's chrono system clock. It offers functionality for getting the current time, formatting time points as strings, and computing time differences in seconds and milliseconds.
-
uint32_t millis()
Returns the number of milliseconds since the system clock's epoch (similar tomillis()in embedded programming).Returns: Milliseconds as an unsigned 32-bit integer.
-
std::string nowUtc()
Returns the current UTC time as a formatted string.Returns: A string representing the current UTC time.
-
TimePoint nowTp()
Returns the current time as aTimePointobject.Returns: A
TimePointobject representing the current time. -
*std::string nowToStr(const char pFmt = NULL)
Returns the current time as a formatted string.- pFmt: A format string for the output (optional). If
NULL, a default format will be used.
Returns: A string representing the current time in the specified format.
- pFmt: A format string for the output (optional). If
-
*std::string tpToStr(const TimePoint &tp, const char pFmt = NULL)
Converts aTimePointto a formatted string.- tp: The
TimePointto convert. - pFmt: A format string for the output (optional). If
NULL, a default format will be used.
Returns: A string representing the time point in the specified format.
- tp: The
-
*TimePoint strToTp(const std::string &str, const char pFmt = NULL)
Converts a formatted string to aTimePoint.- str: The time string to convert.
- pFmt: The format of the input string (optional). If
NULL, a default format will be assumed.
Returns: A
TimePointobject representing the time from the string.
-
size_t tpDiffSec(const TimePoint &tpEnd, const TimePoint &tpStart)
Returns the difference between twoTimePointobjects in seconds.- tpEnd: The later
TimePoint. - tpStart: The earlier
TimePoint.
Returns: The difference in seconds as a size_t.
- tpEnd: The later
-
size_t tpDiffMs(const TimePoint &tpEnd, const TimePoint &tpStart)
Returns the difference between twoTimePointobjects in milliseconds.- tpEnd: The later
TimePoint. - tpStart: The earlier
TimePoint.
Returns: The difference in milliseconds as a size_t.
- tpEnd: The later
// Example of getting the current time in various formats
std::string currentUtc = nowUtc();
std::cout << "Current UTC Time: " << currentUtc << std::endl;
TimePoint currentTp = nowTp();
std::string currentTimeStr = nowToStr();
std::cout << "Current Time: " << currentTimeStr << std::endl;
// Example of calculating time differences
TimePoint start = nowTp();
// Do something...
TimePoint end = nowTp();
size_t elapsedMs = tpDiffMs(end, start);
std::cout << "Elapsed time in milliseconds: " << elapsedMs << std::endl;Processing()
Written by Johannes Natter.