Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something like python timedelta in golang

Tags:

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?
Thanks

like image 688
chespinoza Avatar asked Jul 13 '13 16:07

chespinoza


People also ask

Is Python a Timedelta?

Timedelta in Python is an object that represents the duration. It is mainly used to calculate the duration between two dates and times. It is also used for retrieving the object with some delta date and time.

Can Timedelta be negative Python?

Timedeltas are differences in times, expressed in difference units, e.g. days, hours, minutes, seconds. They can be both positive and negative. Timedelta is a subclass of datetime.

What is the use of Timedelta () function?

The 'timedelta()' function of Python is present in the datetime library, which is usually used to calculate differences in given dates.

What does Timedelta mean in Python?

Python timedelta class. The timedelta is a class in datetime module that represents duration. The delta means average of difference and so the duration expresses the difference between two date, datetime or time instances. By using timedelta, you may estimate the time for future and past.


1 Answers

package main  import (         "fmt"         "time" )  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 
like image 103
zzzz Avatar answered Oct 21 '22 02:10

zzzz