Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Jeep/JTime.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JTIME__
2 #define __JEEP__JTIME__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <sstream>
8 #include <limits>
9 
10 #include "JLang/JComparable.hh"
11 
12 #include "Jeep/JPrint.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 namespace JEEP {}
19 namespace JPP { using namespace JEEP; }
20 
21 namespace JEEP {
22 
23  /**
24  * Auxiliary class for simple time.
25  *
26  * The corresponding ASCII format is <tt>yyyy<separator>mm<separator>dd</tt>.
27  * where the separator is defined by the template parameter.
28  */
29  template<char JSeparator_t>
30  struct JTime :
31  public JLANG::JComparable< JTime<JSeparator_t> >
32  {
33  /**
34  * Separation character.
35  */
36  static const char SEPARATOR = JSeparator_t;
37 
38 
39  /**
40  * Default constructor.
41  */
42  JTime() :
43  hour (0),
44  minute(0),
45  second(0)
46  {}
47 
48 
49  /**
50  * Constructor.
51  *
52  * \param hour hour
53  * \param minute minute
54  * \param second second
55  */
56  JTime(const int hour,
57  const int minute,
58  const int second) :
59  hour (hour),
60  minute(minute),
61  second(second)
62  {}
63 
64 
65  /**
66  * Constructor.
67  *
68  * \param time time
69  */
70  JTime(const std::string& time)
71  {
72  std::istringstream in(time);
73 
74  in >> *this;
75  }
76 
77 
78  static JTime min() { return JTime(-std::numeric_limits<int>::max(), 1, 1); } //!< Minimal time
79  static JTime max() { return JTime(+std::numeric_limits<int>::max(), 12, 31); } //!< Maximal time
80 
81 
82  /**
83  * Less-than method.
84  *
85  * \param time time
86  * \return true if this time earlier than given time; else false
87  */
88  bool less(const JTime& time) const
89  {
90  if (this->hour == time.hour) {
91 
92  if (this->minute == time.minute)
93  return this->second < time.second;
94  else
95  return this->minute < time.minute;
96 
97  } else {
98 
99  return this->hour < time.hour;
100  }
101  }
102 
103 
104  /**
105  * Read time from input stream.
106  *
107  * \param in input stream
108  * \param object time
109  * \return input stream
110  */
111  friend inline std::istream& operator>>(std::istream& in, JTime& object)
112  {
113  using namespace std;
114 
115  object = JTime();
116 
117  string buffer;
118 
119  if (in >> buffer) {
120 
121  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
122  if (*i == JTime::SEPARATOR) {
123  *i = ' ';
124  }
125  }
126 
127  istringstream is(buffer);
128 
129  if (! (is >> object.hour >> object.minute >> object.second)) {
130  in.setstate(is.rdstate());
131  }
132  }
133 
134  return in;
135  }
136 
137 
138  /**
139  * Write time to output stream.
140  *
141  * \param out output stream
142  * \param object time
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JTime& object)
146  {
147  return out << FILL(4,'0') << object.hour << JTime::SEPARATOR
148  << FILL(2,'0') << object.minute << JTime::SEPARATOR
149  << FILL(2,'0') << object.second << FILL();
150  }
151 
152 
153  int hour; //!< hour
154  int minute; //!< minute
155  int second; //!< second
156  };
157 }
158 
159 #endif
friend std::ostream & operator<<(std::ostream &out, const JTime &object)
Write time to output stream.
Definition: Jeep/JTime.hh:145
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
static JTime max()
Maximal time.
Definition: Jeep/JTime.hh:79
is
Definition: JDAQCHSM.chsm:167
I/O formatting auxiliaries.
JTime(const int hour, const int minute, const int second)
Constructor.
Definition: Jeep/JTime.hh:56
bool less(const JTime &time) const
Less-than method.
Definition: Jeep/JTime.hh:88
int second
second
Definition: Jeep/JTime.hh:155
int minute
minute
Definition: Jeep/JTime.hh:154
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
Auxiliary data structure for sequence of same character.
Definition: JPrint.hh:361
static JTime min()
Minimal time.
Definition: Jeep/JTime.hh:78
JTime(const std::string &time)
Constructor.
Definition: Jeep/JTime.hh:70
static const char SEPARATOR
Separation character.
Definition: Jeep/JTime.hh:36
int hour
hour
Definition: Jeep/JTime.hh:153
JTime()
Default constructor.
Definition: Jeep/JTime.hh:42
Auxiliary class for simple time.
Definition: Jeep/JTime.hh:30
friend std::istream & operator>>(std::istream &in, JTime &object)
Read time from input stream.
Definition: Jeep/JTime.hh:111