{"id":4134,"date":"2018-10-25T20:54:37","date_gmt":"2018-10-25T12:54:37","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4134"},"modified":"2018-10-25T20:54:37","modified_gmt":"2018-10-25T12:54:37","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a05-time%e5%8c%85%e7%9a%84%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4134.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#5-time\u5305\u7684\u5b66\u4e60"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5728\u4e00\u4e9b\u65e5\u5fd7\u5206\u6790\u7a0b\u5e8f\u4e2d\uff0c\u65e5\u671f\/\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u89e3\u6790\u64cd\u4f5c\u8981\u7528\u5230\u7684\u5730\u65b9\u6709\u5f88\u591a\uff0c\u4e4b\u524d\u5728\u7528Python\u7684\u65f6\u5019\u5c31\u8fd9\u7c7b\u95ee\u9898\u6574\u7406\u4e86\u597d\u51e0\u7bc7\u6587\u7ae0\uff0c\u73b0\u5728\u5b66\u4e60Go\u8bed\u8a00\u4e86\uff0c\u4e5f\u6574\u7406\u4e00\u7bc7\u76f8\u5173\u6587\u7ae0\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff08\u5b57\u7b26\u4e32\u3001\u5e74\u3001\u6708\u3001\u65e5\uff09\uff1f<\/h6>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    start_time := time.Now()\r\n    y, m, d := start_time.Date()\r\n    today := start_time.Format(\"2006-01-02\")\r\n    datetime := start_time.Format(\"2006-01-02 15:04:05\") \/\/ \u540e\u9762\u7684\u53c2\u6570\u662f\u56fa\u5b9a\u7684\uff0c\u5426\u5219\u5c06\u65e0\u6cd5\u6b63\u5e38\u8f93\u51fa\r\n\r\n    fmt.Printf(\"time is: %v, (%T)\\n\", start_time, start_time) \/\/ time is: 2018-10-21 17:24:29.307336 +0800 CST, (time.Time)\r\n    fmt.Printf(\"y, m, d: %v, %v, %v\\n\", y, m, d)              \/\/ y, m, d: 2018, October, 21\r\n    fmt.Printf(\"y[%T], m[%T], d[%T]\\n\", y, m, d)              \/\/ y[int], m[time.Month], d[int]\r\n    fmt.Printf(\"today is: %q, (%T)\\n\", today, today)          \/\/ today is: \"2018-10-21\", (string)\r\n    fmt.Printf(\"datetime is: %q, (%T)\\n\", datetime, datetime) \/\/ datetime is: \"2018-10-21 17:24:29\", (string)\r\n}\r\n<\/pre>\n<h6>Go \u8bed\u8a00\u4e2d\u5982\u4f55\u89e3\u6790\u65e5\u671f\/\u65f6\u95f4\u5b57\u7b26\u4e32\uff1f\uff08\u4f7f\u7528time.Parse()\u65b9\u6cd5\uff09<\/h6>\n<pre class=\"lang:default decode:true\">datetime_str := \"2018-10-21 12:24:51\"\r\ndt, _ := time.Parse(\"2006-01-02 15:04:05\", datetime_str)\r\nfmt.Printf(\"value: %s, type: %T\\n\", dt, dt) \/\/ value: 2018-10-21 12:24:51 +0000 UTC, type: time.Time\r\nfmt.Printf(\"date: %s, datetime: %s\\n\", dt.Format(\"2006-01-02\"), dt.Format(\"2006-01-02 15:04:05\")) \/\/ date: 2018-10-21, datetime: 2018-10-21 12:24:51\r\n<\/pre>\n<h6>\u5982\u4f55\u83b7\u53d6\u4e0b\u4e00\u5c0f\u65f6\u3001\u660e\u5929\u7684\u4fe1\u606f\uff1f<\/h6>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    start_time := time.Now()\r\n\r\n    duration, _ := time.ParseDuration(\"1h\")\r\n    fmt.Printf(\"duration is: %q, (%T)\\n\", duration, duration) \/\/ duration is: \"1h0m0s\", (time.Duration)\r\n\r\n    next_hour := start_time.Add(duration)\r\n    fmt.Printf(\"1h later is: %q, (%T)\\n\", next_hour.Format(\"2006-01-02 15:04:05\"), next_hour)\r\n\r\n    last_hour := start_time.Add(-duration) \/\/ \u8fd9\u91cc\u7528\u4e86\u4e00\u4e2a\u300c\u8d1f\u53f7\u300d\r\n    fmt.Printf(\"1h before is: %q, (%T)\\n\", last_hour.Format(\"2006-01-02 15:04:05\"), last_hour)\r\n\r\n    yesterday := start_time.AddDate(0, 0, -1) \/\/ yesterday is: 2018-10-20, (time.Time)\r\n    fmt.Printf(\"yesterday is: %s, (%T)\\n\", yesterday.Format(\"2006-01-02\"), yesterday)\r\n\r\n    tomorrow := start_time.AddDate(0, 0, 1) \/\/ tomorrow is: 2018-10-22, (time.Time)\r\n    fmt.Printf(\"tomorrow is: %s, (%T)\\n\", tomorrow.Format(\"2006-01-02\"), tomorrow)\r\n\r\n}\r\n<\/pre>\n<h6>\u5982\u4f55\u6682\u505c(sleep)\u6267\u884c\uff1f<\/h6>\n<pre class=\"lang:default decode:true\">\/\/ \u6682\u505c2\u79d2\r\ntime.Sleep(time.Duration(2) * time.Second)\r\n\/\/ https:\/\/stackoverflow.com\/questions\/17573190\/how-to-multiply-duration-by-integer\r\n<\/pre>\n<h6>\u5982\u4f55\u83b7\u53d6\u7a0b\u5e8f\u7684\u6267\u884c\u8017\u65f6\uff1f<\/h6>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    start_time := time.Now()\r\n\r\n    \/\/ \u5c06\u64cd\u4f5c\u653e\u5728\u8fd9\u91cc\uff0c\u8fd0\u884c\u7ed3\u675f\u65f6\u4f1a\u6253\u5370\u51fa\u8017\u65f6\uff08\u4ee5\u300c\u79d2\u300d\u4e3a\u5355\u4f4d\uff09\r\n\r\n    \/\/ Seconds() \u8fd4\u56de\u7684\u662f float64 \u7c7b\u578b\u7684\u53d8\u91cf\r\n    fmt.Printf(\"Spend %v seconds.\\n\", time.Since(start_time).Seconds()) \/\/ Spend 0.000141 seconds.\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">func ParseDuration(s string) (Duration, error)\r\n`\r\nParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as \"300ms\", \"-1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"\u00b5s\"), \"ms\", \"s\", \"m\", \"h\".\r\n\r\nParseDuration \u7528\u4e8e\u89e3\u6790\u4e00\u4e2a\u8868\u793a\u6301\u7eed\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u3002\u6301\u7eed\u65f6\u95f4\u5b57\u7b26\u4e32\u53ef\u80fd\u662f\u4e00\u4e2a\u6709\u7b26\u53f7\u7684\u5341\u8fdb\u5236\u6570\u5b57\u5e8f\u5217\uff0c\u6bcf\u4e2a\u6570\u5b57\u90fd\u6709\u53ef\u9009\u7684\u5206\u6570\u548c\u5355\u4f4d\u540e\u7f00\uff0c\u4f8b\u5982\u201c300ms\u201d\u3001\u201c-1.5h\u201d\u6216\u201c2h45m\u201d\u3002\u6709\u6548\u7684\u65f6\u95f4\u5355\u4f4d\u662f\u201cns\u201d,\u201cus\u201d(\u6216\u201c\u00b5s\u201d),\u201cms\u201d,\u201cs\u201d,\u201cm\u201d,\u201ch\u201d\u3002\r\n`\r\n<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">func ParseInLocation(layout, value string, loc *Location) (Time, error)\r\n`\r\nParseInLocation is like Parse but differs in two important ways. First, in the absence of time zone information, Parse interprets a time as UTC; ParseInLocation interprets the time as in the given location. Second, when given a zone offset or abbreviation, Parse tries to match it against the Local location; ParseInLocation uses the given location.\r\n\r\nParseInLocation \u4e0e Parse \u7c7b\u4f3c\uff0c\u4f46\u6709\u4e24\u79cd\u91cd\u8981\u7684\u533a\u522b\u3002\u9996\u5148\uff0c\u5728\u6ca1\u6709\u65f6\u533a\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\uff0cParse\u5c06\u65f6\u95f4\u89e3\u91ca\u4e3aUTC\uff0c\u800cParseInLocation\u7528\u7ed9\u5b9a\u4f4d\u7f6e\u6765\u8fdb\u884c\u89e3\u91ca\u3002\u5176\u6b21\uff0c\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u533a\u57df\u504f\u79fb\u91cf\u6216\u7f29\u5199\u65f6\uff0cParse\u8bd5\u56fe\u5c06\u5b83\u4e0e\u672c\u5730\u4f4d\u7f6e\u8fdb\u884c\u5339\u914d\uff1b\u800cParseInLocation\u4f7f\u7528\u7ed9\u5b9a\u7684\u4f4d\u7f6e\u3002\r\n`\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/blog.csdn.net\/wschq\/article\/details\/80114036\">golang\u5305time\u7528\u6cd5\u8be6\u89e3<\/a>#<strong>nice<\/strong><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/herohenu\/article\/details\/44857689\">golang\u65f6\u95f4\u683c\u5f0f\u5316<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/sunicdavy\/article\/details\/44652747\">golang\u7684time.Format\u7684\u5751<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/piperck\/p\/6471352.html\">Golang\u7684\u65f6\u95f4\u751f\u6210\uff0c\u683c\u5f0f\u5316\uff0c\u4ee5\u53ca\u83b7\u53d6\u51fd\u6570\u6267\u884c\u65f6\u95f4\u7684\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/baiyuxiong\/p\/4349595.html\">Go\u65f6\u95f4\u6233\u548c\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u6362<\/a><\/li>\n<li>http:\/\/localhost:8080\/pkg\/time\/#pkg-examples<\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/ghj1976\/p\/4255943.html\">golang \u8bb0\u5f55\u51fd\u6570\u6267\u884c\u8017\u65f6\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u3002<\/a>#<strong>nice<\/strong><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u4e00\u4e9b\u65e5\u5fd7\u5206\u6790\u7a0b\u5e8f\u4e2d\uff0c\u65e5\u671f\/\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u89e3\u6790\u64cd\u4f5c\u8981\u7528\u5230\u7684\u5730\u65b9\u6709\u5f88\u591a\uff0c\u4e4b\u524d\u5728\u7528Pytho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1237,564,546],"class_list":["post-4134","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-duration","tag-golang","tag-time"],"views":2562,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4134"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4134\/revisions"}],"predecessor-version":[{"id":4143,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4134\/revisions\/4143"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}