{"id":207465,"date":"2025-07-08T12:45:34","date_gmt":"2025-07-08T04:45:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207465\/"},"modified":"2025-07-08T12:45:34","modified_gmt":"2025-07-08T04:45:34","slug":"%e5%9c%a8-go-%e4%b8%ad%e5%b0%86-utc-%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%9c%ac%e5%9c%b0%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207465\/","title":{"rendered":"\u5728 Go \u4e2d\u5c06 UTC \u8f6c\u6362\u4e3a\u201c\u672c\u5730\u201d\u65f6\u95f4"},"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 Go \u4e2d\u5c06 UTC \u8f6c\u6362\u4e3a\u201c\u672c\u5730\u201d\u65f6\u95f4<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 20:18:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5728 Go \u4e2d\u5c06 UTC \u8f6c\u6362\u4e3a\u201c\u672c\u5730\u201d\u65f6\u95f4\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u5c06 utc \u65f6\u95f4\u8f6c\u6362\u4e3a<em>\u672c\u5730\u65f6\u95f4<\/em>\uff1f<\/p>\n<p>\u6211\u5df2\u7ecf\u4e3a\u6211\u9700\u8981\u5f53\u5730\u65f6\u95f4\u7684\u6240\u6709\u56fd\u5bb6\/\u5730\u533a\u521b\u5efa\u4e86\u4e00\u5f20\u5177\u6709 utc \u5dee\u5f02\u7684\u5730\u56fe\u3002\u7136\u540e\uff0c\u6211\u5c06\u8be5\u5dee\u5f02\u4f5c\u4e3a\u6301\u7eed\u65f6\u95f4\u6dfb\u52a0\u5230\u5f53\u524d\u65f6\u95f4 (utc) \u4e2d\uff0c\u5e76\u6253\u5370\u7ed3\u679c\uff0c\u5e0c\u671b\u8fd9\u662f\u8be5\u7279\u5b9a\u56fd\u5bb6\/\u5730\u533a\u7684\u5f53\u5730\u65f6\u95f4\u3002<\/p>\n<p>\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\uff0c\u7ed3\u679c\u662f\u9519\u8bef\u7684\u3002\u4f8b\u5982\uff0c\u5308\u7259\u5229\u7684\u65f6\u5dee\u4e3a\u4e00\u5c0f\u65f6\u3002\u77e5\u9053\u4e3a\u4ec0\u4e48\u6211\u5f97\u5230\u7684\u7ed3\u679c\u4e0d\u6b63\u786e\u5417\uff1f<\/p>\n<pre>package main\n\nimport \"fmt\"\nimport \"time\"\n\nfunc main() {\n\n    m := make(map[string]string)\n    m[\"Hungary\"] = \"+01.00h\"\n\n    offSet, err := time.ParseDuration(m[\"Hungary\"])\n    if err != nil {\n        panic(err)\n    }\n    t := time.Now().UTC().Add(offSet)\n    nice := t.Format(\"15:04\")\n\n    fmt.Println(nice)\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u6f14\u793a\u6587\u7a3f\u7684\u65f6\u95f4\u8bbe\u7f6e\u4e3a <code>2009-11-10 23:00:00 +0000 utc<\/code>\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>time.loadlocation<\/code> \uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>var countrytz = map[string]string{\n    \"hungary\": \"europe\/budapest\",\n    \"egypt\":   \"africa\/cairo\",\n}\n\nfunc timein(name string) time.time {\n    loc, err := time.loadlocation(countrytz[name])\n    if err != nil {\n        panic(err)\n    }\n    return time.now().in(loc)\n}\n\nfunc main() {\n    utc := time.now().utc().format(\"15:04\")\n    hun := timein(\"hungary\").format(\"15:04\")\n    eg := timein(\"egypt\").format(\"15:04\")\n    fmt.println(utc, hun, eg)\n}<\/pre>\n<p>\u60a8\u7684\u65b9\u6cd5\u6709\u7f3a\u9677\u3002\u4e00\u4e2a\u56fd\u5bb6\u53ef\u4ee5\u6709\u591a\u4e2a\u65f6\u533a\uff0c\u4f8b\u5982\u7f8e\u56fd\u548c\u4fc4\u7f57\u65af\u3002\u7531\u4e8e\u590f\u4ee4\u65f6 (dst)\uff0c\u4e00\u4e2a\u65f6\u533a\u53ef\u4ee5\u6709\u591a\u4e2a\u65f6\u95f4\uff0c\u4f8b\u5982\u5308\u7259\u5229\u3002\u5308\u7259\u5229\u7684\u590f\u4ee4\u65f6\u65f6\u95f4\u4e3a utc+1:00\uff0c\u590f\u4ee4\u65f6\u65f6\u95f4\u4e3a utc+2:00\u3002<\/p>\n<p>\u5bf9\u4e8e\u60a8\u60f3\u8981\u7ed9\u5b9a utc \u65f6\u95f4\u7684\u672c\u5730\u65f6\u95f4\u7684\u6bcf\u4e2a\u4f4d\u7f6e\uff0c\u8bf7\u4f7f\u7528 iana (tzdata) \u65f6\u533a\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    utc := time.now().utc()\n    fmt.println(utc)\n    local := utc\n    location, err := time.loadlocation(\"europe\/budapest\")\n    if err == nil {\n        local = local.in(location)\n    }\n    fmt.println(\"utc\", utc.format(\"15:04\"), local.location(), local.format(\"15:04\"))\n    local = utc\n    location, err = time.loadlocation(\"america\/los_angeles\")\n    if err == nil {\n        local = local.in(location)\n    }\n    fmt.println(\"utc\", utc.format(\"15:04\"), local.location(), local.format(\"15:04\"))\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>2014-08-14 23:57:09.151377514 +0000 UTC\nUTC 23:57 Europe\/Budapest 01:57\nUTC 23:57 America\/Los_Angeles 16:57<\/pre>\n<p>\u53c2\u8003\u6587\u732e\uff1a<\/p>\n<\/p>\n<\/p>\n<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728 Go \u4e2d\u5c06 UTC \u8f6c\u6362\u4e3a\u201c\u672c\u5730\u201d\u65f6\u95f4\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207465","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207465","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=207465"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207465\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}