Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
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 <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 date.
29  *
30  * The corresponding ASCII format is <tt>yyyy<separator>mm<separator>dd</tt>,\n
31  * where
32  * <tt>yyyy</tt> corresponds to the year,
33  * <tt>mm</tt> to the month and
34  * <tt>dd</tt> to the day.\n
35  * The separator is defined by the template parameter.
36  */
37  template<char JSeparator_t>
38  struct JDate :
39  public JComparable< JDate<JSeparator_t> >
40  {
41  /**
42  * Separation character.
43  */
44  static const char SEPARATOR = JSeparator_t;
45 
46 
47  /**
48  * Default constructor.
49  */
50  JDate() :
51  year (0),
52  month(0),
53  day (0)
54  {}
55 
56 
57  /**
58  * Constructor.
59  *
60  * \param year year
61  * \param month month
62  * \param day day
63  */
64  JDate(const int year,
65  const int month,
66  const int day) :
67  year (year),
68  month(month),
69  day (day)
70  {}
71 
72 
73  /**
74  * Constructor.
75  *
76  * \param date date
77  */
78  JDate(const std::string& date)
79  {
80  std::istringstream in(date);
81 
82  in >> *this;
83  }
84 
85 
86  static JDate min() { return JDate(std::numeric_limits<int>::lowest(), 1, 1); } //!< Minimal date
87  static JDate max() { return JDate(std::numeric_limits<int>::max(), 12, 31); } //!< Maximal date
88 
89 
90  /**
91  * Less-than method.
92  *
93  * \param date date
94  * \return true if this date earlier than given date; else false
95  */
96  bool less(const JDate& date) const
97  {
98  if (this->year == date.year) {
99 
100  if (this->month == date.month)
101  return this->day < date.day;
102  else
103  return this->month < date.month;
104 
105  } else {
106 
107  return this->year < date.year;
108  }
109  }
110 
111 
112  /**
113  * Read date from input stream.
114  *
115  * \param in input stream
116  * \param object date
117  * \return input stream
118  */
119  friend inline std::istream& operator>>(std::istream& in, JDate& object)
120  {
121  using namespace std;
122  using namespace JPP;
123 
124  object = JDate();
125 
126  while (isspace(in.peek())) { in.ignore(); }
127 
128  const locale loc = in.imbue(locale(in.getloc(), new JWhiteSpacesFacet(in.getloc(), JDate::SEPARATOR)));
129 
130  in >> object.year >> object.month >> object.day;
131 
132  in.imbue(loc);
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
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 date.
Definition: JDate.hh:40
static JDate max()
Maximal date.
Definition: JDate.hh:87
friend std::ostream & operator<<(std::ostream &out, const JDate &object)
Write date to output stream.
Definition: JDate.hh:145
JDate()
Default constructor.
Definition: JDate.hh:50
int year
year
Definition: JDate.hh:153
int month
month
Definition: JDate.hh:154
int day
day
Definition: JDate.hh:155
static const char SEPARATOR
Separation character.
Definition: JDate.hh:44
JDate(const std::string &date)
Constructor.
Definition: JDate.hh:78
friend std::istream & operator>>(std::istream &in, JDate &object)
Read date from input stream.
Definition: JDate.hh:119
static JDate min()
Minimal date.
Definition: JDate.hh:86
JDate(const int year, const int month, const int day)
Constructor.
Definition: JDate.hh:64
bool less(const JDate &date) const
Less-than method.
Definition: JDate.hh:96
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139