|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/* |
| 12 | + * File: Clock.hpp |
| 13 | + * Author: John Lång (john.larry.lang@cern.ch) |
| 14 | + * |
| 15 | + * Created on 29 August 2016, 9:29 |
| 16 | + */ |
| 17 | +#ifndef O2_DCS_CLOCK_H |
| 18 | +#define O2_DCS_CLOCK_H |
| 19 | + |
| 20 | +#include <ctime> |
| 21 | +#include <cstdint> |
| 22 | +#include <chrono> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +namespace o2 |
| 27 | +{ |
| 28 | +namespace dcs |
| 29 | +{ |
| 30 | +/** |
| 31 | + * Returns a simple timestamp presenting the milliseconds of time passed |
| 32 | + * since the given time point. |
| 33 | + * |
| 34 | + * @param beginning The time point used as a reference for the time interval |
| 35 | + * calculation. |
| 36 | + * @return The amount of milliseconds passed since the given time point. |
| 37 | + */ |
| 38 | +inline uint64_t time_since( |
| 39 | + const std::chrono::steady_clock::time_point beginning) noexcept |
| 40 | +{ |
| 41 | + return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 42 | + std::chrono::steady_clock::now() - beginning) |
| 43 | + .count(); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Returns the measured number of milliseconds passed since UNIX epoch |
| 48 | + * (1 January 1970 01:00:00.000). This function uses system clock. |
| 49 | + * |
| 50 | + * @return Number of milliseconds since epoch. |
| 51 | + * @see ADAPRO::Library::now |
| 52 | + */ |
| 53 | +inline uint64_t epoch_time() noexcept |
| 54 | +{ |
| 55 | + return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 56 | + std::chrono::system_clock::now().time_since_epoch()) |
| 57 | + .count(); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Returns a timestamp using steady clock. This function is suitable for |
| 62 | + * measuring time intervals, but it's not meant to be used for calculating |
| 63 | + * dates. |
| 64 | + * |
| 65 | + * @return |
| 66 | + * @see ADAPRO::Library::epoch_time |
| 67 | + */ |
| 68 | +inline uint64_t now() noexcept |
| 69 | +{ |
| 70 | + return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 71 | + std::chrono::steady_clock::now().time_since_epoch()) |
| 72 | + .count(); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Returns a timestamp of the current point of time in the local timezone. |
| 77 | + * |
| 78 | + * @return A simple ISO-8601-esque timestamp (<tt>YYYY-MM-DD HH:MM:SS</tt>). |
| 79 | + * Every decimal number in the date string has leading zeros and therefore |
| 80 | + * fixed length. |
| 81 | + * @see ADAPRO::Control::fs_timestamp |
| 82 | + */ |
| 83 | +inline std::string timestamp() noexcept |
| 84 | +{ |
| 85 | + char buffer[20]; |
| 86 | + std::time_t now = std::time(nullptr); |
| 87 | + std::strftime(buffer, 32, "%F %T", std::localtime(&now)); |
| 88 | + return std::string(buffer); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Returns a timestamp of the current point of time in the local timezone. |
| 93 | + * The format of the timestamp is specified with the parameter |
| 94 | + * <tt>format</tt>. The format of the format string is the same as is used |
| 95 | + * by <tt>std::strftime</tt>. |
| 96 | + * |
| 97 | + * @return A simple timestamp in a format specified with the parameter |
| 98 | + * <tt>format</tt>. |
| 99 | + */ |
| 100 | +inline std::string timestamp(const std::string& format) noexcept |
| 101 | +{ |
| 102 | + char buffer[20]; |
| 103 | + std::time_t now = std::time(nullptr); |
| 104 | + std::strftime(buffer, 32, format.c_str(), std::localtime(&now)); |
| 105 | + return std::string(buffer); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Generates a simple timestamp usable file paths. This function is like |
| 110 | + * <tt>ADAPRO::Control::timestamp</tt>, but with spaces replaced with |
| 111 | + * underscores and colons with dots in order to ensure compatibility with |
| 112 | + * (Linux) filesystems. This function uses local timezone. |
| 113 | + * |
| 114 | + * @return A simple ISO-8601-esque timestamp (<tt>YYYY-MM-DD_HH.MM.SS</tt>). |
| 115 | + * Every decimal number in the date string has leading zeros and therefore |
| 116 | + * fixed length. |
| 117 | + * @see ADAPRO::Control::timestamp |
| 118 | + */ |
| 119 | +inline std::string fs_timestamp() noexcept |
| 120 | +{ |
| 121 | + char buffer[20]; |
| 122 | + std::time_t now = std::time(nullptr); |
| 123 | + std::strftime(buffer, 32, "%F_%H.%M.%S", std::localtime(&now)); |
| 124 | + return std::string(buffer); |
| 125 | +} |
| 126 | +} // namespace dcs |
| 127 | +} // namespace o2 |
| 128 | + |
| 129 | +#endif /* O2_DCS_CLOCK_H */ |
0 commit comments