All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Time.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_TIME_H_
23 #define _BLAZE_UTIL_TIME_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #if defined(_MSC_VER)
31 # ifndef NOMINMAX
32 # define NOMINMAX
33 # endif
34 # include <windows.h>
35 # include <winsock.h>
36 # include <time.h>
37 # include <sys/timeb.h>
38 #else
39 # include <sys/resource.h>
40 # include <sys/time.h>
41 # include <sys/types.h>
42 #endif
43 #include <ctime>
44 #include <string>
45 
46 
47 namespace blaze {
48 
49 //=================================================================================================
50 //
51 // TIME FUNCTIONS
52 //
53 //=================================================================================================
54 
55 //*************************************************************************************************
58 inline std::string getDate();
59 inline std::string getTime();
60 inline double getWcTime();
61 inline double getCpuTime();
63 //*************************************************************************************************
64 
65 
66 //*************************************************************************************************
72 inline std::string getDate()
73 {
74  std::time_t t;
75  std::tm* localTime;
76  char c[50];
77 
78  std::time( &t );
79  localTime = std::localtime( &t );
80  std::strftime( c, 50, "%Y-%m-%d", localTime );
81 
82  return std::string( c );
83 }
84 //*************************************************************************************************
85 
86 
87 //*************************************************************************************************
93 inline std::string getTime()
94 {
95  std::time_t t;
96  std::tm* localTime;
97  char c[50];
98 
99  std::time( &t );
100  localTime = std::localtime( &t );
101  std::strftime( c, 50, "%A, %d.%B %Y, %H:%M", localTime );
102 
103  return std::string( c );
104 }
105 //*************************************************************************************************
106 
107 
108 //*************************************************************************************************
114 inline double getWcTime()
115 {
116 #ifdef WIN32
117  struct _timeb timeptr;
118  _ftime( &timeptr );
119  return ( static_cast<double>( timeptr.time ) + static_cast<double>( timeptr.millitm )/1E3 );
120 #else
121  struct timeval tp;
122  gettimeofday( &tp, NULL );
123  return ( static_cast<double>( tp.tv_sec ) + static_cast<double>( tp.tv_usec )/1E6 );
124 #endif
125 }
126 //*************************************************************************************************
127 
128 
129 //*************************************************************************************************
135 inline double getCpuTime()
136 {
137 #ifdef WIN32
138  FILETIME CreateTime, ExitTime, KernelTime, UserTime;
139  SYSTEMTIME SysTime;
140 
141  if( GetProcessTimes( GetCurrentProcess(), &CreateTime, &ExitTime, &KernelTime, &UserTime ) != TRUE ) {
142  return 0.0;
143  }
144  else {
145  FileTimeToSystemTime( &UserTime, &SysTime );
146  return ( static_cast<double>( SysTime.wSecond ) + static_cast<double>( SysTime.wMilliseconds )/1E3 );
147  }
148 #else
149  struct rusage ruse;
150  getrusage( RUSAGE_SELF, &ruse );
151  return ( static_cast<double>( ruse.ru_utime.tv_sec ) + static_cast<double>( ruse.ru_utime.tv_usec )/1E6 );
152 #endif
153 }
154 //*************************************************************************************************
155 
156 } // namespace blaze
157 
158 #endif