Golang时间处理插件 https://github.com/noaway/dateparse

前言:在此之前的项目中,都是自己写的时间字符串处理函数,因为不够全面,陆续发现不够满足处理各种时间格式字符串,因为引进了dateparse

看一段示例代码:

package main

import (
    "flag"
    "fmt"
    "time"

    "github.com/apcera/termtables"
    "github.com/noaway/dateparse"
)

//       _           _
//      | |         | |
//    __| |   __ _  | |_    ___   _ __     __ _   _ __   ___    ___
//   / _` |  / _` | | __|  / _ \ | '_ \   / _` | | '__| / __|  / _ \
//  | (_| | | (_| | | |_  |  __/ | |_) | | (_| | | |    \__ \ |  __/
//   \__,_|  \__,_|  \__|  \___| | .__/   \__,_| |_|    |___/  \___|
//                               | |
//                               |_|

var examples = []string{
    "May 8, 2009 5:57:51 PM",
    "Nov 8, 2017",
    "Mon Jan  2 15:04:05 2006",
    "Mon Jan  2 15:04:05 MST 2006",
    "Mon Jan 02 15:04:05 -0700 2006",
    "Monday, 02-Jan-06 15:04:05 MST",
    "Mon, 02 Jan 2006 15:04:05 MST",
    "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
    "Mon, 02 Jan 2006 15:04:05 -0700",
    "Mon Aug 10 15:44:11 UTC+0100 2015",
    "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
    "12 Feb 2006, 19:17",
    "2013-Feb-03",
    //   mm/dd/yy
    "3/31/2014",
    "03/31/2014",
    "08/21/71",
    "8/1/71",
    "4/8/2014 22:05",
    "04/08/2014 22:05",
    "04/2/2014 03:00:51",
    "8/8/1965 12:00:00 AM",
    "8/8/1965 01:00:01 PM",
    "8/8/1965 01:00 PM",
    "8/8/1965 1:00 PM",
    "8/8/1965 12:00 AM",
    "4/02/2014 03:00:51",
    "03/19/2012 10:11:59",
    "03/19/2012 10:11:59.3186369",
    // yyyy/mm/dd
    "2014/3/31",
    "2014/03/31",
    "2014/4/8 22:05",
    "2014/04/08 22:05",
    "2014/04/2 03:00:51",
    "2014/4/02 03:00:51",
    "2012/03/19 10:11:59",
    "2012/03/19 10:11:59.3186369",
    //   yyyy-mm-ddThh
    "2006-01-02T15:04:05+0000",
    "2009-08-12T22:15:09-07:00",
    "2009-08-12T22:15:09",
    "2009-08-12T22:15:09Z",
    //   yyyy-mm-dd hh:mm:ss
    "2014-04-26 17:24:37.3186369",
    "2012-08-03 18:31:59.257000000",
    "2014-04-26 17:24:37.123",
    "2013-04-01 22:43:22",
    "2014-12-16 06:20:00 UTC",
    "2014-12-16 06:20:00 GMT",
    "2014-04-26 05:24:37 PM",
    "2014-04-26 13:13:43 +0800",
    "2014-04-26 13:13:44 +09:00",
    "2012-08-03 18:31:59.257000000 +0000 UTC",
    "2015-09-30 18:48:56.35272715 +0000 UTC",
    "2015-02-18 00:12:00 +0000 GMT",
    "2015-02-18 00:12:00 +0000 UTC",
    "2017-07-19 03:21:51+00:00",
    "2014-04-26",
    "2014-04",
    "2014",
    "2014-05-11 08:20:13,787",
    //  yyyymmdd and similar
    "20140601",
    // unix seconds, ms
    "1332151919",
    "1384216367189",
    // Chinese date
    "2017年11月09日",
    "2017年11月01日 09:41",
    // How long ago
    "1 day ago",
    "19 hours ago",
    "26 minutes ago",
}

var (
    timezone = ""
)

func main() {
    flag.StringVar(&timezone, "timezone", "UTC", "Timezone aka `America/Los_Angeles` formatted time-zone")
    flag.Parse()

    if timezone != "" {
        // NOTE:  This is very, very important to understand
        // time-parsing in go
        loc, err := time.LoadLocation(timezone)
        if err != nil {
            panic(err.Error())
        }
        time.Local = loc
    }

    table := termtables.CreateTable()

    table.AddHeaders("Input", "Parsed, and Output as %v")
    for _, dateExample := range examples {
        // t, err := dateparse.ParseLocal(dateExample)
        t, err := dateparse.ParseAny(dateExample)
        if err != nil {
            panic(err.Error())
        }
        table.AddRow(dateExample, fmt.Sprintf("%v", t))
    }
    fmt.Println(table.Render())
}

/*
+-------------------------------------------------------+------------------------------------------------------------+
| Input                                                 | Parsed, and Output as %v                                   |
+-------------------------------------------------------+------------------------------------------------------------+
| May 8, 2009 5:57:51 PM                                | 2009-05-08 17:57:51 +0000 UTC                              |
| Nov 8, 2017                                           | 2017-11-08 00:00:00 +0000 UTC                              |
| Mon Jan  2 15:04:05 2006                              | 2006-01-02 15:04:05 +0000 UTC                              |
| Mon Jan  2 15:04:05 MST 2006                          | 2006-01-02 15:04:05 +0000 MST                              |
| Mon Jan 02 15:04:05 -0700 2006                        | 2006-01-02 15:04:05 -0700 -0700                            |
| Monday, 02-Jan-06 15:04:05 MST                        | 2006-01-02 15:04:05 +0000 MST                              |
| Mon, 02 Jan 2006 15:04:05 MST                         | 2006-01-02 15:04:05 +0000 MST                              |
| Tue, 11 Jul 2017 16:28:13 +0200 (CEST)                | 2017-07-11 16:28:13 +0200 +0200                            |
| Mon, 02 Jan 2006 15:04:05 -0700                       | 2006-01-02 15:04:05 -0700 -0700                            |
| Mon Aug 10 15:44:11 UTC+0100 2015                     | 2015-08-10 15:44:11 +0000 UTC                              |
| Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT                              |
| 12 Feb 2006, 19:17                                    | 2006-02-12 19:17:00 +0000 UTC                              |
| 2013-Feb-03                                           | 2013-02-03 00:00:00 +0000 UTC                              |
| 3/31/2014                                             | 2014-03-31 00:00:00 +0000 UTC                              |
| 03/31/2014                                            | 2014-03-31 00:00:00 +0000 UTC                              |
| 08/21/71                                              | 1971-08-21 00:00:00 +0000 UTC                              |
| 8/1/71                                                | 1971-08-01 00:00:00 +0000 UTC                              |
| 4/8/2014 22:05                                        | 2014-04-08 22:05:00 +0000 UTC                              |
| 04/08/2014 22:05                                      | 2014-04-08 22:05:00 +0000 UTC                              |
| 04/2/2014 03:00:51                                    | 2014-04-02 03:00:51 +0000 UTC                              |
| 8/8/1965 12:00:00 AM                                  | 1965-08-08 00:00:00 +0000 UTC                              |
| 8/8/1965 01:00:01 PM                                  | 1965-08-08 13:00:01 +0000 UTC                              |
| 8/8/1965 01:00 PM                                     | 1965-08-08 13:00:00 +0000 UTC                              |
| 8/8/1965 1:00 PM                                      | 1965-08-08 13:00:00 +0000 UTC                              |
| 8/8/1965 12:00 AM                                     | 1965-08-08 00:00:00 +0000 UTC                              |
| 4/02/2014 03:00:51                                    | 2014-04-02 03:00:51 +0000 UTC                              |
| 03/19/2012 10:11:59                                   | 2012-03-19 10:11:59 +0000 UTC                              |
| 03/19/2012 10:11:59.3186369                           | 2012-03-19 10:11:59.3186369 +0000 UTC                      |
| 2014/3/31                                             | 2014-03-31 00:00:00 +0000 UTC                              |
| 2014/03/31                                            | 2014-03-31 00:00:00 +0000 UTC                              |
| 2014/4/8 22:05                                        | 2014-04-08 22:05:00 +0000 UTC                              |
| 2014/04/08 22:05                                      | 2014-04-08 22:05:00 +0000 UTC                              |
| 2014/04/2 03:00:51                                    | 2014-04-02 03:00:51 +0000 UTC                              |
| 2014/4/02 03:00:51                                    | 2014-04-02 03:00:51 +0000 UTC                              |
| 2012/03/19 10:11:59                                   | 2012-03-19 10:11:59 +0000 UTC                              |
| 2012/03/19 10:11:59.3186369                           | 2012-03-19 10:11:59.3186369 +0000 UTC                      |
| 2006-01-02T15:04:05+0000                              | 2006-01-02 15:04:05 +0000 UTC                              |
| 2009-08-12T22:15:09-07:00                             | 2009-08-12 22:15:09 -0700 -0700                            |
| 2009-08-12T22:15:09                                   | 2009-08-12 22:15:09 +0000 UTC                              |
| 2009-08-12T22:15:09Z                                  | 2009-08-12 22:15:09 +0000 UTC                              |
| 2014-04-26 17:24:37.3186369                           | 2014-04-26 17:24:37.3186369 +0000 UTC                      |
| 2012-08-03 18:31:59.257000000                         | 2012-08-03 18:31:59.257 +0000 UTC                          |
| 2014-04-26 17:24:37.123                               | 2014-04-26 17:24:37.123 +0000 UTC                          |
| 2013-04-01 22:43:22                                   | 2013-04-01 22:43:22 +0000 UTC                              |
| 2014-12-16 06:20:00 UTC                               | 2014-12-16 06:20:00 +0000 UTC                              |
| 2014-12-16 06:20:00 GMT                               | 2014-12-16 06:20:00 +0000 UTC                              |
| 2014-04-26 05:24:37 PM                                | 2014-04-26 17:24:37 +0000 UTC                              |
| 2014-04-26 13:13:43 +0800                             | 2014-04-26 13:13:43 +0800 +0800                            |
| 2014-04-26 13:13:44 +09:00                            | 2014-04-26 13:13:44 +0900 +0900                            |
| 2012-08-03 18:31:59.257000000 +0000 UTC               | 2012-08-03 18:31:59.257 +0000 UTC                          |
| 2015-09-30 18:48:56.35272715 +0000 UTC                | 2015-09-30 18:48:56.35272715 +0000 UTC                     |
| 2015-02-18 00:12:00 +0000 GMT                         | 2015-02-18 00:12:00 +0000 UTC                              |
| 2015-02-18 00:12:00 +0000 UTC                         | 2015-02-18 00:12:00 +0000 UTC                              |
| 2017-07-19 03:21:51+00:00                             | 2017-07-19 03:21:51 +0000 UTC                              |
| 2014-04-26                                            | 2014-04-26 00:00:00 +0000 UTC                              |
| 2014-04                                               | 2014-04-01 00:00:00 +0000 UTC                              |
| 2014                                                  | 2014-01-01 00:00:00 +0000 UTC                              |
| 2014-05-11 08:20:13,787                               | 2014-05-11 08:20:13.787 +0000 UTC                          |
| 20140601                                              | 2014-06-01 00:00:00 +0000 UTC                              |
| 1332151919                                            | 2012-03-19 10:11:59 +0000 UTC                              |
| 1384216367189                                         | 2013-11-12 00:32:47.189 +0000 UTC                          |
| 2017年11月09日                                         | 2017-11-09 00:00:00 +0000 UTC                              |
| 2017年11月01日 09:41                                   | 2017-11-01 09:41:00 +0000 UTC                              |
| 1 day ago                                             | 2017-11-08 11:07:30.982877633 +0000 UTC m=-86399.999181941 |
| 19 hours ago                                          | 2017-11-08 16:07:30.982880364 +0000 UTC m=-68399.999179210 |
| 26 minutes ago                                        | 2017-11-09 10:41:30.982884592 +0000 UTC m=-1559.999174982  |
+-------------------------------------------------------+------------------------------------------------------------+
*/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,923评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,154评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,775评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,960评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,976评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,972评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,893评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,709评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,159评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,400评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,552评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,265评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,876评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,528评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,701评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,552评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,451评论 2 352

推荐阅读更多精彩内容