且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

去 - 解析不是'标准'格式的日期/时间字符串

更新时间:2023-11-29 11:10:34

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



通过更改:

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



$ p $ to b
$ b $ pre $ test,err: = time.Parse(01/02/2006,10/15/1983)

解析器会识别它。



以下是修改后的代码

a>。
 包主
$ b $导入fmt
导入时间

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 文件创建您自己的解析格式。

  const(
stdLongMonth =January
stdMonth =Jan
stdNumMonth =1
stdZeroMonth =01
stdLongWeekDay =星期一
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//输出Z为UTC
stdISO8601ColonTZ =Z07:00//输出Z为UTC
stdNumTZ =-0700//总是数字
stdNumShortTZ =-07 //总是数字
stdNumColonTZ =-07:00 //总是数字

因此,无论何时您的格式指定一年,都应该完成在06或2006中,秒由05或5指定,时区在MST,Z0700,Z07:00,-0700,-07或-07:00。如果你引用常量列表,你可能会把你需要解析的标准格式放在一起。



例如,如果你想解析的日期/时间通用日志格式是Apache用于其日志文件的格式,您可以通过将以下字符串传递给 time.Parse()作为布局参数。

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

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



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



因此 time.Parse()调用会看起来像这样:

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


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)
}

This results in a panic.

panic: parsing time "10/15/1983" as "10/15/1983": cannot parse "" as "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")

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

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

By changing:

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

to

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

the parser will recognize it.

Here's the modified code on the playground.

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)
}


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
)

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.

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" 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.

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

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")