clock.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) 2011 OpenFOAM Foundation
9 Copyright (C) 2018 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "clock.H"
30#include "string.H"
31
32#include <sstream>
33#include <iomanip>
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37static const char *monthNames[] =
38{
39 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
40 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
41};
42
43
44// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
45
47{
48 return ::time(reinterpret_cast<time_t*>(0));
49}
50
51
52const struct tm Foam::clock::rawDate()
53{
54 time_t t = getTime();
55 struct tm *curr = ::localtime(&t);
56 return *curr;
57}
58
59
61{
62 time_t t = getTime();
63 struct tm *curr = ::localtime(&t);
64
65 std::ostringstream os;
66 os
67 << std::setfill('0')
68 << std::setw(4) << curr->tm_year + 1900
69 << '-' << std::setw(2) << curr->tm_mon + 1
70 << '-' << std::setw(2) << curr->tm_mday
71 << 'T'
72 << std::setw(2) << curr->tm_hour
73 << ':' << std::setw(2) << curr->tm_min
74 << ':' << std::setw(2) << curr->tm_sec;
75
76 return os.str();
77}
78
79
80std::string Foam::clock::date()
81{
82 time_t t = getTime();
83 struct tm *curr = ::localtime(&t);
84
85 std::ostringstream os;
86 os
87 << monthNames[curr->tm_mon]
88 << ' ' << std::setw(2) << std::setfill('0') << curr->tm_mday
89 << ' ' << std::setw(4) << curr->tm_year + 1900;
90
91 return os.str();
92}
93
94
96{
97 time_t t = getTime();
98 struct tm *curr = ::localtime(&t);
99
100 std::ostringstream os;
101 os
102 << std::setfill('0')
103 << std::setw(2) << curr->tm_hour
104 << ':' << std::setw(2) << curr->tm_min
105 << ':' << std::setw(2) << curr->tm_sec;
106
107 return os.str();
108}
109
110
111// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112
114:
115 start_(getTime()),
116 last_(start_)
117{}
118
119
120// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121
123{
124 last_ = getTime();
125 return ::difftime(last_, start_);
126}
127
128
130{
131 const auto prev(last_);
132
133 last_ = getTime();
134 return ::difftime(last_, prev);
135}
136
137
138// ************************************************************************* //
double clockTimeIncrement() const
Returns wall-clock time since last clockTimeIncrement() call.
Definition: clock.C:129
double elapsedClockTime() const
Returns wall-clock time since clock instantiation.
Definition: clock.C:122
static time_t getTime()
Get the current clock time in seconds.
Definition: clock.C:46
static std::string date()
Definition: clock.C:80
static std::string dateTime()
Definition: clock.C:60
static const struct tm rawDate()
The current wall-clock date as a raw struct.
Definition: clock.C:52
static std::string clockTime()
Definition: clock.C:95
clock()
Construct with the current clock time for the start point.
Definition: clock.C:113
static const char * monthNames[]
Definition: clock.C:37
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.