如何在golang中转换ISO 8601时间?

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

How to convert ISO 8601 time in golang?

问题

以下是相应的Golang代码,用于执行与shell命令date -u +%Y-%m-%dT%T%z等效的操作:

package main

import (
	"fmt"
	"os/exec"
	"strings"
	"time"
)

func main() {
	cmd := exec.Command("date", "-u", "+%Y-%m-%dT%T%z")
	output, err := cmd.Output()
	if err != nil {
		fmt.Println("命令执行出错:", err)
		return
	}

	dateString := strings.TrimSpace(string(output))
	date, err := time.Parse("2006-01-02T15:04:05-0700", dateString)
	if err != nil {
		fmt.Println("日期解析出错:", err)
		return
	}

	fmt.Println("等效的日期时间:", date)
}

请注意,此代码使用os/exec包来执行shell命令,并使用time包来解析日期字符串。最后,它将等效的日期时间打印到控制台上。

英文:

What is the equivalent code in golang for the following shell command ?
date -u +%Y-%m-%dT%T%z

答案1

得分: 251

如果你正在寻找一个简单但不完美的解决方案,可以考虑使用time.RFC3339常量。但是要知道,ISO8601之间存在复杂的差异,这超出了本回答的范围。

请参考https://ijmacd.github.io/rfc3339-iso8601/了解差异,并且该网站还提供了一个方便的测试文件生成器来展示差异。在这里https://stackoverflow.com/questions/522251/whats-the-difference-between-iso-8601-and-rfc-3339-date-formats 也有一个很好的讨论。

package main

import (
	"time"
	"fmt"
)

func main(){
	fmt.Println(time.Now().Format(time.RFC3339))
}

golang Time.Format

英文:

If you're looking for a simple, but not perfect solution consider using time.RFC3339 constant. But also know that there are differences between ISO8601 which are too complex for this answer.

See https://ijmacd.github.io/rfc3339-iso8601/ for differences and also has a handy test file generator to show differences. There is also a good discussion on SO here https://stackoverflow.com/questions/522251/whats-the-difference-between-iso-8601-and-rfc-3339-date-formats

package main

import (
	"time"
	"fmt"
)

func main(){
	fmt.Println(time.Now().Format(time.RFC3339))
}

golang Time.Format

答案2

得分: 53

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}
package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}
package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}
英文:
package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}

答案3

得分: 11

我有以下规范:

YYYY-MM-DDThh:mm:ss.sssZ

在示例中,最后的 Z 是明确存在的。

这是我处理它的方式:

  • 首先,我找到与我的目标最接近的 time.RFCxxx
  • 我复制了它的值
  • 我调整它,直到找到了预期的结果

预期结果是

2006-01-02T15:04:05.999Z
英文:

I had the following spec:

YYYY-MM-DDThh:mm:ss.sssZ

with the final Z being explicitly present in the examples.

Here's how I dealt with it:

  • first I found the time.RFCxxx that was the closest to my target
  • I copied its value
  • I fiddled with it until I found the expected result

which is

2006-01-02T15:04:05.999Z

答案4

得分: 7

ISO8601允许使用不同的粒度级别。你可以只有年份,年份+月份,年份+月份+日期,再加上时间部分,并可选择包含时区部分。然而,Go语言内置的时间解析要求你事先知道哪些部分会被包含进来。

github.com/btubbs/datetime库提供了一个更灵活的解析器,可以处理所有常用的ISO8601格式。请参考https://github.com/btubbs/datetime

声明:我编写了该库。

英文:

ISO8601 allows for variable levels of granularity. You can have just a year, year+month, year+month+day, add a time portion, and optionally have a timezone portion. Go's built-in time parsing, however, requires you to know ahead-of-time which parts will be included.

The github.com/btubbs/datetime library provides a more flexible parser that can handle all the commonly used ISO8601 formats. See https://github.com/btubbs/datetime

Disclosure: I wrote that library.

答案5

得分: 6

将格式中的符号替换为Z会触发ISO 8601行为,即time.RFC3339。如果您希望字符串输出以'Z'结尾,您需要将其转换为UTC时区。

package main    
import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05Z07:00"))
}
// 这是RFC3339使用的相同格式。只是一个关于为什么的注释。
英文:

Replacing the sign in the format with a Z triggers the ISO 8601 behavior. Which is exactly time.RFC3339. If you are wanting the string output to end in 'Z' what you need to do is convert to the UTC zone.

package main    
import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05Z07:00"))
}
// this is the same format used by RFC3339. just a note on why. 

huangapple
  • 本文由 发表于 2016年2月18日 18:36:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/35479041.html
匿名

发表评论

匿名网友

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

确定