XIOS  1.0
Xml I/O Server
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Définitions de type Énumérations Valeurs énumérées Amis Macros
date.cpp
Aller à la documentation de ce fichier.
1 #include "date.hpp"
2 #include "calendar.hpp"
3 #include "calendar_type.hpp"
4 #include "calendar_util.hpp"
5 #include <boost/date_time/gregorian/gregorian.hpp>
6 #include <boost/date_time/posix_time/posix_time.hpp>
7 
8 using namespace boost::posix_time;
9 using namespace boost::gregorian;
10 
11 namespace xios
12 {
14  CDate::CDate(void)
15  : relCalendar(NULL)
16  , year(0), month(1), day(1)
17  , hour(0), minute(0), second(0)
18  {}
19 
20  CDate::CDate(const CCalendar& calendar)
21  : relCalendar(&calendar)
22  , year(0), month(1), day(1)
23  , hour(0), minute(0), second(0)
24  {}
25 
26  CDate::CDate(const CCalendar& calendar,
27  int yr, int mth, int d,
28  int hr, int min, int sec)
29  : relCalendar(&calendar)
30  , year(yr), month(mth), day(d)
31  , hour(hr), minute(min), second(sec)
32  {
33  if (!this->checkDate())
34  {
35  DEBUG(<< "La date initialisée a été modifiée "
36  << "car elle était incorrecte par rapport au calendrier souhaité.");
37  }
38  }
39 
40  CDate::CDate(const CDate& date)
41  : relCalendar(date.relCalendar)
42  , year(date.year), month(date.month), day(date.day)
43  , hour(date.hour), minute(date.minute), second(date.second)
44  {
45  // Delay the verification until we get a calendar we can compare the date to
46  if (relCalendar && !checkDate())
47  {
48  DEBUG(<< "La date initialisée a été modifiée "
49  << "car elle était incorrecte par rapport au calendrier souhaité.");
50  }
51  }
52 
54  { /* Ne rien faire de plus */ }
55 
57 
59  {
60  relCalendar = date.relCalendar;
61  year = date.year; month = date.month; day = date.day;
62  hour = date.hour; minute = date.minute; second = date.second;
63  return (*this);
64  }
65 
66  bool CDate::operator==(const CDate& date)
67  {
68  return (&(*relCalendar) == &(*date.relCalendar) &&
69  year == date.year && month == date.month && day == date.day &&
70  hour == date.hour && minute == date.minute && second == date.second);
71 
72  }
73 
74  StdOStream& operator<<(StdOStream& out, const CDate& date)
75  {
76  std::streamsize s;
77  char c;
78 
79  int width=4;
80  double maxSize=10000;
81  while (date.year>=maxSize)
82  {
83  maxSize*=10;
84  width++;
85  }
86  s = out.width(width); c = out.fill('0'); out << date.year << '-';
87 
88  s = out.width(2); c = out.fill('0'); out << date.month << '-';
89  s = out.width(2); c = out.fill('0'); out << date.day << ' ';
90  s = out.width(2); c = out.fill('0'); out << date.hour << ':';
91  s = out.width(2); c = out.fill('0'); out << date.minute << ':';
92  s = out.width(2); c = out.fill('0'); out << date.second;
93 
94  return out;
95  }
96 
98  {
99  if (date.relCalendar)
100  date.relCalendar->parseDate(in, date);
101  else
102  CCalendar::parseDateDefault(in, date);
103 
104  return in;
105  }
106 
107  CDate::operator Time(void) const // Non vérifiée, pas optimisée ...
108  {
109  // This will check that a calendar was correctly associated to the date
110  const CCalendar& c = getRelCalendar();
111 
112  // Todo : Tester si la date courante est supérieure à la date initiale.
113  Time t = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear();
114 
115  if (c.hasLeapYear())
116  {
117  for (CDate d(c.getTimeOrigin()); d.getYear() < getYear(); d.setYear(d.getYear() + 1))
118  t += c.getYearTotalLength(d);
119  }
120  else
121  t += Time(getYear() - c.getTimeOrigin().getYear()) * c.getYearTotalLength(*this);
122 
123  return t;
124  }
125 
126  //----------------------------------------------------------------
127 
128  bool CDate::checkDate(void)
129  {
130  // This will also check that a calendar was correctly associated to the date
131  return getRelCalendar().checkDate(*this);
132  }
133 
134  //----------------------------------------------------------------
135 
136  int CDate::getYear (void) const { return (this->year ); }
137  int CDate::getMonth (void) const { return (this->month ); }
138  int CDate::getDay (void) const { return (this->day ); }
139  int CDate::getHour (void) const { return (this->hour ); }
140  int CDate::getMinute(void) const { return (this->minute); }
141  int CDate::getSecond(void) const { return (this->second); }
142 
143  //----------------------------------------------------------------
144 
145  const CCalendar& CDate::getRelCalendar(void) const
146  {
147  if (!this->relCalendar)
148  ERROR("const CCalendar& CDate::getRelCalendar(void) const",
149  "Invalid state: The date is not associated with any calendar.");
150  return (*this->relCalendar);
151  }
152 
153  bool CDate::hasRelCalendar(void) const
154  { return (this->relCalendar != NULL); }
155 
156  //----------------------------------------------------------------
157 
163  {
164  CDate yearStart(*this);
165  const CCalendar& c = getRelCalendar();
166  int nbDay = 0;
167 
168  for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1))
169  nbDay += c.getMonthLength(yearStart);
170 
171  // We need to use getDayLengthInSeconds instead of getDayLength since we might
172  // have a non-integral number of hours per day for user defined calendars
173  return ((nbDay + getDay() - 1) * c.getDayLengthInSeconds()
174  + (getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
175  }
176 
181  double CDate::getDayOfYear() const
182  {
184  }
185 
191  {
192  return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this);
193  }
194 
200  {
201  const CCalendar& c = getRelCalendar();
202  return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
203  }
204 
209  double CDate::getFractionOfDay() const
210  {
211  return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds();
212  }
213 
214  //----------------------------------------------------------------
215 
216  void CDate::setYear (int newyear) { this->year = newyear; }
217  void CDate::setMonth (int newmonth) { this->month = newmonth; }
218  void CDate::setDay (int newday) { this->day = newday; }
219  void CDate::setHour (int newhour) { this->hour = newhour; }
220  void CDate::setMinute(int newminute) { this->minute = newminute; }
221  void CDate::setSecond(int newsecond) { this->second = newsecond; }
222 
223  void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec)
224  {
225  this->year = yr;
226  this->month = mth;
227  this->day = d;
228  this->hour = hr;
229  this->minute = min;
230  this->second = sec;
231  }
232 
233  //----------------------------------------------------------------
234 
235  void CDate::addMonth(int value)
236  {// Value doit être égale à 1 ou -1.
237 
238  const CCalendar& c = getRelCalendar();
239  int nbMonthsPerYear = c.getYearLength();
240 
241  this->month += value;
242 
243  if (this->month == nbMonthsPerYear + 1) { year++; this->month = 1; }
244  if (this->month == 0) { year--; this->month = nbMonthsPerYear; }
245  }
246 
247  //----------------------------------------------------------------
248 
249  bool CDate::setRelCalendar(const CCalendar& relCalendar)
250  {
251  this->relCalendar = &relCalendar;
252  return this->checkDate();
253  }
254 
255  //----------------------------------------------------------------
256 
257  CDate CDate::FromString(const StdString& str, const CCalendar& calendar)
258  {
259  CDate dt(calendar);
260  StdIStringStream iss(str);
261  iss >> dt;
262  return dt;
263  }
264 
265  //----------------------------------------------------------------
266 
268  {
269  std::streamsize s;
270  char c;
271 
272  ostringstream oss;
273 
274  s = oss.width(4); c = oss.fill('0'); oss << year;
275  s = oss.width(2); c = oss.fill('0'); oss << month;
276  s = oss.width(2); c = oss.fill('0'); oss << day;
277 
278  return oss.str();
279  }
280 
281  string CDate::getStr(const string& str) const
282  {
283  ostringstream oss;
284  int level;
285 
286  level=0;
287  for(string::const_iterator it=str.begin();it!=str.end();++it)
288  {
289  if (level==0)
290  {
291  if (*it=='%') level++;
292  else oss<<*it;
293  }
294  else if (level==1)
295  {
296  switch (*it)
297  {
298  case 'y' :
299  oss.width(4); oss.fill('0'); oss << year;
300  level=0;
301  break;
302  case 'm' : // month or minute
303  level++;
304  break;
305  case 'd' :
306  oss.width(2); oss.fill('0'); oss << day;
307  level=0;
308  break;
309  case 'h' :
310  oss.width(2); oss.fill('0'); oss << hour;
311  level=0;
312  break;
313  case 's' :
314  oss.width(2); oss.fill('0'); oss << second;
315  level=0;
316  break;
317  case 'S' : // seconds since time origin
318  oss.width(0); oss << Time(*this);
319  level=0;
320  break;
321  case 'D' : // days since time origin
322  oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds();
323  level=0;
324  break;
325  default :
326  oss<<'%'<<*it;
327  level=0;
328  }
329  }
330  else if (level==2)
331  {
332  switch (*it)
333  {
334  case 'o' : // month
335  oss.width(2); oss.fill('0'); oss << month;
336  level=0;
337  break;
338  case 'i' : //minute
339  oss.width(2); oss.fill('0'); oss << minute;
340  level=0;
341  break;
342  default :
343  oss<<"%m"<<*it;
344  level=0;
345  }
346  }
347  }
348  return oss.str();
349  }
350 
352  {
353  StdOStringStream oss;
354  oss << *this;
355  return oss.str();
356  }
357 
359 
360 } // namespace xios
virtual int getHourLength(void) const
Definition: calendar.cpp:174
static void parseDateDefault(StdIStream &in, CDate &date)
Parse a date using a generic parser.
Definition: calendar.cpp:232
int getSecondOfDay() const
Get the fraction of the current day as a real number between 0 and 1.
Definition: date.cpp:199
int getSecond(void) const
Get the calendar associated to the date.
Definition: date.cpp:141
std::istringstream StdIStringStream
Definition: xios_spl.hpp:42
double getFractionOfYear() const
Get the number of seconds since the beginning of the day.
Definition: date.cpp:190
void setDate(int yr, int mth, int d, int hr=0, int min=0, int sec=0)
Definition: date.cpp:223
int year
Definition: date.hpp:97
virtual int getYearLength(void) const
Definition: calendar.cpp:172
const CCalendar & getRelCalendar(void) const
Definition: date.cpp:145
int getMonth(void) const
Definition: date.cpp:137
bool operator==(const CDate &date)
Definition: date.cpp:66
bool hasRelCalendar(void) const
Get the number of seconds since the beginning of the year.
Definition: date.cpp:153
void setMinute(int newminute)
Definition: date.cpp:220
virtual int getMonthLength(const CDate &date) const
Definition: calendar.cpp:161
static CDate FromString(const StdString &str, const CCalendar &calendar)
Definition: date.cpp:257
std::string StdString
Definition: xios_spl.hpp:48
#define xios(arg)
CDate & operator=(const CDate &date)
Opérateurs ///.
Definition: date.cpp:58
long long int Time
////////////////////// Déclarations ////////////////////// ///
Definition: duration.hpp:11
int day
Definition: date.hpp:97
std::istream StdIStream
Definition: xios_spl.hpp:47
int hour
Definition: date.hpp:97
int month
Definition: date.hpp:97
virtual int getMinuteLength(void) const
Definition: calendar.cpp:175
CBufferOut & operator<<(CBufferOut &buffer, const CArray< T_numtype, N_rank > &array)
Definition: array_new.hpp:657
int minute
Definition: date.hpp:97
virtual int getYearTotalLength(const CDate &date) const
Definition: calendar.cpp:170
int getSecondOfYear() const
Get the number of days (expressed as a real number) since the beginning of the year.
Definition: date.cpp:162
CATCH CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar *scalarDestination, CScalar *scalarSource, CReduceScalarToScalar *algo ERROR)("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",<< "Operation must be defined."<< "Scalar source "<< scalarSource->getId()<< std::endl<< "Scalar destination "<< scalarDestination->getId())
int getDay(void) const
Definition: date.cpp:138
string getStr(const string &str) const
Definition: date.cpp:281
int getMinute(void) const
Definition: date.cpp:140
const CDate & getTimeOrigin(void) const
Definition: calendar.cpp:156
virtual bool checkDate(CDate &date) const
Test if a date is valid with regard to the current calendar.
Definition: calendar.cpp:292
bool setRelCalendar(const CCalendar &relCalendar)
Definition: date.cpp:249
CBufferIn & operator>>(CBufferIn &buffer, CArray< T_numtype, N_rank > &array)
Definition: array_new.hpp:664
const CCalendar * relCalendar
Propriétés privées ///.
Definition: date.hpp:96
void addMonth(int value)
Set the calendar associated to the date.
Definition: date.cpp:235
virtual void parseDate(StdIStream &in, CDate &date) const
Parse a date using the calendar&#39;s parser.
Definition: calendar.cpp:286
CDate(void)
Create an empty date associated to the specified calendar.
Definition: date.cpp:14
virtual int getDayLengthInSeconds(void) const
Returns the day length expressed in seconds.
Definition: calendar.cpp:176
virtual bool hasLeapYear() const
Test if the calendar can have leap year.
Definition: calendar.cpp:178
StdString getStryyyymmdd(void) const
Definition: date.cpp:267
int second
Definition: date.hpp:97
void setHour(int newhour)
Definition: date.cpp:219
int getHour(void) const
Definition: date.cpp:139
void setMonth(int newmonth)
Definition: date.cpp:217
int getYear(void) const
Divers accesseurs ///.
Definition: date.cpp:136
void setYear(int newyear)
Mutateurs ///.
Definition: date.cpp:216
double getDayOfYear() const
Get the fraction of the current year as a real number between 0 and 1.
Definition: date.cpp:181
bool checkDate(void)
Traitements ///.
Definition: date.cpp:128
StdString toString(void) const
Autres ///.
Definition: date.cpp:351
double getFractionOfDay() const
Get the fraction of the current day as a real number between 0 and 1.
Definition: date.cpp:209
#define DEBUG(x)
Definition: exception.hpp:70
void setSecond(int newsecond)
Definition: date.cpp:221
void setDay(int newday)
Definition: date.cpp:218
std::ostream StdOStream
Definition: xios_spl.hpp:46
std::ostringstream StdOStringStream
Définition de types (issus de la bibliothèque standard)///.
Definition: xios_spl.hpp:41
~CDate(void)
Destructeur ///.
Definition: date.cpp:53