{"id":208304,"date":"2025-07-08T12:13:31","date_gmt":"2025-07-08T04:13:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208304\/"},"modified":"2025-07-08T12:13:31","modified_gmt":"2025-07-08T04:13:31","slug":"%e5%9c%a8%e6%b5%8b%e8%af%95-golang-%e6%9c%9f%e9%97%b4%e5%ad%98%e6%a0%b9-time-now-%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208304\/","title":{"rendered":"\u5728\u6d4b\u8bd5 golang \u671f\u95f4\u5b58\u6839 time.Now() \uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u6d4b\u8bd5 golang \u671f\u95f4\u5b58\u6839 time.Now() \uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 15:15:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u6d4b\u8bd5 golang \u671f\u95f4\u5b58\u6839 time.Now() \uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728\u8fd9\u91cc\u9075\u5faa\u53e6\u4e00\u4e2a\u7b54\u6848\uff1a\u662f\u5426\u6709\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u5728\u6d4b\u8bd5\u671f\u95f4\u5168\u5c40\u5220\u9664 time.now() \uff1f<\/p>\n<h3>\u6240\u4ee5\u6211\u6709\u8fd9\u4e2a\u6587\u4ef6\uff0c\u6211\u53ef\u4ee5\u5728\u5176\u4e2d\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/h3>\n<pre>var timenow = time.now\n\nfunc generatetimestamp() int64 {\n    now := timenow() \/\/ current local time\n    sec := now.unix() \/\/ number of seconds since january 1, 1970 utc\n\n    return sec \/\/ int 64\n}<\/pre>\n<h3>\u6211\u4eec\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528generatetimestamp()<\/h3>\n<pre>func addts() {\n    \/\/ some if check, use generatetimestamp() here\n}<\/pre>\n<h3>\u73b0\u5728\u5728\u6211\u7684\u6d4b\u8bd5\u6587\u4ef6\u4e2d\uff0c\u6211\u6b63\u5728\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/h3>\n<pre>now := time.Now()\n    timeNow = func() int64 {\n        \/\/ fmt.Println(\"looking for this\", now.Unix())\n        return now.Unix()\n    }<\/pre>\n<p>\u6211\u6536\u5230\u6b64\u9519\u8bef <code> \u4e0d\u80fd\u4f7f\u7528 func \u6587\u5b57\uff08\u7c7b\u578b func() int64\uff09\u4f5c\u4e3a\u7c7b\u578b func() time.time \u5728\u8d4b\u503c <\/code> \u4e2d\u3002\u6211\u9700\u8981\u80fd\u591f\u8fd4\u56de int64 \u7c7b\u578b\uff08\u6211\u7684\u539f\u59cb\u51fd\u6570\u8fd4\u56de\uff09\uff0c\u6211\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f<\/p>\n<p>\u8bf7\u968f\u65f6\u5411\u6211\u6307\u51fa\u6587\u6863\uff0c\u6211\u662f go \u65b0\u624b\uff01\uff01<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p> \u662f\u4e00\u4e2a\u8fd4\u56de \u7c7b\u578b\u503c\u7684\u51fd\u6570\uff1a<\/p>\n<pre>func now() time<\/pre>\n<p>\u6240\u4ee5<code>timenow<\/code>\u7684\u7c7b\u578b\u662f\u8fd9\u79cd\u7c7b\u578b\u7684\u51fd\u6570\uff1a<code>func() time.time<\/code>\u3002\u8fd9\u4e0e <code>func() int64<\/code> \u660e\u663e\u4e0d\u540c\u3002<\/p>\n<p>\u60a8\u5fc5\u987b\u8fd4\u56de <code>time.time<\/code> \u503c\u3002\u5982\u679c\u60a8\u60f3\u8fd4\u56de\u4ee3\u8868\u7279\u5b9aunix\u65f6\u95f4\u7684 <code>time.time<\/code> \u503c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u51fd\u6570\u6765\u83b7\u53d6\u8be5\u503c\uff0c\u4f8b\u5982<\/p>\n<pre>unixsec := int64(1605139200) \/\/ this is 2020-11-12 00:00:00 +0000 utc\n\ntimenow = func() time.time {\n    return time.unix(unixsec, 0)\n}<\/pre>\n<p>\u5982\u679c\u60a8\u60f3\u8fd4\u56de\u7279\u5b9a\u7684\u65e5\u671f\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528 \uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>timenow = func() time.time {\n    return time.date(2020, 11, 12, 0, 0, 0, 0, time.utc)\n}<\/pre>\n<p>\u5f53\u7136\uff0c\u60a8\u4e0d\u9650\u4e8e\u59cb\u7ec8\u8fd4\u56de\u540c\u4e00\u65f6\u95f4\u70b9\u3002\u60a8\u53ef\u4ee5\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u9012\u589e\u7684\u65f6\u95f4\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>unixsec := int64(1605139200) \/\/ This is 2020-11-12 00:00:00 +0000 UTC\n\ntimeNow = func() time.Time {\n    unixsec++ \/\/ Increment number of seconds by 1\n    return time.Unix(unixsec, 0)\n}<\/pre>\n<p>\u6b64 <code>timenow<\/code> \u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u59cb\u7ec8\u589e\u52a0 1 \u79d2\u7684\u65f6\u95f4\u503c\uff08\u4e0e\u4e0a\u4e00\u6b21\u8c03\u7528\u8fd4\u56de\u7684\u503c\u76f8\u6bd4\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5728\u6d4b\u8bd5 golang \u671f\u95f4\u5b58\u6839 time.Now() \uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208304","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208304"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208304\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}