{"id":770,"date":"2020-02-23T15:14:14","date_gmt":"2020-02-23T23:14:14","guid":{"rendered":"http:\/\/209.126.2.187\/?p=770"},"modified":"2020-05-16T10:56:55","modified_gmt":"2020-05-16T17:56:55","slug":"date","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/02\/23\/date\/","title":{"rendered":"Date"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this post, I will introduce some problems related to date.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Details<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Is Leap Year<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">bool is_leap(int y) {\n    if (y % 400 == 0) {\n        return true;\n    } else if (y % 100 == 0) {\n        return false;\n    } else {\n        return y % 4 == 0;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Days of the Year<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">vector&lt;int> days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\nint day_of_year(string date) {\n    \/\/ date: \"1900-12-21\"\n    int year = stoi(date.substr(0, 4));\n    int month = stoi(date.substr(5, 2));\n    int day = stoi(date.substr(8, 2));\n    int result = 0;\n    for (int m = 1; m &lt; month; ++m) {\n        result += days[m];\n        if (m == 2 &amp;&amp; is_leap(year)) {\n            result++;\n        }\n    }\n    result += day;\n    return result;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Days from 1900<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int day_since_1900(string date) {\n    \/\/ date: \"1900-12-21\"\n    int year = stoi(date.substr(0, 4));\n    int result = 0;\n    for (int y = 1900; y &lt; year; ++y) {\n        result += is_leap(y) ? 366 : 365;\n    }\n    result += day_of_year(date);\n    return result;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Days Between Dates<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int daysBetweenDates(string date1, string date2) {\n    return abs(day_since_1900(date1) - day_since_1900(date2));\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some problems related to date.&nbsp; Details Is Leap Year Days of the Year Days from 1900 Days Between Dates<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,49],"tags":[],"class_list":["post-770","post","type-post","status-publish","format-standard","hentry","category-alg","category-math"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/770\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}