/
Date.cpp
184 lines (169 loc) · 4.35 KB
/
Date.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Date.cpp
// Created by Kyle A Peeler on 2/26/16.
#include "Date.h"
#include <cstdlib>
#include <sstream>
using namespace std;
/*
* Name: Date()
* Purpose:
* Default constructor for Date()
* Parameters:
* None
* Return Value:
* Date - Returns default instance of date object (all values set to -1)
*/
Date::Date(){
this->month = -1;
this->day = -1;
this->year = -1;
}//end constructor
/*
* Name: ~Date()
* Purpose:
* Deconstructor for Date() - Automatically called when Date is destroyed
* Parameters:
* None
* Return Value:
* None
*/
Date::~Date(){
}//end Deconstructor
/*
* Name: Date(string dateString)
* Purpose:
* Overloaded constructor for Date class
* Parameters:
* dateString: string - a string in the format of "mm/dd/yyyy"
* Return Value:
* Date - Returns initialized instance of date object with values set to
* specified date
*/
Date::Date(string dateString) {
//convert the date string to a stream
istringstream inputDateString(dateString);
//date string format is in mm/dd/yyyy
//create variables to store month
string month;
// ... and day
string day;
// ... and year
string year;
//parse the date string like we parse other stuff
getline(inputDateString, month, '/');
getline(inputDateString, day, '/');
getline(inputDateString, year);
//convert the substrings to integer values and store them
this->month = atoi(month.c_str());
this->day = atoi(day.c_str());
this->year = atoi(year.c_str());
}//end Overloaded constructor
/*
* Name: Date(int mm, int dd, int yyyy)
* Purpose:
* Overloaded constructor for Date class w/ integer values for day, month, yearStr
* Parameters:
* mm: int - month value
* dd: int - day value
* yyyy: int - year value
* Return Value:
* Date - Returns initialized instance of date object with values set to sepecified integers
*/
Date::Date(int mm, int dd, int yyyy){
this->month = mm;
this->day = dd;
this->year = yyyy;
}//end Overloaded constructor
/*
* Name: setFromString(string dateString)
* Purpose:
* Sets the date object to the month, day, and year specified in the date string
* Parameters:
* dateString: string - the string to obtain the month, day, and year
* Return Value:
* None
*/
void Date::setFromString(string dateString){
//convert the date string to a stream
istringstream inputDateString(dateString);
//date string format is in mm/dd/yyyy
//create variables to store month
string month;
// ... and day
string day;
// ... and year
string year;
//parse the date string like we parse other stuff
getline(inputDateString, month, '/');
getline(inputDateString, day, '/');
getline(inputDateString, year);
//convert the substrings to integer values and store them
this->month = atoi(month.c_str());
this->day = atoi(day.c_str());
this->year = atoi(year.c_str());
}//end setFromString
/*
* Name: getMonth()
* Purpose:
* returns the month value from the current date instance
* Parameters:
* None
* Return Value:
* int - the integer representation of the month, -1 if not set
*/
int Date::getMonth() {
return this->month;
}//end getMonth
/*
* Name: getDay()
* Purpose:
* returns the day value from the current date instance
* Parameters:
* None
* Return Value:
* int - the integer representation of the day, -1 if not set
*/
int Date::getDay() {
return this->day;
}//end getDay
/*
* Name: getYear()
* Purpose:
* returns the year value from the current date instance
* Parameters:
* None
* Return Value:
* int - the integer representation of the year, -1 if not set
*/
int Date::getYear() {
return this->year;
}//end getYear
/*
* Name: intToString(int num)
* Purpose:
* Helper function to convert an integer to stringstream
* Parameters:
* num: int - the integer to convert to string
* Return Value:
* String - the string representation of the passed in integer
*/
string Date::intToString(int num){
stringstream ss;
ss << num;
return ss.str();
}//end intToString
/*
* Name: toString()
* Purpose:
* Returns the string representation of the date in the format 'mm/dd/yyyy'
* Parameters:
* None
* Return Value:
* None
*/
string Date::toString() {
string monthStr = intToString(this->getMonth());
string dayStr = intToString(this->getDay());
string yearStr = intToString(this->getYear());
return monthStr + "/" + dayStr + "/" + yearStr;
}//end toString