Samchon Framework for CPP  1.0.0
Datetime.cpp
1 #include <samchon/library/Datetime.hpp>
2 
3 #include <chrono>
4 #include <samchon/WeakString.hpp>
5 #include <samchon/library/StringUtil.hpp>
6 
7 using namespace std;
8 using namespace samchon;
9 using namespace samchon::library;
10 
11 /* ----------------------------------------------------------
12  CONSTRUCTORS
13 ---------------------------------------------------------- */
14 //CONSTRUCTORS
15 Datetime::Datetime()
16  : super()
17 {
18 }
19 Datetime::Datetime(const chrono::system_clock::time_point &date)
20  : super(date)
21 {
22 }
23 Datetime::Datetime(chrono::system_clock::time_point &&date)
24  : super(move(date))
25 {
26 }
27 Datetime::Datetime(int year, int month, int date, int hour, int min, int sec)
28  : Datetime()
29 {
30  set(year, month, date, hour, min, sec);
31 }
32 Datetime::Datetime(const string &str)
33  : Datetime(WeakString(str))
34 {
35 }
37  : Datetime()
38 {
39  set(wStr);
40 }
41 Datetime::Datetime(long long linuxTime)
42  : Datetime()
43 {
44  set(linuxTime);
45 }
46 
47 //SEMI-CONSTRUCTORS
48 void Datetime::set(const string &str)
49 {
50  set(WeakString(str));
51 }
52 void Datetime::set(const WeakString &wStr)
53 {
54  super::set(wStr);
55  if (wStr.find(" ") == std::string::npos)
56  return;
57 
58  vector<WeakString> &vec = wStr.between(" ").split(":");
59  int hour = stoi(vec[0].str());
60  int min = stoi(vec[1].str());
61  int sec;
62 
63  if (vec.size() == 3)
64  sec = stoi(vec[2].str());
65  else
66  sec = 0;
67 
68  set(getYear(), getMonth(), getDate(), hour, min, sec);
69 }
70 void Datetime::set(int year, int month, int date, int hour, int minutes, int seconds)
71 {
72  super::set(year, month, date);
73  addSecond( hour*60*60 + minutes*60 + seconds );
74 }
75 void Datetime::set(long long linuxTime)
76 {
77  super::set(linuxTime);
78 }
79 
80 /* ----------------------------------------------------------
81  GET-SETTERS
82 ---------------------------------------------------------- */
83 //SETTERS
84 void Datetime::setYear(int val)
85 {
86  int hour = getHour();
87  int minutes = getMinute();
88  int sec = getSecond();
89 
90  super::setYear(val);
91  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
92 }
93 void Datetime::setMonth(int val)
94 {
95  int hour = getHour();
96  int minutes = getMinute();
97  int sec = getSecond();
98 
99  super::setMonth(val);
100  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
101 }
102 void Datetime::setDate(int val)
103 {
104  int hour = getHour();
105  int minutes = getMinute();
106  int sec = getSecond();
107 
108  super::setDate(val);
109  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
110 }
111 void Datetime::setHour(int val)
112 {
113  set(getYear(), getMonth(), getDate(), val, getMinute(), getSecond());
114 }
115 void Datetime::setMinute(int val)
116 {
117  set(getYear(), getMonth(), getDate(), getHour(), val, getSecond());
118 }
119 void Datetime::setSecond(int val)
120 {
121  set(getYear(), getMonth(), getDate(), getHour(), getMinute(), val);
122 }
123 
124 //ADDERS
125 void Datetime::addYear(int val)
126 {
127  int hour = getHour();
128  int minutes = getMinute();
129  int sec = getSecond();
130 
131  super::addYear(val);
132  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
133 }
134 void Datetime::addMonth(int val)
135 {
136  int hour = getHour();
137  int minutes = getMinute();
138  int sec = getSecond();
139 
140  super::addMonth(val);
141  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
142 }
143 void Datetime::addWeek(int val)
144 {
145  int hour = getHour();
146  int minutes = getMinute();
147  int sec = getSecond();
148 
149  super::addWeek(val);
150  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
151 }
152 void Datetime::addDate(int val)
153 {
154  int hour = getHour();
155  int minutes = getMinute();
156  int sec = getSecond();
157 
158  super::addDate(val);
159  set(getYear(), getMonth(), getDate(), hour, minutes, sec);
160 }
161 void Datetime::addHour(int val)
162 {
163  operator+=(chrono::hours(val));
164 }
165 void Datetime::addMinute(int val)
166 {
167  operator+=(chrono::minutes(val));
168 }
169 void Datetime::addSecond(int val)
170 {
171  operator+=(chrono::seconds(val));
172 }
173 
174 //GETTERS
175 int Datetime::getHour() const
176 {
177  struct tm &tm = toTM();
178  return tm.tm_hour;
179 }
181 {
182  struct tm &tm = toTM();
183  return tm.tm_min;
184 }
186 {
187  struct tm &tm = toTM();
188  return tm.tm_sec;
189 }
190 
191 //TO_STRING
192 auto Datetime::toString() const -> std::string
193 {
194  return super::toString() +
196  (
197  " {1}:{2}:{3}",
198  getHour(), getMinute(), getSecond()
199  );
200 }
auto getYear() const -> int
Get year of the Date.
Definition: Date.cpp:221
virtual void setDate(int)
Definition: Date.cpp:168
virtual void setDate(int)
Definition: Datetime.cpp:102
virtual void addDate(int) override
Add days to the Date.
Definition: Datetime.cpp:152
void setHour(int)
Set hour.
Definition: Datetime.cpp:111
void addMinute(int)
Add minutes.
Definition: Datetime.cpp:165
auto toTM() const -> struct::tm
Converts the Date to struct tm.
Definition: Date.cpp:242
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
auto getHour() const -> int
Get hour.
Definition: Datetime.cpp:175
virtual auto toString() const -> std::string override
Converts the Datetime to std::string.
Definition: Datetime.cpp:192
virtual void addDate(int)
Add days to the Date.
Definition: Date.cpp:206
void setSecond(int)
Set hour.
Definition: Datetime.cpp:119
auto getSecond() const -> int
Get second.
Definition: Datetime.cpp:185
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
Definition: WeakString.cpp:91
virtual void setYear(int)
Set year of the Date.
Definition: Date.cpp:131
auto getDate() const -> int
Get the day in month of the Date.
Definition: Date.cpp:236
virtual void addYear(int)
Add years to the Date.
Definition: Date.cpp:174
virtual void addWeek(int) override
Add weeks to the Date.
Definition: Datetime.cpp:143
virtual void addYear(int) override
Add years to the Date.
Definition: Datetime.cpp:125
auto getMinute() const -> int
Get minute.
Definition: Datetime.cpp:180
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:55
void addHour(int)
Add hours.
Definition: Datetime.cpp:161
void addSecond(int)
Add seconds.
Definition: Datetime.cpp:169
virtual void setMonth(int)
Set month of the Date.
Definition: Datetime.cpp:93
void set(const std::string &)
Setter by string.
virtual void setMonth(int)
Set month of the Date.
Definition: Date.cpp:150
virtual void addMonth(int)
Add months to the Date.
Definition: Date.cpp:179
void set(const std::string &)
Setter by string.
auto getMonth() const -> int
Get month of the Date.
Definition: Date.cpp:226
virtual auto toString() const -> std::string
Converts the Date to std::string.
Definition: Date.cpp:250
virtual void addMonth(int) override
Add months to the Date.
Definition: Datetime.cpp:134
virtual void setYear(int)
Set year of the Date.
Definition: Datetime.cpp:84
virtual void addWeek(int)
Add weeks to the Date.
Definition: Date.cpp:202
void setMinute(int)
Set minute.
Definition: Datetime.cpp:115
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
Definition: WeakString.cpp:194
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
A string class only references characeters, reference only.
Definition: WeakString.hpp:32