1 #include <samchon/library/Date.hpp> 5 #include <samchon/WeakString.hpp> 6 #include <samchon/library/StringUtil.hpp> 12 chrono::system_clock::time_point Date::TP_1970 = chrono::system_clock::from_time_t(0);
19 : super(chrono::system_clock::now())
34 set(year, month, date);
45 : super(chrono::system_clock::from_time_t(linuxTime))
58 int year, month, date;
61 if (val.
find(
" ") != std::string::npos)
62 val = val.
between(std::string(),
" ");
65 vector<WeakString> &ymdVec = val.
split(
"-");
66 year = stoi(ymdVec[0].str());
67 month = stoi(ymdVec[1].str());
68 date = stoi(ymdVec[2].str());
70 set(year, month, date);
77 throw invalid_argument(
"month is over 12");
78 if (date > monthArray[month - 1])
79 throw invalid_argument(
"date is over expiration date");
86 *
this = chrono::system_clock::from_time_t(linuxTime);
100 static array<int, 12> monthArray = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
104 if (!(year % 100 == 0 && year % 400 != 0))
115 total = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400);
118 for (
int i = 0; i < month - 1; i++)
119 total += monthArray[i];
123 total = (total * 24 * 60 * 60);
139 month == 2 && newDate == 29
143 !((year % 100 == 0 && year % 400 != 0))
148 set(year, month, newDate);
153 throw invalid_argument(
"month is over 12");
163 if (newDate > monthArray[month - 1])
164 newDate = monthArray[month - 1];
166 set(year, month, newDate);
188 newYear = newYear + (newMonth - 1) / 12;
189 newMonth = newMonth % 12;
197 if (newDate > monthArray[newMonth - 1])
198 newDate = monthArray[newMonth - 1];
200 set(newYear, newMonth, newDate);
208 operator+=(chrono::hours(24 * val));
216 std::chrono::system_clock::duration duration = *
this -
TP_1970;
217 std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
219 return seconds.count();
224 return tm.tm_year + 1900;
229 return tm.tm_mon + 1;
245 time_t tt = chrono::system_clock::to_time_t(*
this);
246 localtime_s(&tm, &tt);
static std::chrono::system_clock::time_point TP_1970
time_point for 1970-01-01 09:00:00
auto getYear() const -> int
Get year of the Date.
virtual void setDate(int)
auto toLinuxTime() const -> long long
Converts the Date to linux_time.
auto toTM() const -> struct::tm
Converts the Date to struct tm.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
static auto calcSeconds(int year, int month, int date) -> long long
Calculates how many seconds have flowen since 0000-01-01 00:00:00.
virtual void addDate(int)
Add days to the Date.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
virtual void setYear(int)
Set year of the Date.
auto getDate() const -> int
Get the day in month of the Date.
virtual void addYear(int)
Add years to the Date.
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...
Date()
Default Constructor.
virtual void setMonth(int)
Set month of the Date.
virtual void addMonth(int)
Add months to the Date.
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
void set(const std::string &)
Setter by string.
auto getMonth() const -> int
Get month of the Date.
static auto calcLastDates(int year) -> std::array< int, 12 >
Calculates and gets an array of final date of each month for that year.
virtual auto toString() const -> std::string
Converts the Date to std::string.
virtual void addWeek(int)
Add weeks to the Date.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
auto getDay() const -> int
Get the day in week of the Date.
Top level namespace of products built from samchon.
A string class only references characeters, reference only.