Jpp  16.0.0-rc.1
the software that should make you happy
 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 <cctype>
9 #include <limits>
10 
11 #include "JLang/JComparable.hh"
13 
14 #include "Jeep/JPrint.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 namespace JEEP {}
21 namespace JPP { using namespace JEEP; }
22 
23 namespace JEEP {
24 
25  using JLANG::JComparable;
26 
27 
28  /**
29  * Auxiliary class for simple date.
30  *
31  * The corresponding ASCII format is <tt>yyyy<separator>mm<separator>dd</tt>,\n
32  * where
33  * <tt>yyyy</tt> corresponds to the year,
34  * <tt>mm</tt> to the month and
35  * <tt>dd</tt> to the day.\n
36  * The separator is defined by the template parameter.
37  */
38  template<char JSeparator_t>
39  struct JDate :
40  public JComparable< JDate<JSeparator_t> >
41  {
42  /**
43  * Separation character.
44  */
45  static const char SEPARATOR = JSeparator_t;
46 
47 
48  /**
49  * Default constructor.
50  */
51  JDate() :
52  year (0),
53  month(0),
54  day (0)
55  {}
56 
57 
58  /**
59  * Constructor.
60  *
61  * \param year year
62  * \param month month
63  * \param day day
64  */
65  JDate(const int year,
66  const int month,
67  const int day) :
68  year (year),
69  month(month),
70  day (day)
71  {}
72 
73 
74  /**
75  * Constructor.
76  *
77  * \param date date
78  */
79  JDate(const std::string& date)
80  {
81  std::istringstream in(date);
82 
83  in >> *this;
84  }
85 
86 
87  static JDate min() { return JDate(std::numeric_limits<int>::lowest(), 1, 1); } //!< Minimal date
88  static JDate max() { return JDate(std::numeric_limits<int>::max(), 12, 31); } //!< Maximal date
89 
90 
91  /**
92  * Less-than method.
93  *
94  * \param date date
95  * \return true if this date earlier than given date; else false
96  */
97  bool less(const JDate& date) const
98  {
99  if (this->year == date.year) {
100 
101  if (this->month == date.month)
102  return this->day < date.day;
103  else
104  return this->month < date.month;
105 
106  } else {
107 
108  return this->year < date.year;
109  }
110  }
111 
112 
113  /**
114  * Read date from input stream.
115  *
116  * \param in input stream
117  * \param object date
118  * \return input stream
119  */
120  friend inline std::istream& operator>>(std::istream& in, JDate& object)
121  {
122  using namespace std;
123  using namespace JPP;
124 
125  object = JDate();
126 
127  while (isspace(in.peek())) { in.ignore(); }
128 
129  const locale loc = in.imbue(locale(in.getloc(), new JWhiteSpacesFacet(in.getloc(), JDate::SEPARATOR)));
130 
131  in >> object.year >> object.month >> object.day;
132 
133  in.imbue(loc);
134 
135  return in;
136  }
137 
138 
139  /**
140  * Write date to output stream.
141  *
142  * \param out output stream
143  * \param object date
144  * \return output stream
145  */
146  friend inline std::ostream& operator<<(std::ostream& out, const JDate& object)
147  {
148  return out << FILL(4,'0') << object.year << JDate::SEPARATOR
149  << FILL(2,'0') << object.month << JDate::SEPARATOR
150  << FILL(2,'0') << object.day << FILL();
151  }
152 
153 
154  int year; //!< year
155  int month; //!< month
156  int day; //!< day
157  };
158 }
159 
160 #endif
static const char SEPARATOR
Separation character.
Definition: Jeep/JDate.hh:45
friend std::istream & operator>>(std::istream &in, JDate &object)
Read date from input stream.
Definition: Jeep/JDate.hh:120
friend std::ostream & operator<<(std::ostream &out, const JDate &object)
Write date to output stream.
Definition: Jeep/JDate.hh:146
static JDate min()
Minimal date.
Definition: Jeep/JDate.hh:87
I/O formatting auxiliaries.
JDate()
Default constructor.
Definition: Jeep/JDate.hh:51
static JDate max()
Maximal date.
Definition: Jeep/JDate.hh:88
Auxiliary class for simple date.
Definition: Jeep/JDate.hh:39
JDate(const int year, const int month, const int day)
Constructor.
Definition: Jeep/JDate.hh:65
int year
year
Definition: Jeep/JDate.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: JManip.hh:328
JDate(const std::string &date)
Constructor.
Definition: Jeep/JDate.hh:79
int month
month
Definition: Jeep/JDate.hh:155
bool less(const JDate &date) const
Less-than method.
Definition: Jeep/JDate.hh:97
char * loc(char *orig)
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
int day
day
Definition: Jeep/JDate.hh:156