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