在Go语言中类似于Python的timedelta的东西

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

Something like python timedelta in golang

问题

package main

import (
"fmt"
"time"
)

var base = time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC)

func main() {
weeks := 1722
days := 1
seconds := 66355
weeksToSecs := 7 * 24 * 60 * 60
daysToSecs := 24 * 60 * 60
totalSecs := (weeks * weeksToSecs) + (days * daysToSecs) + seconds
duration := time.Duration(totalSecs) * time.Second

date := base.Add(duration)

fmt.Printf("Result: %s", date)

}

英文:

I want to get a datetime, counting weeks from a date, days from a week and seconds from 00:00 time.

With Python I can use this:

BASE_TIME = datetime.datetime(1980,1,6,0,0)
tdelta = datetime.timedelta(weeks = 1722,
                            days = 1,
                            seconds = 66355)
mydate = BASE_DATE + tdelta

I'm trying to get it with Go, but I have some problems to reach it:

package main

import (
	"fmt"
	"time"
)

var base = time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC)

func main() {
	weeks := 1722
	days := 1
	seconds := 66355
	weeksToSecs := 7 * 24 * 60 * 60
	daysToSecs := 24 * 60 * 60
	totalSecs := (weeks * weeksToSecs) + (days * daysToSecs) + seconds
	nanosecs := int64(totalSecs) * 1000000000

	//delta := time.Date(0, 0, 0, 0, 0, totalSecs, 0, time.UTC)
	
	date := base.Add(nanosecs)

	fmt.Printf("Result: %s", date)

}

prog.go:21: cannot use nanosecs (type int64) as type time.Duration in function argument

http://play.golang.org/p/XWSK_QaXrQ

What I'm missing?<br>Thanks

答案1

得分: 23

package main

import (
"fmt"
"time"
)

func main() {
baseTime := time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC)
date := baseTime.Add(1722724time.Hour + 24time.Hour + 66355*time.Second)
fmt.Println(date)
}

英文:
package main

import (
        &quot;fmt&quot;
        &quot;time&quot;
)

func main() {
        baseTime := time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC)
        date := baseTime.Add(1722*7*24*time.Hour + 24*time.Hour + 66355*time.Second)
        fmt.Println(date)
}

Playground


Output

2013-01-07 18:25:55 +0000 UTC

答案2

得分: 5

jnml的答案有效且更符合go的习惯用法。但是为了说明为什么你的原始代码不起作用,你只需要改变一行。

date := base.Add(time.Duration(nanosecs)) 将nanosecs转换为time.Duration类型,这是Add方法所期望的类型,而不是int64类型。Go不会自动为你转换类型,所以它会报错类型为int64。

英文:

jnml's answer works and is more idiomatic go. But to illustrate why your original code didn't work, all you have to do is change one line.

date := base.Add(time.Duration(nanosecs)) will cast the nanosecs to a time.Duration which is the type that Add expects as opposed to int64. Go will not automatically cast a type for you so it complained about the type being int64.

答案3

得分: 2

timeutil 支持 timedelta 和 strftime。

package main

import (
    "fmt"
    "time"

    "github.com/leekchan/timeutil"
)

func main() {
    base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
    td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}

    result := base.Add(td.Duration())
    fmt.Println(result) // "2015-02-13 00:17:56 +0000 UTC"
}
英文:

timeutil supports timedelta and strftime.

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;

    &quot;github.com/leekchan/timeutil&quot;
)

func main() {
    base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
    td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}

    result := base.Add(td.Duration())
    fmt.Println(result) // &quot;2015-02-13 00:17:56 +0000 UTC&quot;
}

huangapple
  • 本文由 发表于 2013年7月14日 00:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/17631900.html
匿名

发表评论

匿名网友

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

确定