bpp-popgen3  3.0.0
Date.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Text/TextTools.h>
6 
7 // From Local
8 #include "Date.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 // ** Class constructor: *******************************************************/
14 
15 Date::Date(const int day, const int month, const int year) : day_(day),
16  month_(month),
17  year_(year)
18 {
19  if (day < 1 || day > 31)
20  throw (BadIntegerException("Date::Date: day must be in [1;31].", day));
21  if (month < 1 || month > 12)
22  throw (BadIntegerException("Date::Date: month must be in [1;12].", month));
23 }
24 
25 Date::Date(const Date& date) : day_(date.getDay()),
26  month_(date.getMonth()),
27  year_(date.getYear()) {}
28 
29 // ** Class destructor: ********************************************************/
30 
32 
33 // ** Other methodes: **********************************************************/
34 
35 Date& Date::operator=(const Date& date)
36 {
37  day_ = date.getDay();
38  month_ = date.getMonth();
39  year_ = date.getYear();
40  return *this;
41 }
42 
43 void Date::setDate(const int day, const int month, const int year)
44 {
45  if (day >= 1 && day <= 31)
46  day_ = day;
47  else
48  throw (BadIntegerException("Date::Date: day must be in [1;31].", day));
49  if (month >= 1 && month <= 12)
50  month_ = month;
51  else
52  throw (BadIntegerException("Date::Date: month must be in [1;12].", month));
53  year_ = year;
54 }
55 
56 void Date::setYear(const int year)
57 {
58  year_ = year;
59 }
60 
61 void Date::setMonth(const int month)
62 {
63  if (month >= 1 && month <= 12)
64  month_ = month;
65  else
66  throw (BadIntegerException("Date::Date: month must be in [1;12].", month));
67 }
68 
69 void Date::setDay(const int day)
70 {
71  if (day >= 1 && day <= 31)
72  day_ = day;
73  else
74  throw (BadIntegerException("Date::Date: day must be in [1;31].", day));
75 }
76 
77 std::string Date::getDateStr() const
78 {
79  string date, uDay = "", uMonth = "";
80  if (day_ < 10)
81  uDay = "0";
82  if (month_ < 10)
83  uMonth = "0";
85  return date;
86 }
87 
88 bool Date::operator==(const Date& date) const
89 {
90  if (day_ == date.getDay() && month_ == date.getMonth() && year_ == date.getYear())
91  return true;
92  else
93  return false;
94 }
95 
96 bool Date::operator<(const Date& date) const
97 {
98  if (year_ < date.getYear() || (month_ < date.getMonth() && year_ == date.getYear()) || (day_ < date.getDay() && month_ == date.getMonth() && year_ == date.getYear()))
99  return true;
100  else
101  return false;
102 }
The Date class.
Definition: Date.h:21
std::string getDateStr() const
Get the Date as a string.
Definition: Date.cpp:77
int getMonth() const
Get the month as an int.
Definition: Date.h:105
void setYear(const int year)
Set the year.
Definition: Date.cpp:56
int getYear() const
Get the Year as an int.
Definition: Date.h:100
bool operator==(const Date &date) const
The == operator.
Definition: Date.cpp:88
void setDay(const int day)
Set the day.
Definition: Date.cpp:69
int year_
Definition: Date.h:25
Date(const int day=1, const int month=1, const int year=2000)
Build a new Date from three values.
Definition: Date.cpp:15
void setMonth(const int month)
Set the month.
Definition: Date.cpp:61
bool operator<(const Date &date) const
The < operator.
Definition: Date.cpp:96
Date & operator=(const Date &date)
The Date copy operator.
Definition: Date.cpp:35
~Date()
Destroy the Date object.
Definition: Date.cpp:31
void setDate(const int day, const int month, const int year)
Set the Date.
Definition: Date.cpp:43
int day_
Definition: Date.h:23
int getDay() const
Get the day as an int.
Definition: Date.h:110
int month_
Definition: Date.h:24
std::string toString(T t)