gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Time.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef TIME_H_
18 #define TIME_H_
19 
20 #ifdef _MSC_VER
21 //#error On microsoft, please rename this file to TimeTools.H and delete this line.
22 #endif
23 
24 #include <glib.h>
25 
26 #include <strstream>
27 using namespace std;
28 
32 template <int dummy>
33 class TimeFormat {
34 protected:
35  static char timeFormat[];
36 };
37 template <int dummy> char TimeFormat<dummy>::timeFormat[]="%Y-%m-%d.%H:%M:%S";
38 
41 class Time : protected TimeFormat<0> {
42  GTimeVal time;
43 public:
44  // Constructor
45  Time() {time.tv_sec=time.tv_usec=0;}
46 
51  Time(long sec, long usec) {
52  time.tv_sec=sec;
53  time.tv_usec=usec;
54  }
55 
56  virtual ~Time() {}
57 
61  glong getSecs() const {return time.tv_sec;}
62 
66  glong getUSecs() const {return time.tv_usec;}
67 
72  g_get_current_time(&time);
73  return *this;
74  }
75 
80  Time operator-(const Time &t){
81  return Time(time.tv_sec-t.getSecs(), time.tv_usec-t.getUSecs());
82  }
83 
88  Time &operator-=(const Time &t){
89  time.tv_sec-=t.getSecs(); time.tv_usec-=t.getUSecs();
90  return *this;
91  }
92 
97  Time operator+(const Time &t){
98  return Time(time.tv_sec+t.getSecs(), time.tv_usec+t.getUSecs());
99  }
100 
105  Time &operator+=(const Time &t){
106  time.tv_sec+=t.getSecs(); time.tv_usec+=t.getUSecs();
107  return *this;
108  }
109 
110  friend ostream &operator<<(ostream &o, const Time &t){
111  ostrstream oss;
112  long double tDD=(long double)t.getSecs()+(long double)t.getUSecs()*1.e-6;
113  oss<<tDD<<'\0';
114  o<<oss.str();
115  return o;
116  }
117 
122  static string getTimeString(){
123  return getTimeString(NULL);
124  }
125 
130  static string getTimeString(char *formatStr){
131  time_t timeNow; ::time(&timeNow); // find out the time now as a nice string
132  struct tm *tmLocal = localtime(&timeNow);
133  //cout<<tmLocal<<endl;
134  int maxSize=128;
135  string strTime(maxSize, '\0'); // the string to hold the current time.
136  int ret;
137  if (formatStr==NULL)
138  ret=strftime((char*)strTime.c_str(), maxSize, timeFormat, tmLocal);
139  else
140  ret=strftime((char*)strTime.c_str(), maxSize, formatStr, tmLocal);
141  if (ret<=0)
142  strTime="";
143  else
144  strTime[ret]='\0';
145  //cout<<strTime<<endl;
146  return strTime;
147  }
148 };
149 #endif // TIME_H_
Time & getTime()
Definition: Time.H:71
Time & operator-=(const Time &t)
Definition: Time.H:88
Time & operator+=(const Time &t)
Definition: Time.H:105
Time(long sec, long usec)
Definition: Time.H:51
Definition: Time.H:41
STL namespace.
Time()
Definition: Time.H:45
glong getSecs() const
Definition: Time.H:61
virtual ~Time()
Definition: Time.H:56
Time operator-(const Time &t)
Definition: Time.H:80
static string getTimeString(char *formatStr)
Definition: Time.H:130
friend ostream & operator<<(ostream &o, const Time &t)
Definition: Time.H:110
Time operator+(const Time &t)
Definition: Time.H:97
glong getUSecs() const
Definition: Time.H:66
static string getTimeString()
Definition: Time.H:122
GTimeVal time
Definition: Time.H:42
gtkIOStream: /tmp/gtkiostream/include/Time.H Source File
GTK+ IOStream  Beta