{"id":208173,"date":"2025-07-08T13:25:10","date_gmt":"2025-07-08T05:25:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208173\/"},"modified":"2025-07-08T13:25:10","modified_gmt":"2025-07-08T05:25:10","slug":"%e9%9c%80%e8%a6%81%e5%b8%ae%e5%8a%a9%e5%b0%86%e6%97%b6%e9%97%b4%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%a7%92%ef%bc%8c%e5%8f%af%e4%bb%a5%e5%9c%a8-golang-%e4%b8%ad%e5%a4%84%e7%90%86%e5%b0%8f%e6%97%b6%e5%92%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208173\/","title":{"rendered":"\u9700\u8981\u5e2e\u52a9\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\uff0c\u53ef\u4ee5\u5728 Golang \u4e2d\u5904\u7406\u5c0f\u65f6\u548c\u65e0\u5c0f\u65f6"},"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>\u9700\u8981\u5e2e\u52a9\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\uff0c\u53ef\u4ee5\u5728 Golang \u4e2d\u5904\u7406\u5c0f\u65f6\u548c\u65e0\u5c0f\u65f6<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 21:00:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u9700\u8981\u5e2e\u52a9\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\uff0c\u53ef\u4ee5\u5728 Golang \u4e2d\u5904\u7406\u5c0f\u65f6\u548c\u65e0\u5c0f\u65f6\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u60f3\u8981\u5c06\u6301\u7eed\u65f6\u95f4\u66f4\u6539\u4e3a\u79d2\uff0c\u4f46\u4e0d\u60f3\u6bcf\u6b21\u90fd\u7ed9\u51fa\u5c0f\u65f6\uff08\u5c0f\u65f6\u4e3a\u53ef\u9009\uff09<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n  t1 := \"01:30\"\n  seconds, _ := ConvertTimeFormat(t1) \/\/ not working fine for this\n  fmt.Println(seconds)\n\n   t2 := \"01:01:15\"\n   second, _ := ConvertTimeFormat(t2) \/\/ working fine for this\n   fmt.Println(second)\n}\n\nfunc ConvertTimeFormat(st string) (int, error) {\n    var h, m, s int\n    n, err := fmt.Sscanf(st, \"%d:%d:%d\", &amp;h, &amp;m, &amp;s)\n    fmt.Print(n, err)\n    if err != nil || n != 3 {\n        return 0, err  \n    }\n    return h*3600 + m*60 + s, nil\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u4ee3\u7801\u6709\u9519\u8bef\uff0c\u56e0\u4e3a\u60a8\u603b\u662f\u671f\u671b scanf \u51fd\u6570\u4e2d\u6709 3 \u4e2a\u503c\u3002<\/p>\n<p>\u5c1d\u8bd5\u7528\u53e6\u4e00\u79cd\u65b9\u5f0f\u89e3\u6790\u5b83\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n    \"strconv\"\n    \"strings\"\n)\n\nfunc main() {\n    t1 := \"01:30\"\n    seconds, _ := converttimeformat(t1) \/\/ not working fine for this\n    fmt.println(seconds)\n\n    t2 := \"01:01:15\"\n    second, _ := converttimeformat(t2) \/\/ working fine for this\n    fmt.println(second)\n}\n\nfunc converttimeformat(st string) (int, error) {\n    var sh, sm, ss string\n    parts := strings.split(st, \":\")\n    switch len(parts) {\n    case 2:\n        sm = parts[0]\n        ss = parts[1]\n    case 3:\n        sh = parts[0]\n        sm = parts[1]\n        ss = parts[2]\n    default:\n        return 0, errors.new(\"invalid format\")\n    }\n\n    var (\n        h, m, s int\n        err     error\n    )\n    if sh != \"\" {\n        h, err = strconv.atoi(sh)\n        if err != nil {\n            return 0, err\n        }\n    }\n    m, err = strconv.atoi(sm)\n    if err != nil {\n        return 0, err\n    }\n    s, err = strconv.atoi(ss)\n    if err != nil {\n        return 0, err\n    }\n    return h*3600 + m*60 + s, nil\n}<\/pre>\n<\/p>\n<p>\u53e6\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u662f\u5904\u7406\u9519\u8bef\u5e76\u5c1d\u8bd5\u4ee5\u53e6\u4e00\u79cd\u683c\u5f0f\u89e3\u6790\u5b83\uff1a<\/p>\n<pre>func ConvertTimeFormat(st string) (int, error) {\n    var h, m, s int\n    _, err := fmt.Sscanf(st, \"%d:%d:%d\", &amp;h, &amp;m, &amp;s)\n    fmt.Println(h, m, s, err)\n    if err != nil {\n        h, m, s = 0, 0, 0\n        _, err = fmt.Sscanf(st, \"%d:%d\", &amp;m, &amp;s)\n        if err != nil {\n            return 0, err\n        }\n    }\n    return h*3600 + m*60 + s, nil\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u9700\u8981\u5e2e\u52a9\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\uff0c\u53ef\u4ee5\u5728 Golang \u4e2d\u5904\u7406\u5c0f\u65f6\u548c\u65e0\u5c0f\u65f6\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\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-208173","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208173","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=208173"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208173\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}