Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. #include <ctime>
  2.  
  3. // Adjust date by a number of days +/-
  4. void DatePlusDays( struct tm* date, int days )
  5. {
  6. const time_t ONE_DAY = 24 * 60 * 60 ;
  7.  
  8. // Seconds since start of epoch
  9. time_t date_seconds = mktime( date ) + (days * ONE_DAY) ;
  10.  
  11. // Update caller's date
  12. // Use localtime because mktime converts to UTC so may change date
  13. *date = *localtime( &date_seconds ) ; ;
  14. }
  15.  
  16. #include <iostream>
  17.  
  18. int main()
  19. {
  20. struct tm date = { 0, 0, 12 } ; // nominal time midday (arbitrary).
  21. int year = 2010 ;
  22. int month = 2 ; // February
  23. int day = 26 ; // 26th
  24.  
  25. // Set up the date structure
  26. date.tm_year = year - 1900 ;
  27. date.tm_mon = month - 1 ; // note: zero indexed
  28. date.tm_mday = day ; // note: not zero indexed
  29.  
  30. // Date, less 100 days
  31. DatePlusDays( &date, -100 ) ;
  32.  
  33. // Show time/date using default formatting
  34. std::cout << asctime( &date ) << std::endl ;
  35. }
  36.  
  37. m = 1;
  38. while(d > numberOfDaysInMonth(m, y)) {
  39. d -= numberOfDaysInMonth(m, y);
  40. m++;
  41. }
  42. return date with year = y, month = m, day = d;
  43.  
  44. while(d >= 365) {
  45. d -= 365;
  46. if(isLeapYear(y)) {
  47. d -= 1;
  48. }
  49. y++;
  50. }
  51. // now use the case where d is less than 365
  52.  
  53. tm* dateTimeAdd(const tm* const dateTime, const int& days, const int& hours, const int& mins, const int& secs) {
  54. tm* newTime = new tm;
  55. memcpy(newTime, dateTime, sizeof(tm));
  56.  
  57. newTime->tm_mday += days;
  58. newTime->tm_hour += hours;
  59. newTime->tm_min += mins;
  60. newTime->tm_sec += secs;
  61.  
  62. time_t nt_seconds = mktime(newTime) - timezone;
  63. delete newTime;
  64.  
  65. return gmtime(&nt_seconds);
  66. }
  67.  
  68. time_t t = time(NULL);
  69. tm* utc = gmtime(&t);
  70. tm* newUtc = dateTimeAdd(utc, -5, 0, 0, 0); //subtract 5 days
  71.  
  72. #include <iostream>
  73. #include <string>
  74.  
  75. using namespace std;
  76.  
  77. class Date {
  78. public:
  79. Date(size_t year, size_t month, size_t day):m_year(year), m_month(month), m_day(day) {}
  80. ~Date() {}
  81.  
  82. // Add specified number of days to date
  83. Date operator + (size_t days) const;
  84.  
  85. // Subtract specified number of days from date
  86. Date operator - (size_t days) const;
  87.  
  88. size_t Year() { return m_year; }
  89. size_t Month() { return m_month; }
  90. size_t Day() { return m_day; }
  91.  
  92. string DateStr();
  93. private:
  94. // Leap year check
  95. inline bool LeapYear(int year) const
  96. { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
  97.  
  98. // Holds all max days in a general year
  99. static const int MaxDayInMonth[13];
  100.  
  101. // Private members
  102. size_t m_year;
  103. size_t m_month;
  104. size_t m_day;
  105. };
  106.  
  107. // Define MaxDayInMonth
  108. const int Date::MaxDayInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  109.  
  110. //===========================================================================================
  111. /// Add specified number of days to date
  112. Date Date::operator + (size_t days) const {
  113. // Maximum days in the month
  114. int nMaxDays(MaxDayInMonth[m_month] + (m_month == 2 && LeapYear(m_year) ? 1 : 0));
  115.  
  116. // Initialize the Year, Month, Days
  117. int nYear(m_year);
  118. int nMonth(m_month);
  119. int nDays(m_day + days);
  120.  
  121. // Iterate till it becomes a valid day of a month
  122. while (nDays > nMaxDays) {
  123. // Subtract the max number of days of current month
  124. nDays -= nMaxDays;
  125.  
  126. // Advance to next month
  127. ++nMonth;
  128.  
  129. // Falls on to next year?
  130. if (nMonth > 12) {
  131. nMonth = 1; // January
  132. ++nYear; // Next year
  133. }
  134.  
  135. // Update the max days of the new month
  136. nMaxDays = MaxDayInMonth[nMonth] + (nMonth == 2 && LeapYear(nYear) ? 1 : 0);
  137. }
  138.  
  139. // Construct date
  140. return Date(nYear, nMonth, nDays);
  141. }
  142.  
  143. //===========================================================================================
  144. /// Subtract specified number of days from date
  145. Date Date::operator - (size_t days) const {
  146. // Falls within the same month?
  147. if (0 < (m_day - days)) {
  148. return Date(m_year, m_month, m_day - days);
  149. }
  150.  
  151. // Start from this year
  152. int nYear(m_year);
  153.  
  154. // Start from specified days and go back to first day of this month
  155. int nDays(days);
  156. nDays -= m_day;
  157.  
  158. // Start from previous month and check if it falls on to previous year
  159. int nMonth(m_month - 1);
  160. if (nMonth < 1) {
  161. nMonth = 12; // December
  162. --nYear; // Previous year
  163. }
  164.  
  165. // Maximum days in the current month
  166. int nDaysInMonth = MaxDayInMonth[nMonth] + (nMonth == 2 && LeapYear(nYear) ? 1 : 0);
  167.  
  168. // Iterate till it becomes a valid day of a month
  169. while (nDays >= 0) {
  170. // Subtract the max number of days of current month
  171. nDays -= nDaysInMonth;
  172.  
  173. // Falls on to previous month?
  174. if (nDays > 0) {
  175. // Go to previous month
  176. --nMonth;
  177.  
  178. // Falls on to previous year?
  179. if (nMonth < 1) {
  180. nMonth = 12; // December
  181. --nYear; // Previous year
  182. }
  183. }
  184.  
  185. // Update the max days of the new month
  186. nDaysInMonth = MaxDayInMonth[nMonth] + (nMonth == 2 && LeapYear(nYear) ? 1 : 0);
  187. }
  188.  
  189. // Construct date
  190. return Date(nYear, nMonth, (0 < nDays ? nDays : -nDays));
  191. }
  192.  
  193. //===========================================================================================
  194. /// Get the date string in yyyy/mm/dd format
  195. string Date::DateStr() {
  196. return to_string(m_year)
  197. + string("/")
  198. + string(m_month < 10 ? string("0") + to_string(m_month) : to_string(m_month))
  199. + string("/")
  200. + string(m_day < 10 ? string("0") + to_string(m_day) : to_string(m_day));
  201. }
  202.  
  203.  
  204. int main() {
  205. // Add n days to a date
  206. cout << Date(2017, 6, 25).DateStr() << " + 10 days = "
  207. << (Date(2017, 6, 25) /* Given Date */ + 10 /* Days to add */).DateStr() << endl;
  208.  
  209. // Subtract n days from a date
  210. cout << Date(2017, 6, 25).DateStr() << " - 10 days = "
  211. << (Date(2017, 6, 25) /* Given Date */ - 10 /* Days to subract */).DateStr() << endl;
  212.  
  213. return 0;
  214. }
  215.  
  216. Output
  217. 2017/06/25 + 10 days = 2017/07/05
  218. 2017/06/25 - 10 days = 2017/06/15
  219.  
  220. // C++ program to find date after adding
  221. // given number of days.
  222. #include<bits/stdc++.h>
  223. using namespace std;
  224.  
  225. // Return if year is leap year or not.
  226. bool isLeap(int y)
  227. {
  228. if (y%100 != 0 && y%4 == 0 || y %400 == 0)
  229. return true;
  230.  
  231. return false;
  232. }
  233.  
  234. // Given a date, returns number of days elapsed
  235. // from the beginning of the current year (1st
  236. // jan).
  237. int offsetDays(int d, int m, int y)
  238. {
  239. int offset = d;
  240.  
  241. switch (m - 1)
  242. {
  243. case 11:
  244. offset += 30;
  245. case 10:
  246. offset += 31;
  247. case 9:
  248. offset += 30;
  249. case 8:
  250. offset += 31;
  251. case 7:
  252. offset += 31;
  253. case 6:
  254. offset += 30;
  255. case 5:
  256. offset += 31;
  257. case 4:
  258. offset += 30;
  259. case 3:
  260. offset += 31;
  261. case 2:
  262. offset += 28;
  263. case 1:
  264. offset += 31;
  265. }
  266.  
  267. if (isLeap(y) && m > 2)
  268. offset += 1;
  269.  
  270. return offset;
  271. }
  272.  
  273. // Given a year and days elapsed in it, finds
  274. // date by storing results in d and m.
  275. void revoffsetDays(int offset, int y, int *d, int *m)
  276. {
  277. int month[13] = { 0, 31, 28, 31, 30, 31, 30,
  278. 31, 31, 30, 31, 30, 31 };
  279.  
  280. if (isLeap(y))
  281. month[2] = 29;
  282.  
  283. int i;
  284. for (i = 1; i <= 12; i++)
  285. {
  286. if (offset <= month[i])
  287. break;
  288. offset = offset - month[i];
  289. }
  290.  
  291. *d = offset;
  292. *m = i;
  293. }
  294.  
  295. // Add x days to the given date.
  296. void addDays(int d1, int m1, int y1, int x)
  297. {
  298. int offset1 = offsetDays(d1, m1, y1);
  299. int remDays = isLeap(y1)?(366-offset1):(365-offset1);
  300.  
  301. // y2 is going to store result year and
  302. // offset2 is going to store offset days
  303. // in result year.
  304. int y2, offset2;
  305. if (x <= remDays)
  306. {
  307. y2 = y1;
  308. offset2 = offset1 + x;
  309. }
  310.  
  311. else
  312. {
  313. // x may store thousands of days.
  314. // We find correct year and offset
  315. // in the year.
  316. x -= remDays;
  317. y2 = y1 + 1;
  318. int y2days = isLeap(y2)?366:365;
  319. while (x >= y2days)
  320. {
  321. x -= y2days;
  322. y2++;
  323. y2days = isLeap(y2)?366:365;
  324. }
  325. offset2 = x;
  326. }
  327.  
  328. // Find values of day and month from
  329. // offset of result year.
  330. int m2, d2;
  331. revoffsetDays(offset2, y2, &d2, &m2);
  332.  
  333. cout << "d2 = " << d2 << ", m2 = " << m2
  334. << ", y2 = " << y2;
  335. }
  336.  
  337. // Driven Program
  338. int main()
  339. {
  340. int d = 14, m = 3, y = 2015;
  341. int x = 366;
  342.  
  343. addDays(d, m, y, x);
  344.  
  345. return 0;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement