1 //   Copyright (c)  2005,2013  John Abbott
2 
3 //   This file is part of the source of CoCoALib, the CoCoA Library.
4 
5 //   CoCoALib is free software: you can redistribute it and/or modify
6 //   it under the terms of the GNU General Public License as published by
7 //   the Free Software Foundation, either version 3 of the License, or
8 //   (at your option) any later version.
9 
10 //   CoCoALib is distributed in the hope that it will be useful,
11 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //   GNU General Public License for more details.
14 
15 //   You should have received a copy of the GNU General Public License
16 //   along with CoCoALib.  If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #include "CoCoA/time.H"
20 
21 #if defined(__MINGW32__) || defined(_WINDOWS)
22 #include <ctime>
23 // this work for linux too
SecondsFromClock()24 double SecondsFromClock()
25 {
26   const double floatSpan = clock();
27   const double seconds = (floatSpan / CLOCKS_PER_SEC);
28   return seconds;
29 }
30 
31 
32 // Non Unix-like environment, so use "lobotomized" time fns.
33 namespace CoCoA
34 {
35 
CpuTime()36   double CpuTime()
37   {
38     return SecondsFromClock();
39   }
40 
RealTime()41   double RealTime()
42   {
43     return 0.0;
44   }
45 
46 
DateTime(long & date,long & time)47   void DateTime(long& date, long& time)
48   {
49     date = 0;
50     time = 0;
51   }
52 
53 } // end of namespace CoCoA
54 
55 #else
56 
57 // Good news: we're in a unix-like environment.
58 // These are the normal defns.
59 
60 #include <ctime>
61 using std::time;
62 #include <sys/time.h>
63 #include <sys/resource.h>
64 //not needed???? #include <unistd.h>
65 
66 namespace CoCoA
67 {
68 
CpuTime()69   double CpuTime()
70   {
71     static struct rusage used;
72 
73     getrusage(RUSAGE_SELF, &used);
74     // Use volatile to force truncation to IEEE doubles in x86 processors;
75     // otherwise it is possible to get negative time differences!
76     volatile double seconds = used.ru_utime.tv_sec + used.ru_stime.tv_sec +
77       (used.ru_utime.tv_usec + used.ru_stime.tv_usec) / 1.0e6;
78     return seconds;
79   }
80 
RealTime()81   double RealTime()
82   {
83     static struct timeval tv;
84     gettimeofday(&tv, nullptr);
85     // Use volatile to force truncation to IEEE doubles in x86 processors;
86     // otherwise it is possible to get negative time differences!
87     volatile double seconds = tv.tv_sec + tv.tv_usec/1.0e6;
88     return seconds;
89   }
90 
DateTime(long & date,long & time)91   void DateTime(long& date, long& time)
92   {
93     std::time_t SecondsSinceEpoch;
94     std::time(&SecondsSinceEpoch); // gets current time - Async-signal-safe
95 //   const string date = std::ctime_r(&SecondsSinceEpoch);
96 //   cout << "Time & date: " << date << endl;
97 
98     std::tm CurrentTime;
99     localtime_r(&SecondsSinceEpoch, &CurrentTime); // thread-safe
100     const int year = CurrentTime.tm_year + 1900;
101     const int month = CurrentTime.tm_mon + 1;
102     const int day = CurrentTime.tm_mday;
103     const int hour = CurrentTime.tm_hour;
104     const int minute = CurrentTime.tm_min;
105     const int second = CurrentTime.tm_sec;
106 
107     date = 10000*year + 100*month + day;
108     time = 10000*hour + 100*minute + second;
109   }
110 
111 
112 } // end of namespace CoCoA
113 
114 
115 #endif
116 
117 
118 // RCS header/log in the next few lines
119 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/src/AlgebraicCore/time.C,v 1.10 2019/03/19 11:07:08 abbott Exp $
120 // $Log: time.C,v $
121 // Revision 1.10  2019/03/19 11:07:08  abbott
122 // Summary: Replaced 0 by nullptr where appropriate
123 //
124 // Revision 1.9  2013/05/20 15:57:54  abbott
125 // Corrected copyright date.
126 //
127 // Revision 1.8  2013/05/15 09:33:11  bigatti
128 // -- added tentative CpuTime for windows
129 //
130 // Revision 1.7  2012/04/02 15:29:09  abbott
131 // Added extra CPP check, so that lobotomized versions are used on Windoze.
132 //
133 // Revision 1.6  2011/10/04 13:03:02  bigatti
134 // -- new logo for gui
135 //
136 // Revision 1.5  2011/07/15 16:54:24  abbott
137 // Added some missing consts.
138 //
139 // Revision 1.4  2011/07/06 15:49:40  bigatti
140 // -- added DateTime
141 //
142 // Revision 1.3  2011/06/04 08:43:43  abbott
143 // Added MINGW workaround.
144 //
145 // Revision 1.2  2007/10/30 17:14:06  abbott
146 // Changed licence from GPL-2 only to GPL-3 or later.
147 // New version for such an important change.
148 //
149 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
150 // Imported files
151 //
152 // Revision 1.1.1.1  2006/05/30 11:39:37  cocoa
153 // Imported files
154 //
155 // Revision 1.1  2005/11/15 12:24:35  cocoa
156 // New timing functions.
157 //
158