fintp_utils
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TimeUtil.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef TIMEUTIL_H
22 #define TIMEUTIL_H
23 
24 #include <sys/timeb.h>
25 #include <ctime>
26 #include <iostream>
27 #include <string>
28 
29 #include "DllMainUtils.h"
30 
31 using namespace std;
32 
33 namespace FinTP
34 {
35  //Class to convert time to string and to compute difference between two moments
37  {
38  public :
39  class TimeMarker
40  {
41  public :
42 
43  inline TimeMarker()
44  {
45  ftime( &m_Marker );
46  }
47  explicit inline TimeMarker( const struct timeb& timeMarker ) : m_Marker( timeMarker ) {}
48 
49  //return difference in miliseconds between parameter and member
50  inline double operator-( const TimeUtil::TimeMarker& startMarker )
51  {
52  return difftime( m_Marker.time, startMarker.m_Marker.time )*1000 + m_Marker.millitm - startMarker.m_Marker.millitm;
53  }
54 
55  private :
56 
57  struct timeb m_Marker;
58  };
59 
60  private :
61  TimeUtil(){};
62 
63  public :
64 
65  //Convert current time to string in "timeformat" form
66  inline static string Get( const string& timeformat, const unsigned int bufferSize )
67  {
68  time_t acttime;
69 
70  acttime = time( NULL );
71  return Get( timeformat, bufferSize, &acttime );
72  }
73 
74  //Convert "acttime" to string in "timeformat" form
75  inline static string Get( const string& timeformat, const unsigned int bufferSize, const time_t* acttime )
76  {
77  struct tm timeptr;
78 
79  #ifdef CRT_SECURE
80  int localtimeResult = localtime_s( &timeptr, acttime );
81  if ( 0 != localtimeResult )
82  cerr << "Unable to aquire local time [" << localtimeResult << "]";
83  #else
84  timeptr = *localtime( acttime );
85  #endif
86 
87  char* formattedTime = new char[ bufferSize + 1 ];
88 
89  strftime( formattedTime, bufferSize + 1, timeformat.c_str(), &timeptr );
90  string returnValue( formattedTime, bufferSize );
91 
92  if ( formattedTime != NULL )
93  delete[] formattedTime;
94 
95  return returnValue;
96  }
97 
98  // compares diference beetween time2 and time1 with interval , all in string format
99  static double Compare( const string& time1, const string& time2, const string& format );
100  };
101 }
102 
103 #endif // TIMEUTIL_H