clockValue.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "clockValue.H"
29 #include "IOstreams.H"
30 
31 #include <sstream>
32 #include <iomanip>
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
37 :
38  value_(value_type::zero())
39 {}
40 
41 
42 Foam::clockValue::clockValue(const value_type& value)
43 :
44  value_(value)
45 {}
46 
47 
49 :
50  value_(value_type::zero())
51 {
52  if (useNow)
53  {
54  update();
55  }
56 }
57 
58 
59 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
60 
62 {
63  value_ = value_type::zero();
64 }
65 
66 
68 {
69  value_ = std::chrono::high_resolution_clock::now().time_since_epoch();
70 }
71 
72 
74 {
75  return (now() -= *this);
76 }
77 
78 
80 {
81  return std::chrono::duration_cast<std::chrono::seconds>(value_).count();
82 }
83 
84 
85 std::string Foam::clockValue::str() const
86 {
87  std::ostringstream os;
88 
89  // seconds
90  const unsigned long ss =
91  std::chrono::duration_cast<std::chrono::seconds>(value_).count();
92 
93  // days
94  const auto dd = (ss / 86400);
95 
96  // hours
97  const int hh = ((ss / 3600) % 24);
98 
99  if (dd) os << dd << '-';
100 
101  if (dd || hh)
102  {
103  os << std::setw(2) << std::setfill('0')
104  << hh << ':';
105  }
106 
107  // minutes
108  os << std::setw(2) << std::setfill('0')
109  << ((ss / 60) % 60) << ':';
110 
111  // seconds
112  os << std::setw(2) << std::setfill('0')
113  << (ss % 60);
114 
115  // milliseconds. As none or 3 decimal places
116  const long ms =
117  (
118  std::chrono::duration_cast<std::chrono::milliseconds>(value_).count()
119  - (ss * 1000)
120  );
121 
122  if (ms > 0)
123  {
124  os << '.' << std::setw(3) << std::setfill('0') << ms;
125  }
126 
127  return os.str();
128 }
129 
130 
131 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
132 
133 Foam::clockValue::operator double () const
134 {
135  return
136  (
137  (double(value_.count()) * value_type::period::num)
138  / value_type::period::den
139  );
140 }
141 
142 
144 {
145  value_ -= rhs.value_;
146  return *this;
147 }
148 
149 
151 {
152  value_ += rhs.value_;
153  return *this;
154 }
155 
156 
157 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
158 
160 {
161  return clockValue(a.value() - b.value());
162 }
163 
164 
166 {
167  return clockValue(a.value() + b.value());
168 }
169 
170 
171 // ************************************************************************* //
Foam::clockValue::seconds
long seconds() const
The value in seconds (rounded)
Definition: clockValue.C:79
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::clockValue::operator-=
clockValue & operator-=(const clockValue &rhs)
Subtract time value.
Definition: clockValue.C:143
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:404
Foam::clockValue::elapsed
clockValue elapsed() const
The time elapsed from now() since the start time.
Definition: clockValue.C:73
Foam::clockValue::clockValue
clockValue()
Construct zero initialized.
Definition: clockValue.C:36
Foam::clockValue::value
const value_type & value() const
Return the value.
Definition: clockValue.H:87
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::clockValue::update
void update()
Update to the current now() time from the system.
Definition: clockValue.C:67
clockValue.H
Foam::clockValue::str
std::string str() const
Format as day-hh:mm:ss string.
Definition: clockValue.C:85
Foam::clockValue
Access to high-resolution clock value with some basic operations.
Definition: clockValue.H:51
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::clockValue::clear
void clear()
Reset to zero.
Definition: clockValue.C:61
Foam::setfill
Omanip< char > setfill(char fillch)
Definition: IOmanip.H:175
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::clockValue::operator+=
clockValue & operator+=(const clockValue &rhs)
Add time value.
Definition: clockValue.C:150
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61