我正在开发一个解析器,用于解析游戏日志文件,以便我可以对游戏内的拍卖进行分析。然而,记录器编写的日期格式似乎会导致问题,因为该格式似乎是专门为记录器编写的。例如,时间戳看起来像这样:[Wed Nov 23 23:26:10 2016]
我尝试使用以下代码进行解析:
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))
}
当我尝试解析上面的日期时间字符串时,我会得到以下错误:
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中,因此不希望将拍卖时间存储为字符串。
time.Parse
中的date
和layout
参数。 - Gavin