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

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 .

panic: parsing time "10/15/1983" as "10/15/1983": cannot parse "" as "0/"

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

其他语言的功能与以下类似:

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

我在Go文档中找不到这样的函数,我唯一的 Select 是regex吗?

推荐答案

现在有一些关键的价值观.Parse正在寻找.

通过更改:

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

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

解析器会识别它.

这是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 至 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 至gether any standard format you'd need 至 parse.

For example, if you want 至 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 至 time.Parse() as the layout argument.

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

Go相关问答推荐

如何复制*C.char?

如何在jsonrpc服务器的服务器端捕获错误?

将类型定义为泛型类型实例化

租户GUID X的租户不存在self 邮箱帐户的租户(我是唯一的成员)

在整个SQL事务中将使用上下文作为默认设置吗?

如何根据中间件的请求设置上下文值?获取 go-staticcheck 问题

Golang Gorm Fiber - 如何将定义为别名的名称发送到索引模板?

htmx 表单 + gin 无法正确读取请求正文

使用Go和Operator SDK通过API调用设置Kubernetes Pods的安装步骤

无法使用带有 422 的 go-github 创建提交 - 更新不是快进

在 .go 文件中运行一个函数和在 Go 模板中调用它有什么区别?

Apache Beam 在 Go 中从 PCollection 中 Select 前 N 行

获取不带类型参数的泛型 struct 的类型名称

如何使用带有方法的字符串枚举作为通用参数?

如何为导入的嵌入式 struct 文字提供值?

Golang Getrlimit 返回与 ulimit 不同的值

当有多个同名包时如何在vscode中显示golang包完整路径?

如何使用 math/big 对 bigInt 进行取模?

httprouterhttp.HandlerFunc() 是如何工作的?

Go 导入范围查找 protobuf 类型