理解Go中的time.Parse函数

问题描述 投票:0回答:1

我目前正在移植代码从转到c#并遇到了这个(简化的)代码。我知道它使用给定格式171228175744.085转换给定的字符串060102150405

官方文档只有使用2017-Feb-1等常见格式的示例,而不是这种格式(可能的时间戳?)

我知道这将导致时间变成2017-12-28 17:57:44.085 +0000 UTC,但我不知道如何,因为我没有信息字符串171228175744.085和布局代表什么。我知道其中一些信息与GPS有关。所以,我的问题是:有谁知道如何在c#中做到这一点?

package main

import (
    "fmt"
    "time"
)

func main() {
    t, err := time.Parse("060102150405", "171228175744.085")
    if err == nil{
        fmt.Println(t)
    }

}
c# go time
1个回答
3
投票

围绕time.Format的文档解释了格式的含义。

引用:

Format returns a textual representation of the time value formatted
according to layout, which defines the format by showing how the 
reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

示例中的格式字符串:060102150405告诉时间解析器查找以下内容:

  • 06:年
  • 01:月
  • 02:每月的某一天
  • 15:一天中的小时
  • 04:分钟
  • 05:第二

这是告诉解析器如何解释每个数字的便捷方式。如果仔细观察,你会发现数字没有被重用,所以当你说06时,解析器会将它与2006匹配。

在C#中,您可以使用datetime.ParseExact。有点像:

DateTime.ParseExact(dateString, "yyMMddhhmmss", some_provider);

(注意:我没有尝试过上面的C#片段。你可能需要调整它)

© www.soinside.com 2019 - 2024. All rights reserved.