golang 时间格式化

栏目: Go · 发布时间: 5年前

内容简介:golang 时间格式化要指定默认的格式,如果不指定默认的格式就会出现错误,如果一个未知的时间格式需要做格式化,是比较棘手的。下面是我在项目写的一个时间格式化函数,简单的支持几种格式测试

golang 时间格式化要指定默认的格式,如果不指定默认的格式就会出现错误,如果一个未知的时间格式需要做格式化,是比较棘手的。

下面是我在项目写的一个时间格式化函数,简单的支持几种格式

package util

import (
    "strings"
    "time"
)

func TimeParseLocal(in string) (out time.Time, err error) {
    birStrLen := strings.Count(in, "") - 1
    switch birStrLen {
    case 10:
        //2006-01-02
        return TimeParse10(in)
    case 19:
        //2006-01-02 15:04:05
        return TimeParse19(in)
    default:
    }
    return
}

// yyyy-mm-dd | mm-dd-yyyy 格式
func TimeParse10(in string) (out time.Time, err error) {
    chars := []rune(in)
    fc := ""
    for k, char := range chars {
        if char == '-' || char == '/' || char == ':' || char == '.' {
            fc = string(char)
        }
        switch {
        case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))):
            // 这个是  Jun-MM-YYYY 格式
        case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'):
            return TimeParseMMDDYYYY(in, string(char))
        case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'):
            return TimeParseYYYYMMDD(in, string(char))
        }
    }
    return TimeParseYYYYMMDD(in, fc)
}

// yyyy-mm-dd hh:mm:ss | mm-dd-yyyy hh:mm:ss 格式
func TimeParse19(in string) (out time.Time, err error) {
    chars := []rune(in)
    fc := ""
    for k, char := range chars {
        if char == '-' || char == '/' || char == ':' || char == '.' {
            fc = string(char)
        }
        switch {
        case (k == 0) && ((char <= ('Z') && char >= ('A')) || (char <= ('z') && char >= ('a'))):
            // 这个是  Jun-MM-YYYY 格式
        case (k == 2) && (char == '-' || char == '/' || char == ':' || char == '.'):
            return TimeParseMMDDYYYYHHMMSS(in, string(char))
        case (k == 4) && (char == '-' || char == '/' || char == ':' || char == '.'):
            return TimeParseYYYYMMDDHHMMSS(in, string(char))
        }
    }
    return TimeParseYYYYMMDDHHMMSS(in, fc)
}

func TimeParseYYYYMMDD(in string, sub string) (out time.Time, err error) {
    layout := "2006" + sub + "01" + sub + "02"
    out, err = time.ParseInLocation(layout, in, time.Local)
    if err != nil {
        return
    }
    return
}

func TimeParseMMDDYYYY(in string, sub string) (out time.Time, err error) {
    layout := "01" + sub + "02" + sub + "2006"
    out, err = time.ParseInLocation(layout, in, time.Local)
    if err != nil {
        return
    }
    return
}

func TimeParseYYYYMMDDHHMMSS(in string, sub string) (out time.Time, err error) {
    layout := "2006" + sub + "01" + sub + "02 15:04:05"
    out, err = time.ParseInLocation(layout, in, time.Local)
    if err != nil {
        return
    }
    return
}

func TimeParseMMDDYYYYHHMMSS(in string, sub string) (out time.Time, err error) {
    layout := "01" + sub + "02" + sub + "2006 15:04:05"
    out, err = time.ParseInLocation(layout, in, time.Local)
    if err != nil {
        return
    }
    return
}

测试

package util

import (
    "testing"
)

func TestTimeParseLocal(t *testing.T) {
    t.Log(TimeParseLocal("2019/03/26"))
    t.Log(TimeParseLocal("03/26/2019"))
    t.Log(TimeParseLocal("2019/03/26 17:30:03"))
    t.Log(TimeParseLocal("03/26/2019 17:30:03"))
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming Collective Intelligence

Programming Collective Intelligence

Toby Segaran / O'Reilly Media / 2007-8-26 / USD 39.99

Want to tap the power behind search rankings, product recommendations, social bookmarking, and online matchmaking? This fascinating book demonstrates how you can build Web 2.0 applications to mine the......一起来看看 《Programming Collective Intelligence》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具