Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Jeep/JDate.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JDATE__
2 #define __JEEP__JDATE__
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 date.
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 JDate :
31  public JLANG::JComparable< JDate<JSeparator_t> >
32  {
33  /**
34  * Separation character.
35  */
36  static const char SEPARATOR = JSeparator_t;
37 
38 
39  /**
40  * Default constructor.
41  */
42  JDate() :
43  year (0),
44  month(0),
45  day (0)
46  {}
47 
48 
49  /**
50  * Constructor.
51  *
52  * \param year year
53  * \param month month
54  * \param day day
55  */
56  JDate(const int year,
57  const int month,
58  const int day) :
59  year (year),
60  month(month),
61  day (day)
62  {}
63 
64 
65  /**
66  * Constructor.
67  *
68  * \param date date
69  */
70  JDate(const std::string& date)
71  {
72  std::istringstream in(date);
73 
74  in >> *this;
75  }
76 
77 
78  static JDate min() { return JDate(-std::numeric_limits<int>::max(), 1, 1); } //!< Minimal date
79  static JDate max() { return JDate(+std::numeric_limits<int>::max(), 12, 31); } //!< Maximal date
80 
81 
82  /**
83  * Less-than method.
84  *
85  * \param date date
86  * \return true if this date earlier than given date; else false
87  */
88  bool less(const JDate& date) const
89  {
90  if (this->year == date.year) {
91 
92  if (this->month == date.month)
93  return this->day < date.day;
94  else
95  return this->month < date.month;
96 
97  } else {
98 
99  return this->year < date.year;
100  }
101  }
102 
103 
104  /**
105  * Read date from input stream.
106  *
107  * \param in input stream
108  * \param object date
109  * \return input stream
110  */
111  friend inline std::istream& operator>>(std::istream& in, JDate& object)
112  {
113  using namespace std;
114 
115  object = JDate();
116 
117  string buffer;
118 
119  if (in >> buffer) {
120 
121  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
122  if (*i == JDate::SEPARATOR) {
123  *i = ' ';
124  }
125  }
126 
127  istringstream is(buffer);
128 
129  if (! (is >> object.year >> object.month >> object.day)) {
130  in.setstate(is.rdstate());
131  }
132  }
133 
134  return in;
135  }
136 
137 
138  /**
139  * Write date to output stream.
140  *
141  * \param out output stream
142  * \param object date
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JDate& object)
146  {
147  return out << FILL(4,'0') << object.year << JDate::SEPARATOR
148  << FILL(2,'0') << object.month << JDate::SEPARATOR
149  << FILL(2,'0') << object.day << FILL();
150  }
151 
152 
153  int year; //!< year
154  int month; //!< month
155  int day; //!< day
156  };
157 }
158 
159 #endif
static const char SEPARATOR
Separation character.
Definition: Jeep/JDate.hh:36
friend std::istream & operator>>(std::istream &in, JDate &object)
Read date from input stream.
Definition: Jeep/JDate.hh:111
friend std::ostream & operator<<(std::ostream &out, const JDate &object)
Write date to output stream.
Definition: Jeep/JDate.hh:145
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
is
Definition: JDAQCHSM.chsm:167
static JDate min()
Minimal date.
Definition: Jeep/JDate.hh:78
I/O formatting auxiliaries.
JDate()
Default constructor.
Definition: Jeep/JDate.hh:42
static JDate max()
Maximal date.
Definition: Jeep/JDate.hh:79
Auxiliary class for simple date.
Definition: Jeep/JDate.hh:30
JDate(const int year, const int month, const int day)
Constructor.
Definition: Jeep/JDate.hh:56
int year
year
Definition: Jeep/JDate.hh:153
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
JDate(const std::string &date)
Constructor.
Definition: Jeep/JDate.hh:70
int month
month
Definition: Jeep/JDate.hh:154
bool less(const JDate &date) const
Less-than method.
Definition: Jeep/JDate.hh:88
int day
day
Definition: Jeep/JDate.hh:155