如何在golang中使用.Format将time.Time对象转换为格式化字符串?

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

How to convert time.Time object to formatted string using .Format in golang?

问题

我目前正在尝试从我的SQL数据库中获取一个time.Time对象,并将检索到的值转换为格式化的字符串,格式如下:

TIME_FORMAT := "%Y-%m-%dT%H:%M:%S"

这是我在Python中使用相同方法的格式,但我知道它在Go语言中不正确。我已经从数据库中获取了值,现在只需要格式化它。请注意,我将ccc.Ia_date定义为interface{}类型,因为数据库中的这个值可能为空。这是我的代码片段:

fmt.Println(reflect.TypeOf(ccc.Ia_date))  //输出time.Time
t := ccc.Ia_date //打印结果为:2016-11-16 21:06:39 +0000 +0000
fmt.Println(t.Format())

然后我得到以下错误:

t.Format未定义(类型interface {}是没有方法的接口)

我是否漏掉了某个导入?或者使用接口类型是否不可能?如果不可能,我有哪些替代方法来接受来自数据库的空值?
(我已经看到了相关问题https://stackoverflow.com/questions/33119748/golang-convert-time-time-to-string的答案-我尝试了那个解决方案,但我没有使用time.Now,这是我看到的唯一区别)

英文:

I'm currently attempting to fetch a time.Time object from my SQL database and convert the retrieved value to a formatted string that looks like so:

TIME_FORMAT := "%Y-%m-%dT%H:%M:%S"

This is the format I have used to do the same thing in Python, but I know it is not correct for go. I have already grabbed the value from the DB, and now just need to format it. Please note I have defined ccc.Ia_date as interface{} type because it is possible that this value in the DB could be null. Here is a clip of my code:

fmt.Println(reflect.TypeOf(ccc.Ia_date))  //gives me time.Time
t := ccc.Ia_date //which prints as: 2016-11-16 21:06:39 +0000 +0000
fmt.Println(t.Format())

and I get the following error:

t.Format undefined (type interface {} is interface with no methods)

Am I missing an import? Or is this just not possible using interface types? If so, what alternatives do I have to accept null values from the DB?
(I have already seen the related answer to https://stackoverflow.com/questions/33119748/golang-convert-time-time-to-string - I tried that solution but I'm not grabbing time.Now and that's the only difference I see)

答案1

得分: 1

如果你有一个持有time.time值的interface{}类型的值,你可以使用type assertion来获取包装的time.Time值:

if t, ok := ccc.Ia_date.(time.Time); ok {
    // 这里的 t 是 time.Time 类型,所以你可以调用它的 Format() 方法:
    fmt.Println(t.Format(layout))
} else {
    // ccc.Ia_date 不是 time.Time 类型,或者它是 nil
}

请注意,如果你想允许nil,那么使用指针*time.Time会更容易,这样你就不需要使用类型断言。查看这个答案以获取更多细节:https://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field/32646035#32646035

如果 ccc.Ia_date*time.Time 类型:

if ccc.Ia_date != nil {
    fmt.Println(ccc.Ia_date.Format(layout))
}

你所期望的格式的布局字符串是:

layout := "2006-01-02T15:04:05"
英文:

If you have an interface{} value holding a time.time value, you may use type assertion to obtain the wrapped time.Time:

if t, ok := ccc.Ia_date.(time.Time); ok {
    // here t is of type time.Time, and so you can call its Format() method:
    fmt.Println(t.Format(layout))
} else {
    // ccc.Ia_date is not of type time.Time, or it is nil
}

Note that it would be easier to just use a pointer *time.Time if you want to allow nil, and so you don't need to use type assertion. See this answer for more details: https://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field/32646035#32646035

If ccc.Ia_date would be of type *time.Time:

if ccc.Ia_date != nil {
    fmt.Println(ccc.Ia_date.Format(layout))
}

And the layout string for your desired format is:

layout := "2006-01-02T15:04:05"

答案2

得分: 0

你有多个问题。首先,如果ccc.Ia_date是一个interface{},在使用time.Time的任何方法之前,你必须将其类型断言为time.Time。你可以使用两个参数的类型断言形式来避免发生恐慌:

t, ok := ccc.Ia_date.(time.Time)
if !ok {
    // 错误的类型或空值,进行处理
}
t.Format(timeFormat)

你还需要定义你的格式字符串。格式字符串被定义为标准化的日期和时间,即Mon Jan 2 15:04:05 -0700 MST 2006。所以对于你的格式,你可以使用:

timeFormat := "2006-01-02T15:04:05"
t.Format(timeFormat)

然而,这个格式缺少时区信息。你也可以使用几乎相同的RFC3339格式("2006-01-02T15:04:05Z07:00"):

t.Format(time.RFC3339)
英文:

You have multiple issues. First off, if ccc.Ia_date is an interface{}, you must type-assert it to a time.Time before you can use any methods of a time.Time. You can do this using the two-argument type-assertion form to avoid panics:

t, ok := ccc.Ia_date.(time.Time)
if !ok {
    // wrong type or nil value, handle it
}
t.Format(timeFormat)

You also need to define your format string. The format string is defined as a standardized date and time, that being Mon Jan 2 15:04:05 -0700 MST 2006. So for your format, you want:

time_format := "2006-01-02T15:04:05"
t.Format(timeFormat)

However, this lacks a timezone. It is also nearly identical to RFC3339 ("2006-01-02T15:04:05Z07:00"), which you can use instead:

t.Format(time.RFC3339)

huangapple
  • 本文由 发表于 2016年11月22日 00:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/40724954.html
匿名

发表评论

匿名网友

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

确定