-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcDate_t.cpp
193 lines (141 loc) · 3.31 KB
/
cDate_t.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
#include "cDate_t.h"
#include <time.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
/*****************
* Static Members
*****************/
int cDate_t::format = 1;
//Days in month i - 1 in non-Leap year
const int cDate_t::daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
const string cDate_t::dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const string cDate_t::months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
/*****************
* CTORs and DTOR
****************/
cDate_t::cDate_t()
{
time_t time_data;
time( &time_data);
time_info = localtime(&time_data);
setDate_p(time_info->tm_mday, (time_info->tm_mon+1), (time_info->tm_year + 1900));
}
cDate_t::cDate_t( int day, int month, int year )
{
setDate_p(day,month,year);
}
cDate_t::cDate_t( const cDate_t& d)
{
setDate_p(d.getDay(),d.getMonth(),d.getYear());
}
cDate_t::~cDate_t()
{
//TODO: do we need to release time_info?
}
/*************
* Operators
*************/
const cDate_t& cDate_t::operator=( const cDate_t& d)
{
if (this != &d){
day = d.getDay();
month = d.getMonth();
year = d.getYear();
}
return *this;
}
/****************
* Public Methods
****************/
void cDate_t::setDate( int day, int month, int year)
{
setDate_p(day,month,year);
}
void cDate_t::printDate() const
{
printDate(format);
}
void cDate_t::printDate (int format) const {
switch (format){
case 1:
cout << setfill('0');
cout << setw(2) << getDay() << '/'
<< getNameOfMonth().substr(0, 3) << '/'
<< getYear();
break;
case 2: //European
cout << setfill('0');
cout << setw(2) << getDay() << '/'
<< setw(2) << getMonth() << '/'
<< getYear() << " European";
break;
case 3: //American
cout << setfill('0');
cout << setw(2) << getMonth() << '/'
<< setw(2) << getDay() << '/'
<< getYear() << " American";
break;
default:
cout << "Unknown format";
}
}
int cDate_t::getDayOfTheYear()
{
setTime_info();
return (time_info->tm_yday) + 1;
}
string cDate_t::getNameOfDay()
{
setTime_info();
return dayNames[time_info->tm_wday];
}
/******************
* Private Methods
******************/
void cDate_t::setDate_p( int d, int m, int y )
{
if (d > 0 && m > 0 && y > 0){
//set the year
year = y;
//set the month and accumulate the overflow to year
month = ((m % 12) == 0) ? 12 : (m % 12);
year += int((m-1)/12);
//set the day and accumulate the overflow to the month & year
while(d > daysInMonth[month-1]){
//Take care of leap years
if (month == 2 && isYearLeap() && d == 29){
break;
}
//Subtract number of days in current month from d
d -= daysInMonth[month-1];
//If Feburary and Year is leap subtract one more day (total 29)
if (month == 2 && isYearLeap()){
d--;
}
//Advance month
month++;
//Advance Year if needed
if(month > 12){
month = 1;
year++;
}
}
//Finnaly - set the current day
day = d;
//case invalid date set to 1/1/1970 by default
}else{
day = 1;
month = 1;
year = 1970;
}
}
void cDate_t::setTime_info()
{
time_info->tm_mday = day;
time_info->tm_mon = month - 1;
time_info->tm_year = year - 1900;
mktime(time_info);
}