Jpp  19.1.0
the software that should make you happy
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 <cctype>
9 #include <limits>
10 
11 #include "JLang/JComparable.hh"
13 #include "JLang/JManip.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 namespace JEEP {}
20 namespace JPP { using namespace JEEP; }
21 
22 namespace JEEP {
23 
24  using JLANG::JComparable;
25 
26 
27  /**
28  * Auxiliary class for simple time.
29  *
30  * The corresponding ASCII format is <tt>hh<separator>mm<separator>ss</tt>,
31  * where
32  * <tt>hh</tt> corresponds to the hour,
33  * <tt>mm</tt> to the minutes and
34  * <tt>ss</tt> to the seconds.\n
35  * The separator is defined by the template parameter.
36  */
37  template<char JSeparator_t>
38  struct JTime :
39  public JComparable< JTime<JSeparator_t> >
40  {
41  /**
42  * Separation character.
43  */
44  static const char SEPARATOR = JSeparator_t;
45 
46 
47  /**
48  * Default constructor.
49  */
50  JTime() :
51  hour (0),
52  minute(0),
53  second(0)
54  {}
55 
56 
57  /**
58  * Constructor.
59  *
60  * \param hour hour
61  * \param minute minute
62  * \param second second
63  */
64  JTime(const int hour,
65  const int minute,
66  const int second) :
67  hour (hour),
68  minute(minute),
69  second(second)
70  {}
71 
72 
73  /**
74  * Constructor.
75  *
76  * \param time time
77  */
78  JTime(const std::string& time)
79  {
80  std::istringstream in(time);
81 
82  in >> *this;
83  }
84 
85 
86  static JTime min() { return JTime(std::numeric_limits<int>::lowest(), 1, 1); } //!< Minimal time
87  static JTime max() { return JTime(std::numeric_limits<int>::max(), 12, 31); } //!< Maximal time
88 
89 
90  /**
91  * Less-than method.
92  *
93  * \param time time
94  * \return true if this time earlier than given time; else false
95  */
96  bool less(const JTime& time) const
97  {
98  if (this->hour == time.hour) {
99 
100  if (this->minute == time.minute)
101  return this->second < time.second;
102  else
103  return this->minute < time.minute;
104 
105  } else {
106 
107  return this->hour < time.hour;
108  }
109  }
110 
111 
112  /**
113  * Read time from input stream.
114  *
115  * \param in input stream
116  * \param object time
117  * \return input stream
118  */
119  friend inline std::istream& operator>>(std::istream& in, JTime& object)
120  {
121  using namespace std;
122  using namespace JPP;
123 
124  object = JTime();
125 
126  while (isspace(in.peek())) { in.ignore(); }
127 
128  const locale loc = in.imbue(locale(in.getloc(), new JWhiteSpacesFacet(in.getloc(), JTime::SEPARATOR)));
129 
130  in >> object.hour >> object.minute >> object.second;
131 
132  in.imbue(loc);
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(2,'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
I/O manipulators.
Auxiliary class to specify white space character(s) in currect locale.
char * loc(char *orig)
General puprpose classes and methods.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:330
Auxiliary class for simple time.
Definition: Jeep/JTime.hh:40
bool less(const JTime &time) const
Less-than method.
Definition: Jeep/JTime.hh:96
int second
second
Definition: Jeep/JTime.hh:155
static const char SEPARATOR
Separation character.
Definition: Jeep/JTime.hh:44
JTime(const int hour, const int minute, const int second)
Constructor.
Definition: Jeep/JTime.hh:64
JTime(const std::string &time)
Constructor.
Definition: Jeep/JTime.hh:78
int minute
minute
Definition: Jeep/JTime.hh:154
JTime()
Default constructor.
Definition: Jeep/JTime.hh:50
static JTime max()
Maximal time.
Definition: Jeep/JTime.hh:87
friend std::ostream & operator<<(std::ostream &out, const JTime &object)
Write time to output stream.
Definition: Jeep/JTime.hh:145
int hour
hour
Definition: Jeep/JTime.hh:153
static JTime min()
Minimal time.
Definition: Jeep/JTime.hh:86
friend std::istream & operator>>(std::istream &in, JTime &object)
Read time from input stream.
Definition: Jeep/JTime.hh:119
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139