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 -------------------------------------------------------------------------------
11 License
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 
31 #include <string>
32 #include <sstream>
33 #include <iomanip>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 static 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 
46 time_t Foam::clock::getTime()
47 {
48  return ::time(reinterpret_cast<time_t*>(0));
49 }
50 
51 
52 const struct tm Foam::clock::rawDate()
53 {
54  time_t t = getTime();
55  struct tm *curr = ::localtime(&t);
56  return *curr;
57 }
58 
59 
60 std::string Foam::clock::dateTime()
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 
80 std::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 
95 std::string Foam::clock::clockTime()
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;
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 
129 double Foam::clock::clockTimeIncrement() const
130 {
131  const auto prev(last_);
132 
133  last_ = getTime();
134  return ::difftime(last_, prev);
135 }
136 
137 
138 // ************************************************************************* //
static const char * monthNames[]
Definition: clock.C:30
Omanip< char > setfill(char fillch)
Definition: IOmanip.H:175
static time_t getTime()
Get the current clock time in seconds.
Definition: clock.C:39
clock()
Construct with the current clock time for the start point.
Definition: clock.C:106
static std::string date()
The current wall-clock date as a string formatted as (MON dd yyyy), where MON is Jan, Feb, etc.
Definition: clock.C:73
static const struct tm rawDate()
The current wall-clock date as a raw struct.
Definition: clock.C:45
double elapsedClockTime() const
Returns wall-clock time since clock instantiation.
Definition: clock.C:115
static std::string dateTime()
The current wall-clock date/time (in local time) as a string in ISO-8601 format (yyyy-mm-ddThh:mm:ss)...
Definition: clock.C:53
static std::string clockTime()
The current wall-clock (in local time) as a string formatted as as (hh:mm:ss).
Definition: clock.C:88
OBJstream os(runTime.globalPath()/outputName)
double clockTimeIncrement() const
Returns wall-clock time since last clockTimeIncrement() call.
Definition: clock.C:122
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Namespace for OpenFOAM.