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