1 | #include "calendar.hpp" |
---|
2 | #include "duration.hpp" |
---|
3 | #include "date.hpp" |
---|
4 | #include "calendar_util.hpp" |
---|
5 | |
---|
6 | namespace xmlioserver |
---|
7 | { |
---|
8 | namespace date |
---|
9 | { |
---|
10 | /// ////////////////////// Définitions ////////////////////// /// |
---|
11 | CCalendar::CCalendar(void) |
---|
12 | : CObject() |
---|
13 | , initDate(*this) |
---|
14 | , currentDate(initDate) |
---|
15 | { /* Ne rien faire de plus */ } |
---|
16 | |
---|
17 | CCalendar::CCalendar(const StdString & id, |
---|
18 | int yr, int mth, int d , |
---|
19 | int hr, int min, int sec) |
---|
20 | : CObject(id) |
---|
21 | , initDate(*this, yr, mth, d, hr, min, sec) |
---|
22 | , currentDate(initDate) |
---|
23 | { /* Ne rien faire de plus */ } |
---|
24 | |
---|
25 | CCalendar::CCalendar(const StdString & id, const StdString & dateStr) |
---|
26 | : CObject(id) |
---|
27 | , initDate(CDate::FromString(dateStr, *this)) |
---|
28 | , currentDate(initDate) |
---|
29 | { /* Ne rien faire de plus */ } |
---|
30 | |
---|
31 | CCalendar::~CCalendar(void) |
---|
32 | { /* Ne rien faire de plus */ } |
---|
33 | |
---|
34 | ///--------------------------------------------------------------- |
---|
35 | |
---|
36 | StdString CCalendar::toString(void) const |
---|
37 | { |
---|
38 | StdOStringStream oss; |
---|
39 | oss << "[type: " << this->getId() |
---|
40 | << ", start: " << this->initDate |
---|
41 | << ", current: " << this->currentDate << "]"; |
---|
42 | return (oss.str()); |
---|
43 | } |
---|
44 | |
---|
45 | void CCalendar::fromString(const StdString & str) |
---|
46 | { ERROR("CCalendar::fromString(str)", |
---|
47 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
48 | |
---|
49 | //----------------------------------------------------------------- |
---|
50 | |
---|
51 | void CCalendar::setTimeStep(const CDuration & duration) |
---|
52 | { this->timestep = duration; } |
---|
53 | |
---|
54 | CDate & CCalendar::update(int step) |
---|
55 | { |
---|
56 | info(20) << "update step : " << step <<" timestep "<<this->timestep << std::endl; |
---|
57 | return (this->getCurrentDate() = this->getInitDate() + step * this->timestep); |
---|
58 | } |
---|
59 | |
---|
60 | //----------------------------------------------------------------- |
---|
61 | |
---|
62 | const CDuration & CCalendar::getTimeStep(void) const { return (this->timestep); } |
---|
63 | const CDate & CCalendar::getInitDate(void) const { return (this->initDate); } |
---|
64 | CDate & CCalendar::getCurrentDate(void) { return (this->currentDate); } |
---|
65 | |
---|
66 | //----------------------------------------------------------------- |
---|
67 | |
---|
68 | int CCalendar::getMonthLength(const CDate & date) const |
---|
69 | { // Retourne la durée du mois en jour. |
---|
70 | static const int NoLeapMonthLength[] = |
---|
71 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
---|
72 | return (NoLeapMonthLength[date.getMonth()-1]); |
---|
73 | } |
---|
74 | |
---|
75 | StdString CCalendar::getType(void) const { return (StdString(this->getId())); } |
---|
76 | |
---|
77 | int CCalendar::getYearTotalLength(const CDate & date) const { return (365 * 86400); } |
---|
78 | |
---|
79 | int CCalendar::getYearLength (void) const { return (12); } |
---|
80 | int CCalendar::getDayLength (void) const { return (24); } |
---|
81 | int CCalendar::getHourLength (void) const { return (60); } |
---|
82 | int CCalendar::getMinuteLength(void) const { return (60); } |
---|
83 | |
---|
84 | int CCalendar::getNbSecond(const CDate & date) const |
---|
85 | { // Retourne le nombre de secondes écoulées depuis le début de l'année. |
---|
86 | CDate _d0(date); int nbday = 0; |
---|
87 | |
---|
88 | for(_d0.setMonth(1); _d0.getMonth() < date.getMonth(); _d0.setMonth(_d0.getMonth()+1)) |
---|
89 | nbday += getMonthLength(_d0); |
---|
90 | return ((((nbday + date.getDay()) * getDayLength() + date.getHour()) * getHourLength() |
---|
91 | + date.getMinute()) * getMinuteLength() + date.getSecond()); |
---|
92 | } |
---|
93 | |
---|
94 | StdString CCalendar::getMonthName(int month_id) const |
---|
95 | { |
---|
96 | static const StdString Monthname_str[] = |
---|
97 | { "january", "february", "march" , "april" , "may" , "june" , |
---|
98 | "july" , "august" , "september", "october", "november", "december" }; |
---|
99 | return(Monthname_str[month_id-1]); |
---|
100 | } |
---|
101 | |
---|
102 | const StdString CCalendar::getMonthShortName(int month_id) const |
---|
103 | { StdString value = this->getMonthName(month_id); value.resize(3); return (value); } |
---|
104 | |
---|
105 | ///---------------------------------------------------------------- |
---|
106 | |
---|
107 | } // namespace date |
---|
108 | } // namespace xmlioserver |
---|