7 #include <samchon/WeakString.hpp> 8 #include <samchon/library/StringUtil.hpp> 9 #include <samchon/library/IOperator.hpp> 39 :
public std::chrono::system_clock::time_point
42 typedef std::chrono::system_clock::time_point super;
50 static auto calc_seconds(
int year,
int month,
int date) ->
long long 56 total = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400);
59 for (
int i = 0; i < month - 1; i++)
64 total = (total * 24 * 60 * 60);
77 static std::array<int, 12> months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81 if (!(year % 100 == 0 && year % 400 != 0))
96 Date() : super(
std::chrono::system_clock::now())
106 Date(
const super &date) : super(date)
116 Date(super &&date) : super(move(date))
128 Date(
int year,
int month,
int date,
int hour = 0,
int min = 0,
int sec = 0)
130 set(year, month, date, hour, min, sec);
167 int year, month, date, hour = 0, min = 0, sec = 0;
170 size_t delimiter = wstr.
find(
" ");
171 if (delimiter == std::string::npos)
176 date_wstr = wstr.
substr(0, delimiter);
177 time_wstr = wstr.
substr(delimiter);
179 std::vector<WeakString> &hms = time_wstr.
between(
" ").split(
":");
180 int hour = std::stoi(hms[0]);
181 int min = std::stoi(hms[1]);
185 sec = std::stoi(hms[2]);
190 std::vector<WeakString> &ymd = date_wstr.
split(
"-");
191 year = std::stoi(ymd[0]);
192 month = std::stoi(ymd[1]);
193 date = std::stoi(ymd[2]);
195 set(year, month, date, hour, min, sec);
206 void set(
int year,
int month,
int date,
int hour = 0,
int min = 0,
int sec = 0)
211 throw std::invalid_argument(
"month is over 12");
212 if (date > monthArray[month - 1])
213 throw std::invalid_argument(
"date is over expiration date");
217 linuxTime += hour * 60 * 60 + min * 60 + sec;
227 void set(
long long linuxTime)
229 *
this = std::chrono::system_clock::from_time_t(linuxTime);
249 return tm.tm_year + 1900;
260 return tm.tm_mon + 1;
271 return tm.tm_mon + 1;
292 struct tm &tm =
toTM();
303 struct tm &tm =
toTM();
314 struct tm &tm =
toTM();
341 month == 2 && new_date == 29
345 !((year % 100 == 0 && year % 400 != 0))
350 set(year, month, new_date);
368 throw std::invalid_argument(
"month is over 12");
378 if (new_date > months[month - 1])
379 new_date = months[month - 1];
381 set(year, month, new_date);
461 year = year + (month - 1) / 12;
470 if (date > months[month - 1])
471 date = months[month - 1];
473 set(year, month, date);
517 operator+=(std::chrono::hours(val));
533 operator+=(std::chrono::minutes(val));
550 operator+=(std::chrono::seconds(val));
561 auto toTM() const -> struct ::tm
564 time_t tt = std::chrono::system_clock::to_time_t(*
this);
565 localtime_s(&tm, &tt);
577 std::chrono::system_clock::duration duration = *
this - std::chrono::system_clock::from_time_t(0);
578 std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
580 return seconds.count();
590 struct tm &tm =
toTM();
592 if (tm.tm_hour == 0 && tm.tm_min == 0 && tm.tm_sec == 0)
596 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday
601 "{1}-{2}-{3} {4}:{5}:{6}",
602 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
603 tm.tm_hour, tm.tm_min, tm.tm_sec
auto getYear() const -> int
Get year of the Date.
auto getHour() const -> int
Get hour.
Date(long long linuxTime)
Construct from linux_time.
Date()
Default Constructor.
void setHour(int val)
Set hour.
Date(const super &date)
Copy Constructor.
Date(const std::string &str)
Construct from string.
void setYear(int year)
Set year of the Date.
void setSecond(int val)
Set hour.
static auto calc_seconds(int year, int month, int date) -> long long
Calculates how many seconds have flowen since 0000-01-01 00:00:00.
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...
void addMonth(int val)
Add months to the Date.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
auto toString() const -> std::string
Converts the Date to std::string.
void addHour(int val)
Add hours.
auto getSecond() const -> int
Get second.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
void addDate(int val)
Add days to the Date.
auto getMonth() const -> int
Get month of the Date.
auto getDate() const -> int
Get the day in month of the Date.
void setMonth(int month)
Set month of the Date.
static auto calc_last_dates(int year) -> std::array< int, 12 >
Calculates and gets an array of final date of each month for that year.
auto substr(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
auto toTM() const -> struct::tm
Converts the Date to struct tm.
auto getMinute() const -> int
Get minute.
void addSecond(int val)
Add seconds.
Date (year, month, day in month)
auto getDay() const -> int
Get the day in week of the Date.
void setMinute(int val)
Set minute.
Date(int year, int month, int date, int hour=0, int min=0, int sec=0)
Construct from date elements.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
void addWeek(int val)
Add weeks to the Date.
void addYear(int val)
Add years to the Date.
auto toLinuxTime() const -> long long
Converts the Date to linux_time.
A string class only references characeters, reference only.
void addMinute(int val)
Add minutes.