How to use time.Parse to parse hh:mm format in Go

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

How to use time.Parse to parse hh:mm format in Go

问题

我正在导入一系列格式为:

09:02 AM
10:02 AM
12:30 PM
04:10 PM
04:50 PM
05:30 PM

我想将这些字段转换为可以进行算术运算的形式。例如,倒计时到事件发生的时间。因此,将字段保存为微秒...甚至秒。

我一直在尝试让time.Parse工作...但是没有成功。

fmt.Println(time.Parse("hh:mm", m.Feed.Entry[i].GsxA100Time.T))

返回...

>0001-01-01 00:00:00 +0000 UTC 解析时间"07:50 PM"为"hh:mm"时出错:无法解析"07:50 PM"为"hh:mm"

有什么建议吗?

英文:

I am importing a lot of fields of the format:

09:02 AM
10:02 AM
12:30 PM
04:10 PM
04:50 PM
05:30 PM

I would like to convert the fields into something I can do arithmetic on. For example, do a count down to when the event Occurs. Thus, saving the field in microseconds... or even seconds.
I have been trying to get the time.Parse to work... no joy.

fmt.Println(time.Parse("hh:mm", m.Feed.Entry[i].GsxA100Time.T))

returns...

>0001-01-01 00:00:00 +0000 UTC parsing time "07:50 PM" as "hh:mm": cannot parse "07:50 PM" as "hh:mm"

any suggestions?

答案1

得分: 8

time.Parse的布局字符串不能处理"hh:mm"格式。在你的情况下,布局字符串应该是"03:04 PM",你可以在文档中看到。

要在解析字符串后获得time.Duration,你可以将你的时间与参考时间相减,对于你的情况,我假设参考时间是"12:00 AM"

工作示例:

package main

import (
	"fmt"
	"time"
)

func main() {
	ref, _ := time.Parse("03:04 PM", "12:00 AM")
	t, err := time.Parse("03:04 PM", "11:22 PM")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(t.Sub(ref).Seconds())
}

输出:

84120

Playground

英文:

The layout string for time.Parse does not handle the "hh:mm" format. In your case, the layout string would rather be "03:04 PM" as you can see in the documentation.

To get a time.Duration after parsing the string, you can substract your time with a reference time, in your case I would assume "12:00 AM".

Working example:

package main

import (
	"fmt"
	"time"
)

func main() {
	ref, _ := time.Parse("03:04 PM", "12:00 AM")
	t, err := time.Parse("03:04 PM", "11:22 PM")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(t.Sub(ref).Seconds())
}

Output:
> 84120

Playground

答案2

得分: 2

你是否阅读了time.Parse的文档?在文档的开头部分有这样的说明:

> 布局通过展示参考时间的格式来定义,参考时间被定义为Mon Jan 2 15:04:05 -0700 MST 2006,如果它是该值,将如何解释。

在包文档的开头部分还有关于布局字符串的更多详细信息。请注意,你可以省略一些字段(在你的情况下是天、年和时区),这些字段将始终获得零值。

因此,fmt.Println(time.Parse("3:04 PM", "07:50 PM")) 应该可以工作。

英文:

Did you read the documentation of time.Parse? In the very beginning it says:

> The layout defines the format by showing how the reference time,
> defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted
> if it were the value

In the beginning of the package documentation there are more details about the layout string. Note that you can omit some of the fields (in your case days, years and timezeone) and those will then always get zero value.

So fmt.Println(time.Parse("3:04 PM", "07:50 PM")) should work.

答案3

得分: 0

如果你再次查看有关时间的Parse方法的文档,你会发现Go语言解析时间的方法与你在其他编程语言中使用的方法完全不同。为了向Parse函数提供你想要解析的时间的布局,你需要将一个特定的日期(即Mon Jan 2 15:04:05 -0700 MST 2006)转换为你想要的布局。在你的情况下,应该是"03:04 PM"。

完整的代码:fmt.Println(time.Parse("03:04 PM", m.Feed.Entry[i].GsxA100Time.T))

英文:

If you'll check again the documentation about time's Parse method you'll find the fact that Go's approach of parsing the time is totally different than the one that you used to work with in other programming languages. To provide the Parse function the layout of the time that you want to parse you need to transform one specific date (which is Mon Jan 2 15:04:05 -0700 MST 2006) into the layout that you're aiming for. In your case that would be "03:04 PM".

The full code: fmt.Println(time.Parse("03:04 PM", m.Feed.Entry[i].GsxA100Time.T))

答案4

得分: -1

如果有人正在使用纪元时间,可以使用以下代码:

func convert(in int64) string {
    u := time.Unix(in/1000, in%1000)
    ampm := "上午"
    if u.Hour()/12 == 1 {
        ampm = "下午"
    }
    th := u.Hour() % 12
    hh := strconv.Itoa(th)
    if th < 10 {
        hh = "0" + hh
    }
    tm := u.Minute()
    mm := strconv.Itoa(tm)
    if tm < 10 {
        mm = "0" + mm
    }
    return hh + ":" + mm + " " + ampm
}

这段代码可以将纪元时间转换为小时和分钟,并添加上午或下午的标识。

英文:

Just in case anyone is working with epoch times can use this,

func convert (in int64) string {
    u := time.Unix(in/1000, in%1000)
    ampm := &quot;AM&quot;
    if u.Hour()/12 ==1 { ampm = &quot;PM&quot; }
	th := u.Hour()%12
    hh := strconv.Itoa(th)
	if th &lt; 10 { hh = &quot;0&quot; + hh }
    tm := u.Minute()
	mm := strconv.Itoa(tm)
    if tm &lt; 10 { mm = &quot;0&quot; + mm }
	return hh + &quot;:&quot; + mm + &quot; &quot; + ampm
}

huangapple
  • 本文由 发表于 2017年7月5日 19:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/44924628.html
匿名

发表评论

匿名网友

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

确定