Samchon Framework for CPP  1.0.0
Date.cpp
1 #include <samchon/library/Date.hpp>
2 
3 #include <array>
4 
5 #include <samchon/WeakString.hpp>
6 #include <samchon/library/StringUtil.hpp>
7 
8 using namespace std;
9 using namespace samchon;
10 using namespace samchon::library;
11 
12 chrono::system_clock::time_point Date::TP_1970 = chrono::system_clock::from_time_t(0);
13 
14 /* --------------------------------------------------------------------------
15  CONSTRUCTORS
16 -------------------------------------------------------------------------- */
17 //CONSTRUCTORS
18 Date::Date()
19  : super(chrono::system_clock::now())
20  //: super(chrono::system_clock::now())
21 {
22 }
23 Date::Date(const super &date)
24  : super(date)
25 {
26 }
27 Date::Date(super &&date)
28  : super(move(date))
29 {
30 }
31 
32 Date::Date(int year, int month, int date)
33 {
34  set(year, month, date);
35 }
36 Date::Date(const string &str)
37  : Date(WeakString(str))
38 {
39 }
40 Date::Date(const WeakString &wStr)
41 {
42  set(wStr);
43 }
44 Date::Date(long long linuxTime)
45  : super(chrono::system_clock::from_time_t(linuxTime))
46 {
47 }
48 
49 //SEMI-CONSTRUCTORS
50 void Date::set(const string &str)
51 {
52  set(WeakString(str));
53 }
54 void Date::set(const WeakString &wStr)
55 {
56  WeakString val = wStr.trim();
57 
58  int year, month, date;
59 
60  //½ÃºÐÃʱîÁö ÀÖÀ» ¶§
61  if (val.find(" ") != std::string::npos)
62  val = val.between(std::string(), " ");
63 
64  //³â¿ùÀÏ ¼³Á¤
65  vector<WeakString> &ymdVec = val.split("-");
66  year = stoi(ymdVec[0].str());
67  month = stoi(ymdVec[1].str());
68  date = stoi(ymdVec[2].str());
69 
70  set(year, month, date);
71 }
72 void Date::set(int year, int month, int date) //³¯Â¥, ½Ã°£ ¼³Á¤
73 {
74  array<int, 12> &monthArray = calcLastDates(year);
75 
76  if (month > 12)
77  throw invalid_argument("month is over 12");
78  if (date > monthArray[month - 1])
79  throw invalid_argument("date is over expiration date");
80 
81  long long linuxTime = calcSeconds(year, month, date) - (calcSeconds(1970, 1, 1) + 9 * 60 * 60);
82  set(linuxTime);
83 };
84 void Date::set(long long linuxTime)
85 {
86  *this = chrono::system_clock::from_time_t(linuxTime);
87 
88  /*long long prevTime = toLinuxTime();
89 
90  this->operator+=(chrono::seconds(linuxTime));
91  this->operator-=(chrono::seconds(prevTime));*/
92 }
93 
94 /* --------------------------------------------------------------------------
95  HIDDEN METHODS
96 -------------------------------------------------------------------------- */
97 auto Date::calcLastDates(int year) -> array<int, 12>
98 {
99  //Last dates of each month
100  static array<int, 12> monthArray = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
101 
102  //When the year contains leaf month, then make expiration date of Feburary to 29
103  if (year % 4 == 0)
104  if (!(year % 100 == 0 && year % 400 != 0))
105  monthArray[1] = 29;
106 
107  return monthArray;
108 }
109 long long Date::calcSeconds(int year, int month, int date)
110 {
111  array<int, 12> &monthArray = calcLastDates(year);
112  long long total;
113 
114  //¿¬, ¿ù, ÀÏÀÇ ÃÑ Àϼö total = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400); //´Þ¼ö¸¸Å­ ³¯ÀÚ¸¦ ´õÇÔ for (int i = 0; i < month - 1; i++) total += monthArray[i]; total += date; //ÀÏÀÚ¿¡ 3,600À» °öÇÑ´Ù total = (total * 24 * 60 * 60); return total; } /* -------------------------------------------------------------------------- SETTERS OF EACH COMPONENT -------------------------------------------------------------------------- */ //SET METHODS void Date::setYear(int year) { int month = getMonth(); int newDate = getDate(); //À±´Þ¿¡ 29ÀÏÀε¥ addYearÀ» ÇÏ·Á´Â °æ¿ì if ( month == 2 && newDate == 29 //ÇöÀç°¡ À±´Þ 29ÀÏ && !( year == 4 && !((year % 100 == 0 && year % 400 != 0)) ) //¹Ù²Ü ¿¬µµÀÇ 2¿ùÀº À±´ÞÀÌ ¾Æ´Ô ) newDate = 28; set(year, month, newDate); } void Date::setMonth(int month) { if (month > 12) throw invalid_argument("month is over 12"); //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
115  total = (year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400);
116 
117  //´Þ¼ö¸¸Å­ ³¯ÀÚ¸¦ ´õÇÔ for (int i = 0; i < month - 1; i++) total += monthArray[i]; total += date; //ÀÏÀÚ¿¡ 3,600À» °öÇÑ´Ù total = (total * 24 * 60 * 60); return total; } /* -------------------------------------------------------------------------- SETTERS OF EACH COMPONENT -------------------------------------------------------------------------- */ //SET METHODS void Date::setYear(int year) { int month = getMonth(); int newDate = getDate(); //À±´Þ¿¡ 29ÀÏÀε¥ addYearÀ» ÇÏ·Á´Â °æ¿ì if ( month == 2 && newDate == 29 //ÇöÀç°¡ À±´Þ 29ÀÏ && !( year == 4 && !((year % 100 == 0 && year % 400 != 0)) ) //¹Ù²Ü ¿¬µµÀÇ 2¿ùÀº À±´ÞÀÌ ¾Æ´Ô ) newDate = 28; set(year, month, newDate); } void Date::setMonth(int month) { if (month > 12) throw invalid_argument("month is over 12"); //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
118  for (int i = 0; i < month - 1; i++)
119  total += monthArray[i];
120  total += date;
121 
122  //ÀÏÀÚ¿¡ 3,600À» °öÇÑ´Ù total = (total * 24 * 60 * 60); return total; } /* -------------------------------------------------------------------------- SETTERS OF EACH COMPONENT -------------------------------------------------------------------------- */ //SET METHODS void Date::setYear(int year) { int month = getMonth(); int newDate = getDate(); //À±´Þ¿¡ 29ÀÏÀε¥ addYearÀ» ÇÏ·Á´Â °æ¿ì if ( month == 2 && newDate == 29 //ÇöÀç°¡ À±´Þ 29ÀÏ && !( year == 4 && !((year % 100 == 0 && year % 400 != 0)) ) //¹Ù²Ü ¿¬µµÀÇ 2¿ùÀº À±´ÞÀÌ ¾Æ´Ô ) newDate = 28; set(year, month, newDate); } void Date::setMonth(int month) { if (month > 12) throw invalid_argument("month is over 12"); //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
123  total = (total * 24 * 60 * 60);
124  return total;
125 }
126 
127 /* --------------------------------------------------------------------------
128  SETTERS OF EACH COMPONENT
129 -------------------------------------------------------------------------- */
130 //SET METHODS
131 void Date::setYear(int year)
132 {
133  int month = getMonth();
134  int newDate = getDate();
135 
136  //À±´Þ¿¡ 29ÀÏÀε¥ addYearÀ» ÇÏ·Á´Â °æ¿ì if ( month == 2 && newDate == 29 //ÇöÀç°¡ À±´Þ 29ÀÏ && !( year == 4 && !((year % 100 == 0 && year % 400 != 0)) ) //¹Ù²Ü ¿¬µµÀÇ 2¿ùÀº À±´ÞÀÌ ¾Æ´Ô ) newDate = 28; set(year, month, newDate); } void Date::setMonth(int month) { if (month > 12) throw invalid_argument("month is over 12"); //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
137  if
138  (
139  month == 2 && newDate == 29 //ÇöÀç°¡ À±´Þ 29ÀÏ
140  &&
141  !(
142  year == 4 &&
143  !((year % 100 == 0 && year % 400 != 0))
144  ) //¹Ù²Ü ¿¬µµÀÇ 2¿ùÀº À±´ÞÀÌ ¾Æ´Ô ) newDate = 28; set(year, month, newDate); } void Date::setMonth(int month) { if (month > 12) throw invalid_argument("month is over 12"); //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
145  )
146  newDate = 28;
147 
148  set(year, month, newDate);
149 }
150 void Date::setMonth(int month)
151 {
152  if (month > 12)
153  throw invalid_argument("month is over 12");
154 
155  //´Þ·Â int year = getYear(); int newDate = getDate(); array<int, 12> &monthArray = calcLastDates(year); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
156  int year = getYear();
157  int newDate = getDate();
158 
159  array<int, 12> &monthArray = calcLastDates(year);
160 
161  //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[month - 1]) newDate = monthArray[month - 1]; set(year, month, newDate); } void Date::setDate(int val) { set(getYear(), getMonth(), val); } //ADD METHODS void Date::addYear(int val) { //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ setYear(getYear() + val); } void Date::addMonth(int val) { //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
162  //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤
163  if (newDate > monthArray[month - 1])
164  newDate = monthArray[month - 1];
165 
166  set(year, month, newDate);
167 }
168 void Date::setDate(int val)
169 {
170  set(getYear(), getMonth(), val);
171 }
172 
173 //ADD METHODS
174 void Date::addYear(int val)
175 {
176  //ÇöÀç°¡ 2012³â 2¿ù 29ÀÏ(À±´Þ)Àε¥ 1³âÀ» Ãß°¡Çϸé 2013³â 2¿ù 28ÀÏ·Î º¯ÇÔ
177  setYear(getYear() + val);
178 }
179 void Date::addMonth(int val)
180 {
181  //ÇöÀç°¡ 3¿ù 31ÀÏÀε¥ 1¿ùÀÌ Ãß°¡µÇ¸é ³¯Â¥´Â 4¿ù 30ÀÏ·Î º¯ÇÑ´Ù int newYear = getYear(); int newMonth = getMonth() + val; int newDate = getDate(); if (newMonth > 12) { newYear = newYear + (newMonth - 1) / 12; newMonth = newMonth % 12; } //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
182  int newYear = getYear();
183  int newMonth = getMonth() + val;
184  int newDate = getDate();
185 
186  if (newMonth > 12)
187  {
188  newYear = newYear + (newMonth - 1) / 12;
189  newMonth = newMonth % 12;
190  }
191 
192  //´Þ·Â std::array<int, 12> &monthArray = calcLastDates(newYear); //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
193  std::array<int, 12> &monthArray = calcLastDates(newYear);
194 
195  //ÇØ´ç ¿ùÀÇ ¸¶Áö¸· ÀÏÀ» ÃʰúÇÒ ¶§, Á¶Á¤ÇÑ´Ù //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤ if (newDate > monthArray[newMonth - 1]) newDate = monthArray[newMonth - 1]; set(newYear, newMonth, newDate); } void Date::addWeek(int val) { addDate(7 * val); } void Date::addDate(int val) { operator+=(chrono::hours(24 * val)); } /* -------------------------------------------------------------------------- GETTERS AND TO_STRING -------------------------------------------------------------------------- */ auto Date::toLinuxTime() const -> long long { std::chrono::system_clock::duration duration = *this - TP_1970; std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration); return seconds.count(); } int Date::getYear() const { auto &tm = toTM(); return tm.tm_year + 1900; } int Date::getMonth() const { auto &tm = toTM(); return tm.tm_mon + 1; } int Date::getDay() const { auto &tm = toTM(); return tm.tm_wday; } int Date::getDate() const { auto &tm = toTM(); return tm.tm_mday; } auto Date::toTM() const -> struct tm { struct tm tm; time_t tt = chrono::system_clock::to_time_t(*this); localtime_s(&tm, &tt); return tm; } auto Date::toString() const -> std::string { return StringUtil::substitute ( "{1}-{2}-{3}", getYear(), getMonth(), getDate() ); }
196  //EX-> 4¿ùÀε¥ 31ÀÏÀ̸é 4¿ù 30ÀÏ·Î Á¶Á¤
197  if (newDate > monthArray[newMonth - 1])
198  newDate = monthArray[newMonth - 1];
199 
200  set(newYear, newMonth, newDate);
201 }
202 void Date::addWeek(int val)
203 {
204  addDate(7 * val);
205 }
206 void Date::addDate(int val)
207 {
208  operator+=(chrono::hours(24 * val));
209 }
210 
211 /* --------------------------------------------------------------------------
212  GETTERS AND TO_STRING
213 -------------------------------------------------------------------------- */
214 auto Date::toLinuxTime() const -> long long
215 {
216  std::chrono::system_clock::duration duration = *this - TP_1970;
217  std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
218 
219  return seconds.count();
220 }
221 int Date::getYear() const
222 {
223  auto &tm = toTM();
224  return tm.tm_year + 1900;
225 }
226 int Date::getMonth() const
227 {
228  auto &tm = toTM();
229  return tm.tm_mon + 1;
230 }
231 int Date::getDay() const
232 {
233  auto &tm = toTM();
234  return tm.tm_wday;
235 }
236 int Date::getDate() const
237 {
238  auto &tm = toTM();
239  return tm.tm_mday;
240 }
241 
242 auto Date::toTM() const -> struct tm
243 {
244  struct tm tm;
245  time_t tt = chrono::system_clock::to_time_t(*this);
246  localtime_s(&tm, &tt);
247 
248  return tm;
249 }
250 auto Date::toString() const -> std::string
251 {
253  (
254  "{1}-{2}-{3}",
255  getYear(), getMonth(), getDate()
256  );
257 }
static std::chrono::system_clock::time_point TP_1970
time_point for 1970-01-01 09:00:00
Definition: Date.hpp:48
auto getYear() const -> int
Get year of the Date.
Definition: Date.cpp:221
virtual void setDate(int)
Definition: Date.cpp:168
auto toLinuxTime() const -> long long
Converts the Date to linux_time.
Definition: Date.cpp:214
auto toTM() const -> struct::tm
Converts the Date to struct tm.
Definition: Date.cpp:242
Definition: RWMutex.hpp:4
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
Definition: WeakString.cpp:212
static auto calcSeconds(int year, int month, int date) -> long long
Calculates how many seconds have flowen since 0000-01-01 00:00:00.
Definition: Date.cpp:109
Package of libraries.
Definition: library.hpp:84
virtual void addDate(int)
Add days to the Date.
Definition: Date.cpp:206
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
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
Date()
Default Constructor.
Definition: Date.cpp:18
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
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
Definition: WeakString.cpp:374
void set(const std::string &)
Setter by string.
auto getMonth() const -> int
Get month of the Date.
Definition: Date.cpp:226
static auto calcLastDates(int year) -> std::array< int, 12 >
Calculates and gets an array of final date of each month for that year.
Definition: Date.cpp:97
virtual auto toString() const -> std::string
Converts the Date to std::string.
Definition: Date.cpp:250
virtual void addWeek(int)
Add weeks to the Date.
Definition: Date.cpp:202
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
Definition: WeakString.cpp:194
auto getDay() const -> int
Get the day in week of the Date.
Definition: Date.cpp:231
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
A string class only references characeters, reference only.
Definition: WeakString.hpp:32