解析不是“标准"格式的日期/时间字符串 [英] Parsing date/time strings which are not 'standard' formats

查看:60
本文介绍了解析不是“标准"格式的日期/时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Go 中解析非标准日期/时间字符串.例如,如果我想将字符串 10/15/1983 转换为 time.Time?time.Parse() 函数应该允许您指定格式.

How do I parse non-standard date/time strings in Go. In example if I wanted to convert the string 10/15/1983 into a time.Time? The time.Parse() function supposedly allows you to specify a format.

http://play.golang.org/p/v5DbowXt1x

package main

import "fmt"
import "time"

func main() {
    test, err := time.Parse("10/15/1983", "10/15/1983")
    if err != nil {
        panic(err)
    }

    fmt.Println(test)
}

这会导致恐慌.

panic:将时间10/15/1983"解析为10/15/1983":无法将"解析为0/"

从逻辑上讲,这是有道理的,因为它应该如何知道哪一天是哪月.

Logically that makes sense because how is it supposed to know which is the day and which is the month.

其他语言的功能类似如下:

Other languages have a function similar to the following:

parse("mm/dd/yyyy", "10/15/1983")

我在 Go 文档中找不到这样的函数,是我唯一的正则表达式选择吗?

I cannot find such a function in the Go docs, is my only choice to regex?

推荐答案

time.Parse 正在寻找一些关键值.

There are some key values that the time.Parse is looking for.

通过改变:

test, err := time.Parse("10/15/1983", "10/15/1983")

test, err := time.Parse("01/02/2006", "10/15/1983")

解析器会识别它.

这是操场上的修改代码.

package main

import "fmt"
import "time"

func main() {
    test, err := time.Parse("01/02/2006", "10/15/1983")
    if err != nil {
        panic(err)
    }

    fmt.Println(test)
}


您可以利用 src/pkg/time/format.go 文件中的常量列表来创建您自己的解析格式.

You can utilize the constants list in the src/pkg/time/format.go file to create your own parse formats.

const (
    stdLongMonth      = "January"
    stdMonth          = "Jan"
    stdNumMonth       = "1"
    stdZeroMonth      = "01"
    stdLongWeekDay    = "Monday"
    stdWeekDay        = "Mon"
    stdDay            = "2"
    stdUnderDay       = "_2"
    stdZeroDay        = "02"
    stdHour           = "15"
    stdHour12         = "3"
    stdZeroHour12     = "03"
    stdMinute         = "4"
    stdZeroMinute     = "04"
    stdSecond         = "5"
    stdZeroSecond     = "05"
    stdLongYear       = "2006"
    stdYear           = "06"
    stdPM             = "PM"
    stdpm             = "pm"
    stdTZ             = "MST"
    stdISO8601TZ      = "Z0700"  // prints Z for UTC
    stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
    stdNumTZ          = "-0700"  // always numeric
    stdNumShortTZ     = "-07"    // always numeric
    stdNumColonTZ     = "-07:00" // always numeric
)

因此,无论何时您的格式指定年份,都应使用06"或2006"完成,秒由05"或5"指定,时区指定为MST"、Z0700"、"Z07:00"、-0700"、-07"或-07:00".如果您参考常量列表,您很可能会将您需要解析的任何标准格式放在一起.

So anytime your format specifies a year, it should be done with "06" or "2006", seconds are specified by "05" or "5" and time zones are specified at "MST", "Z0700", "Z07:00", "-0700", "-07" or "-07:00". If you reference the constants list you can likely put together any standard format you'd need to parse.

例如,如果您想解析通用日志格式中的日期/时间,Apache使用的格式它的日志文件,您可以通过将以下字符串作为 layout 参数传递给 time.Parse() 来实现.

For example, if you want to parse the date/time in the Common Log Format, the format Apache uses for its log files, you would do so by passing the following string to time.Parse() as the layout argument.

"02/Jan/2006:15:04:05 -0700"

02"表示月份字段,Jan"表示月份名称字段,2006"表示年份字段,15"表示24小时格式的小时字段,04"表示分钟字段,05"表示秒字段,-0700"表示时区字段.

"02" denotes the day of the month field, "Jan" denotes the month name field, "2006" denotes the year field, "15" denotes the hour of day field in 24 hour format, "04" denotes the minutes field, "05" denotes the seconds field and "-0700" denotes the time zone field.

该格式将解析当前 PST 时间:31/Dec/2012:15:32:25 -0800

That format would parse the current PST time: 31/Dec/2012:15:32:25 -0800

所以 time.Parse() 调用看起来像这样:

So the time.Parse() call would look like this:

test, err := time.Parse("02/Jan/2006:15:04:05 -0700", "31/Dec/2012:15:32:25 -0800")

这篇关于解析不是“标准"格式的日期/时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆