解析奇怪的日期格式

huangapple go评论91阅读模式
英文:

parse strange date format

问题

我正在开发一个解析器,用于解析游戏的日志文件,以便我可以对游戏内的拍卖进行分析。然而,记录器写入的日期格式似乎导致了问题,因为该格式似乎是为记录器定制的。一个示例的日期时间戳如下所示:[Wed Nov 23 23:26:10 2016]。我尝试使用以下代码进行解析:

func (r *AuctionReader) extractSaleInformation(line string) {
    fmt.Println("Extracting information from: ", line)

    // 输出格式掩码
    layout := "DD-MM-YYYY hh:mm:ss"

    // 替换方括号,只保留日期时间字符串
    date := strings.TrimSpace(strings.Replace((strings.Split(line, "]")[0]), "[", "", -1))

    fmt.Println(time.Parse(date, layout))
}

当我尝试解析上述日期时间字符串时,我得到以下错误:

0001-01-01 00:00:00 +0000 UTC parsing time "DD-MM-YYYY hh:mm:ss" as "Wed Nov 23 23:26:10 2016": cannot parse "DD-MM-YYYY hh:mm:ss" as "Wed Nov "

我该如何让解析器识别这个似乎是自定义格式的日期时间?我将把这些数据保存到Mongo数据库中,因此不希望将拍卖时间保存为字符串,而是希望能够单独查询时间戳。

英文:

I'm working on a Parser which Parses log files from a game so I can do analysis on auctions made within the game, however the date format that's being written by the logger seems to be causing problems as the format seems to be custom written for the logger, an example datetime stamp looks like: [Wed Nov 23 23:26:10 2016] I try to Parse it with:

func (r *AuctionReader) extractSaleInformation(line string) {
    fmt.Println("Extracting information from: ", line)

    // Format mask for output
    layout := "DD-MM-YYYY hh:mm:ss"

    // Replace the square brackets so we're just left with the date-time string
    date := strings.TrimSpace(strings.Replace((strings.Split(line, "]")[0]), "[", "", -1))

    fmt.Println(time.Parse(date, layout))
}

When I attempt to Parse the above date-time string I get the following error:

> 0001-01-01 00:00:00 +0000 UTC parsing time "DD-MM-YYYY hh:mm:ss" as "Wed Nov 23 23:26:10 2016": cannot parse "DD-MM-YYYY hh:mm:ss" as "Wed Nov "

How am I able to get the parser to recognise this seemingly custom format, I will be saving this data to Mongo so I don't want to store the auction time as a string as I want to query the timestamps individually.

答案1

得分: 14

Golang以独特的方式处理所有日期格式化-它使用参考时间Mon Jan 2 15:04:05 MST 2006(01/02 03:04:05PM '06 -0700)来显示格式化/解析给定时间/字符串的模式。

因此,要读取格式为“Wed Nov 23 23:26:10 2016”的日期,您需要将参考日期放入该格式中:“Mon Jan 2 15:04:05 2006”,然后执行以下操作:

t, _ := time.Parse("Mon Jan 2 15:04:05 2006", "Wed Nov 23 23:26:10 2016")

然后,如果您想以给定格式输出它,例如DD-MM-YYYY hh:mm:ss,您需要将参考时间放入该格式中:02-01-2006 15:04:05,然后执行以下操作:

t.Format("02-01-2006 15:04:05")

https://play.golang.org/p/VO5413Z7-z

因此,基本上,主要更改是

// 输出的格式掩码
layout := "DD-MM-YYYY hh:mm:ss"

应改为

// 输出的格式掩码
layout := "02-01-2006 15:04:05"

time.Parse(date, layout)

应改为

time.Parse(layout, date)
英文:

Golang handle all date formatting in a unique way - it uses the reference time Mon Jan 2 15:04:05 MST 2006 (01/02 03:04:05PM '06 -0700) to show the pattern with which to format/parse a given time/string.

So, to read the format "Wed Nov 23 23:26:10 2016" you would put the reference date into that format: "Mon Jan 2 15:04:05 2006", and then do:

t, _ := time.Parse("Mon Jan 2 15:04:05 2006", "Wed Nov 23 23:26:10 2016")

Then, to output it in the given format, if you wanted the format DD-MM-YYYY hh:mm:ss, you would put the reference time into that format: 02-01-2006 15:04:05, and then do:

t.Format("02-01-2006 15:04:05")

https://play.golang.org/p/VO5413Z7-z

So basically, the main change is

// Format mask for output
layout := "DD-MM-YYYY hh:mm:ss"

should be

// Format mask for output
layout := "02-01-2006 15:04:05"

and

time.Parse(date, layout)

should be

time.Parse(layout, date)

huangapple
  • 本文由 发表于 2016年12月3日 03:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/40939261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定