{"id":10006873,"date":"2022-11-18T17:41:18","date_gmt":"2022-11-18T09:41:18","guid":{"rendered":"https:\/\/ityet.com\/?p=10006873"},"modified":"2022-11-18T17:41:18","modified_gmt":"2022-11-18T09:41:18","slug":"go1-20-%e4%b8%ad%e4%b8%a4%e4%b8%aa%e5%85%b3%e4%ba%8e-time-%e7%9a%84%e6%9b%b4%e6%96%b0%ef%bc%8c%e7%bb%88%e4%ba%8e%e4%b8%8d%e7%94%a8%e8%83%8c-2006-01-02-150405-%e4%ba%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006873","title":{"rendered":"Go1.20 \u4e2d\u4e24\u4e2a\u5173\u4e8e Time \u7684\u66f4\u65b0\uff0c\u7ec8\u4e8e\u4e0d\u7528\u80cc 2006-01-02 15:04:05 \u4e86\uff01"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p>Go \u8bed\u8a00\u4e2d\u6709\u4e00\u4e2a\u4e1c\u897f\u662f\u6bd4\u8f83\u6709\u7279\u8272\u7684\uff0c\u90a3\u5c31\u662f time \u6807\u51c6\u5e93\uff0c\u5728\u5404\u7c7b\u4e0e\u65f6\u95f4\u6709\u5173\u7684\u573a\u666f\u90fd\u4f1a\u5e38\u5e38\u7528\u5230\uff0c\u4f8b\u5982\uff1a\u5b9a\u65f6\/\u5ef6\u8fdf\u4efb\u52a1\u3001\u6570\u636e\u66f4\u65b0\u3001\u65f6\u95f4\u6bd4\u8f83\u3002<\/p>\n<p>\u5b98\u65b9 Demo \u662f\u4e00\u4e2a\u8ba1\u7b97\u65f6\u95f4\u5dee\u7684\u6837\u4f8b\uff1a<\/p>\n<pre><code class=\"go\">import &quot;time&quot;\n\nfunc main() {\n    start := time.Now()\n    time.Sleep(time.Second)\n    t := time.Now()\n    elapsed := t.Sub(start)\n    println(elapsed)\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>1000000000<\/code><\/pre>\n<h2>\u795e\u79d8\u7684 2006-01-02 15:04:05<\/h2>\n<p>\u5728\u793e\u533a\u53cd\u9988\u4e2d\uff0c\u66f4\u864e\u7684\u662f\u4ee5\u4e0b\u8fd9\u4e2a\u6837\u4f8b\uff1a<\/p>\n<pre><code class=\"go\">func main() {\n    now := time.Now()\n    v := now.Format(&quot;2006-01-02 15:04:05&quot;)\n    println(v)\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>2009-11-10 23:00:00<\/code><\/pre>\n<p>\u8fd9\u65f6\u5019\u5927\u5bb6\u4f1a\u9047\u5230\u51e0\u4e2a\u95ee\u9898&#8230;<strong>\u6700\u88ab\u5e38\u95ee\u7684\u662f 2006-01-02 15:04:05<\/strong>\uff0c\u8fd9\u662f\u968f\u4fbf\u5199\u7684\u65f6\u95f4\u70b9\u5417\uff0c\u751a\u81f3\u8fd8\u66fe\u7ecf\u6709\u4f20\u8a00\u8bf4\u8fd9\u662f Go \u8bed\u8a00\u7684\u8bde\u751f\u65f6\u95f4&#8230;<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u662f\u8fd9\u4e2a\u65f6\u95f4\u70b9<\/h3>\n<p>\u5b9e\u9645\u4e0a 2006-01-02 15:04:05 \u662f\u4e00\u4e2a\u53c2\u8003\u65f6\u95f4\u7684\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f\u5176\u5b83\u8bed\u8a00\u4e2d <code>Y-m-d H:i:s<\/code> \u683c\u5f0f\uff0c\u5728\u529f\u80fd\u4e0a\u7528\u4e8e\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u5904\u7406\u548c\u89c4\u8303\u3002<\/p>\n<p>\u90a3\u4e3a\u4ec0\u4e48\u662f 2006-01-02 15:04:05 \u5462\uff1f\u5176\u5b9e\u8fd9\u4e9b \u201d\u6570\u5b57\u201c \u662f\u6709\u610f\u4e49\u7684\uff0c\u6bcf\u4e2a\u5e03\u5c40\u5b57\u7b26\u4e32\u90fd\u662f\u4e00\u4e2a\u65f6\u95f4\u6233\u7684\u8868\u793a\uff0c\u5e76\u975e\u968f\u4fbf\u5199\u7684\u65f6\u95f4\u70b9\u3002<\/p>\n<p>\u53ef\u53c2\u89c1\u5b98\u65b9\u4f8b\u5b50\u4e2d\u7684\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<pre><code>Jan 2 15:04:05 2006 MST\n1   2  3  4  5    6  -7<\/code><\/pre>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u5176\u8bb0\u5fc6\u4e3a 2006 \u5e74 1 \u6708 2 \u65e5 3 \u70b9 4 \u5206 5 \u79d2\u3002<\/p>\n<h3>\u786e\u5b9e\u80cc\u4e0d\u4e0b\u6765\uff0c\u5feb\u4f18\u5316<\/h3>\n<p>\u867d\u7136 Go \u5b98\u65b9\u7ed9\u51fa\u4e86\u4e00\u7cfb\u5217\u611f\u89c9\u5f88\u5408\u7406\u7684\u89e3\u91ca\uff0c\u4e5f\u901a\u8fc7\u4e0a\u9762\u7684\u65b9\u5f0f\u8bd5\u56fe\u6559\u4f1a\u5927\u5bb6\u9760\u7406\u89e3\u800c\u4e0d\u662f\u9760\u7eaf\u80cc\u628a\u8fd9\u4e2a\u65f6\u95f4\u70b9\u683c\u5f0f\u89c4\u8303\u7ed9\u8bb0\u4e0b\u6765&#8230;<\/p>\n<p>\u73b0\u5b9e\u662f\u5f88\u6b8b\u9177\u7684\uff0c\u6bcd\u8bed\u4e0d\u540c\uff0c\u4e1a\u52a1\u7c7b\u578b\u4e0d\u540c\uff0c\u4e0d\u4f1a\u7ecf\u5e38\u7528\u5230 time \u6807\u51c6\u5e93\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u5177\u4f53\u6570\u503c\u3002<\/p>\n<p>\u5927\u4f6c\u4e5f\u5206\u6790\u4e86 Go modules \u4e0a\u7684\u6240\u6709\u6a21\u5757\u4ee3\u7801\uff0c\u53d1\u73b0\u8fd9\u4e2a\u65e5\u671f\u65f6\u95f4\u70b9\uff0c\u4f7f\u7528\u7684\u6b21\u6570\u975e\u5e38\u9ad8\u9891\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6392\u540d<\/th>\n<th>\u9891\u7387<\/th>\n<th>\u683c\u5f0f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>75616<\/td>\n<td>time.RFC3339<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>23954<\/td>\n<td>time.RFC3339Nano<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>13312<\/td>\n<td>&#8220;2006-01-02 15:04:05&#8221;<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>12332<\/td>\n<td>&#8220;2006-01-02&#8221;<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>11940<\/td>\n<td>time.RFC1123<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7efc\u4e0a\u6570\u636e\u6240\u8ff0\uff0c\u7ec8\u4e8e\u5728 Go \u8fc7\u53bb\u4e86 13 \u5e74\u540e\uff0c\u5c06\u4f1a\u5728 Go1.20 \u589e\u52a0\u5982\u4e0b\u4e09\u4e2a\u5e38\u91cf\uff1a<\/p>\n<pre><code class=\"go\">DateTime   = &quot;2006-01-02 15:04:05&quot;\nDateOnly   = &quot;2006-01-02&quot;\nTimeOnly   = &quot;15:04:05&quot;<\/code><\/pre>\n<p>\u65b0\u7248\u672c\u4e2d\u76f4\u63a5\u5f15\u7528\u5373\u53ef\u3002<\/p>\n<p>\u4ee5\u540e\u95ee\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\u7684\u540c\u5b66\uff0c\u53ef\u80fd\u4f1a\u53d8\u5c11\u3002<\/p>\n<h2>\u652f\u6301\u6bd4\u8f83\uff1aTime.Compare<\/h2>\n<p>\u76ee\u524d\u5728\u6807\u51c6\u5e93\u4e2d\uff0c\u6709 3 \u4e2a\u65b9\u6cd5\u6765\u6bd4\u8f83 time.Time \u5bf9\u8c61\uff0c\u5206\u522b\u662f\uff1aBefore()\u3001Equal() \u548c After()\uff0c\u4f5c\u7528\u4e0a\u7c7b\u4f3c &lt;\u3001== \u548c &gt;\u3002\u4f46\u7f3a\u5c11 &lt;= \u548c &gt;= \u7684\u7b49\u4ef7\u7269\uff0c\u56e0\u6b64\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u65bd\u8d77\u6765\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002<\/p>\n<p>\u7ecf\u5e38\u4e00\u756a\u8ba8\u8bba\uff0c\u5c06<strong>\u4f1a\u5728 Go1.20 \u652f\u6301 Time.Compare<\/strong>\uff0c\u4ee5\u6b64\u6765\u8fbe\u5230\u7c7b\u4f3c\u7684\u6548\u679c\u3002\u4f5c\u7528\u662f\u5c06 Time \u5bf9\u8c61 t \u548c u \u4e24\u8005\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p>\u51fd\u6570\u8fd4\u56de\u5982\u4e0b\u51e0\u79cd\u7ed3\u679c\uff1a<\/p>\n<ul>\n<li>\u5982\u679c t \u5728 u \u4e4b\u524d\uff0c\u5219\u8fd4\u56de -1\u3002<\/li>\n<li>\u5982\u679c t \u5728 u \u4e4b\u540e\uff0c\u5219\u8fd4\u56de +1\u3002<\/li>\n<li>\u5982\u679c\u5b83\u4eec\u76f8\u540c\uff0c\u5219\u8fd4\u56de 0\u3002<\/li>\n<\/ul>\n<p>\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">func (t Time) Compare(u Time) int {\n    var tc, uc int64\n    if t.wall&amp;u.wall&amp;hasMonotonic != 0 {\n        tc, uc = t.ext, u.ext\n    } else {\n        tc, uc = t.sec(), u.sec()\n        if tc == uc {\n            tc, uc = int64(t.nsec()), int64(u.nsec())\n        }\n    }\n    switch {\n    case tc &lt; uc:\n        return -1\n    case tc &gt; uc:\n        return +1\n    }\n    return 0\n}<\/code><\/pre>\n<p>\u51fd\u6570\u5185\u90e8\u6bd4\u8f83\u7b80\u5355\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Go Time \u4e00\u76f4\u90fd\u662f\u8bdd\u9898\u699c\u4e0a\u7684\u8001\u9762\u5b54\u3002\u8fd9\u4e00\u6b21\u5728 Go1.20 \u7684\u65b0\u7279\u6027\u4e2d\uff0c\u5bf9 2006-01-02 15:04:05 \u5e38\u7528\u7684\u65f6\u95f4\u70b9\u8fdb\u884c\u4e86\u5e38\u91cf\u58f0\u660e\uff0c\u4fbf\u4e8e\u6211\u4eec\u540e\u7eed\u76f4\u63a5\u5f15\u7528\uff0c\u4e0d\u7528 \u201c\u6b7b\u8bb0\u786c\u80cc\u201d\u3002<\/p>\n<p>\u5728 Time \u5bf9\u8c61\u7684\u6bd4\u8f83\u4e0a\uff0c\u4e5f\u8865\u5168\u4e86 Compare \u65b9\u6cd5\uff0c\u4fbf\u4e8e\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u4e4b\u524d\u4e4b\u540e\u7684\u5bf9\u6bd4\u3002<\/p>\n<p>Go1.20 \u5c06\u5728\u660e\u5e74 2023 \u5e74 2\uff0c3 \u6708\u4efd\u5de6\u53f3\u5c31\u53ef\u4ee5\u89c1\u5230\u4e86\u300211 \u6708\u5e95\u4f1a\u53d1\u9884\u89c8\u7248\u672c\u3002\u51b2\u4e0d\uff1f<\/p>\n<blockquote><p>\u6587\u7ae0\u6301\u7eed\u66f4\u65b0\uff0c\u53ef\u4ee5\u5fae\u4fe1\u641c\u3010\u8111\u5b50\u8fdb\u714e\u9c7c\u4e86\u3011\u9605\u8bfb\uff0c\u672c\u6587 <strong>GitHub<\/strong> <a href=\"https:\/\/link.segmentfault.com\/?enc=JQdv5mQ9niBXDUtrpstblw%3D%3D.%2FEL0P6pJusDr%2FrljBSE0Zn%2B7qiJv5hiUujY77eN%2BPzQ%3D\" rel=\"nofollow\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=FC4wy8GUo7YgY8YerPG8Og%3D%3D.qjQjYubtSFlBbh6gHJTOk2RKaQWdoxrTF83FV7tuGvUMXTJSCxoEw7Sovg%2F4rznn\" rel=\"nofollow\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<h2>\u63a8\u8350\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=1z33cFHrmVlbma4JzSXAkA%3D%3D.DRjC3H1Fxw%2BmlmIdfqxglagIC77WlK1Au1MhqUCGftTpPQPWVhQEYcbjWRHj7t7sBugjqbNmaeFrImjIxsfpqQ%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=CcGs1OsVH0K9jHU%2Bvuu1ZQ%3D%3D.heHZzdkYw46FLJqWPV5oqwowrAyDpW5NmPqeSrF%2BK1mF3Rdn7IpyOad2JBRC5I06sb3NmNQtRySyZ8UOhFmYbw%3D%3D\" rel=\"nofollow\">Go \u9519\u8bef\u5904\u7406\u65b0\u601d\u8def\uff1f\u7528\u5de6\u4fa7\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=9w%2BVHP9%2FK73pql5R1tWQ4A%3D%3D.RPUcx%2FDyz06UMM9N%2BTm9EP4ItOEknUmYNrp1tSppVoLlpM%2BDDxRsKWxWM5RfnAUTiODHC05c6Az4Byo0JOKzPg%3D%3D\" rel=\"nofollow\">\u5148\u7779\u4e3a\u5feb\uff0cGo2 Error \u7684\u6323\u624e\u4e4b\u8def<\/a><\/li>\n<\/ul>\n<h3>Go \u56fe\u4e66\u7cfb\u5217<\/h3>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=XNfMxKef7dSXyjg%2Foqu%2BEg%3D%3D.BzlfYfq7wLza9ga%2FjzcsX71aTNiCC%2FS6Dqiac6o8BrJzIWMXIvVIFy3BqbdWh4Fw\" rel=\"nofollow\">Go \u8bed\u8a00\u5165\u95e8\u7cfb\u5217\uff1a\u521d\u63a2 Go \u9879\u76ee\u5b9e\u6218<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=XtRDcdfdbk19sMy9qJl4pA%3D%3D.AYePHhPE1MbZQdJt4ijFRxYcpJN10yGtZbWMTcDxLjI%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u7f16\u7a0b\u4e4b\u65c5\uff1a\u6df1\u5165\u7528 Go \u505a\u9879\u76ee<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=Xf8SKPrYBBfwga%2B2WqkQUg%3D%3D.xfPwbgnxOMN1RcntQoclZw94d6uUsSr9QT1Mxxui0%2FY%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8bbe\u8ba1\u54f2\u5b66\uff1a\u4e86\u89e3 Go \u7684\u4e3a\u4ec0\u4e48\u548c\u8bbe\u8ba1\u601d\u8003<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=8%2F605DVPvA3vSb8avO5Rzg%3D%3D.7o%2FSvFfTWH%2B%2BMJJyOivpoeWAA3v6LeI8A18WsucMJVM%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8fdb\u9636\u4e4b\u65c5\uff1a\u8fdb\u4e00\u6b65\u6df1\u5165 Go \u6e90\u7801<\/a><\/li>\n<\/ul>\n<h4>\u63a8\u8350\u9605\u8bfb<\/h4>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=miHECZZvBbI3afL2jMpqig%3D%3D.tc%2Be5bCtPHpon%2FjoJ8%2F2l%2FKh%2BXoZvUpjfyJMf0oibHHoBrgOiaGFgerzVeImQg9H636rVr84CwO7zgAsUmY%2FZA%3D%3D\" rel=\"nofollow\">Go for \u5faa\u73af\u6709\u65f6\u5019\u771f\u7684\u5f88\u5751\u3002\u3002\u3002<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=XmsVpZUOrUrM6ZjCHxuMTA%3D%3D.vi1x%2BqfvLNDTjWt1QX7yHmcliX6DZtBO%2BECRJa9O4Siyt0j%2BPCx9rWRbOYIAvs1Zaxacb95RGtEDYawqgM4Y%2FQ%3D%3D\" rel=\"nofollow\">Go \u5341\u5e74\u4e86\uff0c\u7ec8\u4e8e\u60f3\u8d77\u8981\u7edf\u4e00 log \u5e93\u4e86\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=VBDxjCiO2JS%2FYST8AvTJbA%3D%3D.JC5FImZ3mTLIcsWadeoA9BVH5qXPH4n4A7ZgAgPhzuSA6526bYOjBWPVb4JEPfAk3S25IuaaIj8hMlLrFv8ghA%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042837742\">https:\/\/segmentfault.com\/a\/1190000042837742<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 Go \u8bed\u8a00\u4e2d\u6709\u4e00\u4e2a\u4e1c\u897f\u662f\u6bd4\u8f83\u6709\u7279\u8272\u7684\uff0c\u90a3\u5c31\u662f time \u6807\u51c6\u5e93\uff0c\u5728\u5404\u7c7b\u4e0e\u65f6\u95f4\u6709\u5173\u7684\u573a\u666f\u90fd\u4f1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006873","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006873"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006873\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}