-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateFormat.cpp
228 lines (182 loc) · 5.62 KB
/
DateFormat.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Date.h"
int noOfHyphens ( string st )
{
int num = 0;
int size = strlen( st.c_str() );
for(int i=0; i<size; i++){
if(st[i] == '-')
num++;
}
return num;
}
/*****************************************************/
void checkFormat_NULL( char* df, char* mf, char* yf)
throw ( invalid_argument, domain_error, out_of_range )
{
//cout<<df<<" "<<mf<<" "<<yf<<" "<<endl;
if(strcmp(df,"0")!=0 && strcmp(df,"d")!=0 && strcmp(df,"dd")!=0){
//cout<<"Date Format Assigned - NULL"<<endl;
delete[] df;
df= NULL;
throw invalid_argument("Incorrect format for date");
}
if(strcmp(mf,"0")!=0 && strcmp(mf,"mm")!=0 && strcmp(mf,"m")!=0 && strcmp(mf,"mmm")!=0){
//cout<<"Month Format Assigned - NULL"<<endl;
delete[] mf;
mf= NULL;
throw invalid_argument("Incorrect format for month");
}
if(strcmp(yf,"0")!=0 && strcmp(yf,"yy")!=0 && strcmp(yf,"yyyy")!=0){
//cout<<"Year Format Assigned - NULL"<<endl;
delete[] yf;
yf= NULL;
throw invalid_argument("Incorrect format for year");
}
//cout<<df<<" "<<mf<<" "<<yf<<" "<<endl;
}
/*****************************************************/
void parser ( string form, char* &df, char* &mf, char* &yf )
throw (invalid_argument)
{
int first, second; //index of first and second hyphens
int size = strlen(form.c_str());
first = -1;
for(int i=0; i<size; i++) {
if(form[i] == '-' && first==-1){
first = i;
}
else if(form[i] == '-'){
second = i;
}
}
//cout<<"String received: "<<form<<endl;
int size1 = first;
if(size1 == 0) {
throw invalid_argument("No Date Format Found");
}
strcpy(df, const_cast<char*> ((form.substr( 0, size1 )).c_str()));
//cout<<"Day format assigned: "<<df<<endl;
int size2 = second - first - 1 ;
if(size1 == 0) {
throw invalid_argument("No Month Format Found");
}
strcpy(mf ,const_cast<char*> ((form.substr( first + 1, size2 )).c_str()));
//cout<<"Month Format assigned: "<<mf<<endl;
int size3 = (size-1) - second;
if(size1 == 0) {
throw invalid_argument("No Year Format Found");
}
strcpy(yf , const_cast<char*> ((form.substr( second + 1, size3 )).c_str()));
//cout<<"Year format assigned: "<<yf<<endl;
//cout<<"Exiting parser..."<<endl;
}
/*****************************************************/
/*****************************************************/
/*****************************************************/
//constructors
DateFormat::DateFormat(const char* dForm, const char* mForm, const char* yForm) //constructor taking in all formats
throw ( invalid_argument, domain_error, out_of_range )
{
dateFormat = new char[5];
monthFormat = new char[5];
yearFormat = new char[5];
//cout<<"here"<<endl;
if(mForm == NULL){
//cout<<"Abhi"<<endl;
string s("0");
char* temp = const_cast<char *> (s.c_str());
strcpy ( monthFormat , temp);
//cout<<"Ab"<<endl;
}
else{
strcpy( monthFormat , const_cast<char *> (mForm));
//cout<<monthFormat<<endl;
}
//cout<<"here"<<endl;
if(dForm == NULL){
//cout<<"Abhi"<<endl;
string s("0");
char* temp = const_cast<char *> (s.c_str());
strcpy ( dateFormat , temp);
//cout<<"Ab"<<endl;
}
else{
strcpy( dateFormat , const_cast<char *> (dForm));
//cout<<dateFormat<<endl;
}
//cout<<"here"<<endl;
if(yForm == NULL){
//cout<<"Abhi"<<endl;
string s("0");
char* temp = const_cast<char *> (s.c_str());
strcpy ( yearFormat , temp);
//cout<<"Ab"<<endl;
}
else{
strcpy( yearFormat , const_cast<char *> (yForm));
//cout<<yearFormat<<endl;
}
checkFormat_NULL ( dateFormat, monthFormat, yearFormat );
//cout<<dateFormat<<" "<<monthFormat<<" "<<yearFormat<<endl;
}
/*****************************************************/
DateFormat::DateFormat(const char* format) // constructor taking the parse string as input
throw ( invalid_argument, domain_error, out_of_range )
{
char* formatString = const_cast<char *> (format);
string str(formatString);
int n = noOfHyphens (str);
if( n != 2 ){
//cout<<"Format Assigned - NULL-NULL-NULL"<<endl;
dateFormat = NULL;
monthFormat = NULL;
yearFormat = NULL;
throw invalid_argument("No of separators is more than required");
}
dateFormat = new char[5];
monthFormat = new char[5];
yearFormat = new char[5];
parser( str, dateFormat, monthFormat, yearFormat ); // str has 2 hyphens
//cout<<"Day format assigned: " << dateFormat << endl;
//cout<<"Month format assigned: "<< monthFormat<< endl;
//cout<<"Year format assigned: " << yearFormat << endl;
checkFormat_NULL ( dateFormat, monthFormat, yearFormat );
//cout<<"Assigned Date Format: "<< dateFormat<<"-"<<monthFormat<<"-"<<yearFormat<<endl;
}
/*****************************************************/
DateFormat::DateFormat()
{
dateFormat = new char[2];
strcpy(dateFormat, "dd");
monthFormat = new char[3];
strcpy(monthFormat, "mmm");
yearFormat = new char[4];
strcpy(yearFormat, "yy");
//cout<<"Default Constructor called. Format Assigned - dd-mmm-yy"<<endl;
}
/*****************************************************/
/*****************************************************/
//Destructor
DateFormat::~DateFormat() {
}
/*****************************************************/
//getter and setters
char* DateFormat::getDF(){
return dateFormat;
}
char* DateFormat::getMF(){
return monthFormat;
}
char* DateFormat::getYF(){
return yearFormat;
}
void DateFormat::setDF( char* st ){
dateFormat = st;
}
void DateFormat::setMF( char* st ){
monthFormat = st;
}
void DateFormat::setYF( char* st ){
yearFormat = st;
}
/******************************************************/