-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDate.cpp
100 lines (85 loc) · 1.72 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Date.cpp
* Author: Bahati Boniface
*
* Created on April 25, 2017, 4:28 PM
*/
#include "Date.h"
#include <ctime> //Time dependency
#include <string>
#include <sstream>
Date::Date() {
time_t t = time(0); //time(0); or time(NULL); returns the system
//time in seconds
struct tm *now = localtime(&t);
year = now->tm_year + 1900; //http://soen.kr/lecture/ccpp/cpp1/8-1-1.htm
month = now->tm_mon + 1;
day = now->tm_mday;
}
void Date::setDate(int d, int m, int y) {
day = d;
month = m;
year = y;
}
int Date::getDay() {
return day;
}
int Date::getMonth() {
return month;
}
int Date::getYear() {
return year;
}
const std::string Date::DayNames[7] = {
"Sun",
"Mon",
"Tue",
"Wen",
"Thu",
"Fri",
"Sat"
};
const std::string Date::MonthNames[12] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
const int Date::MonthDays[12] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
};
void Date::IncreaseMonth() {
if (month < 12)
++month;
else {
month = 1;
++year;
}
}
void Date::DecreaseMonth() {
if (month > 1)
--month;
else {
month = 12;
--year;
}
}
string Date::ToString() const {
stringstream ss;
ss << year << "-" << month << "-" << day;
return ss.str();
}