Something like python timedelta in golang - go

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

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

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.

timeutil supports timedelta and 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"
}

Related

Correct way to convert timezone offset in minutes to 24h format

I'm receiving a timezone offset in minutes ie. 240 (representing GMT-4) and need to convert it to -0800 or -08:00. How can I do that correctly in Go lang?
This seems to do it:
package main
import (
"fmt"
"time"
)
func zone(s string, d time.Duration) *time.Location {
f := d.Seconds()
return time.FixedZone(s, int(f))
}
func main() {
z := zone("GMT-4", -240 * time.Minute)
t := time.Date(2021, 3, 30, 19, 22, 53, 0, z)
fmt.Println(t) // 2021-03-30 19:22:53 -0400 GMT-4
}
https://golang.org/pkg/time#FixedZone

How to get specific time from next day

I want to create a time.Time for an exact point in time the following day (tomorrow). For now I would like to set the hour and minute.
This is the code I use at the moment:
now := time.Now()
tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.UTC).AddDate(0,0,1)
This will create a Date for today with the exact time (hour and minute) I am looking for and then adds one day to that Date. This works fine.
Example:
Imagine time.Now() is 2009-11-10 23:00:00 +0000 UTC.
The result of the following code would be: 2009-11-10 15:00:00 +0000 UTC
tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.UTC)
To this date I add one day using AddDate(0, 0, 1). The result is then the desired time the next day: 2009-11-11 15:00:00 +0000 UTC.
See: https://play.golang.org/p/OKR9V1HN50x
Question:
Is there a shorter way to write this code?
Package time
import "time"
The month, day, hour, min, sec, and nsec values may be outside their
usual ranges and will be normalized during the conversion. For
example, October 32 converts to November 1.
This is more efficient. It minimizes calls to package time functions and methods.
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Round(0))
yyyy, mm, dd := now.Date()
tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location())
fmt.Println(tomorrow)
}
Output:
2019-06-21 16:23:06.525478162 -0400 EDT
2019-06-22 15:00:00 -0400 EDT
Some benchmarks:
BenchmarkNow-8 31197811 36.6 ns/op
BenchmarkTomorrowPeterSO-8 29852671 38.4 ns/op
BenchmarkTomorrowJens-8 9523422 124 ns/op
bench_test.go:
package main
import (
"testing"
"time"
)
func BenchmarkNow(b *testing.B) {
for N := 0; N < b.N; N++ {
now := time.Now()
_ = now
}
}
var now = time.Now()
func BenchmarkTomorrowPeterSO(b *testing.B) {
for N := 0; N < b.N; N++ {
// now := time.Now()
yyyy, mm, dd := now.Date()
tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location())
_ = tomorrow
}
}
func BenchmarkTomorrowJens(b *testing.B) {
for N := 0; N < b.N; N++ {
// now := time.Now()
tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
_ = tomorrow
}
}

How to get the start date and end date of current month using golang?

I want to make a simple program of golang which only tell me the current month start date and and month's end date. There is also a condition in this is that if the user will entered the month and year then it will give me the start date and end date of that year and month. For this I have tried the below program but it will not giving me the right results:
package main
import (
"fmt"
"time"
)
func main() {
var year int
var month int
year = 2018
month = 1
if year != 0 && month != 0 {
t2 := time.Now().AddDate(year, month, 0)
fmt.Println(t2)
} else {
t2 := time.Now().AddDate(0, 0, 0)
fmt.Println(t2)
}
}
go playground link
Can any body tell me how I will correct the program.
Thanks in advance.
For example,
package main
import (
"fmt"
"time"
)
func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
firstDay = time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
lastDay = time.Date(y, m+1, 1, 0, 0, 0, -1, time.UTC)
return firstDay, lastDay
}
func main() {
var (
y int
m time.Month
)
y, m, _ = time.Now().Date()
first, last := monthInterval(y, m)
fmt.Println(first.Format("2006-01-02"))
fmt.Println(last.Format("2006-01-02"))
y, m = 2018, time.Month(2)
first, last = monthInterval(y, m)
fmt.Println(first.Format("2006-01-02"))
fmt.Println(last.Format("2006-01-02"))
}
Output:
2018-10-01
2018-10-31
2018-02-01
2018-02-28
Playground: https://play.golang.org/p/TkzCo9jLpZR
You can do with time package in golang itself.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(BeginningOfMonth(t))
fmt.Println(EndOfMonth(t))
// If you need only date use Format(). But remember Format() will return as a string
dateFormat := "2006-01-02"
fmt.Println(BeginningOfMonth(t).Format(dateFormat))
fmt.Println(EndOfMonth(t).Format(dateFormat))
}
func BeginningOfMonth(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}
func EndOfMonth(t time.Time) time.Time {
return BeginningOfMonth(t).AddDate(0, 1, 0).Add(-time.Second)
}
Output:
2018-10-01 00:00:00 +0530 IST
2018-10-31 23:59:59 +0530 IST
2018-10-01
2018-10-31
Umm You have to just see the below code I have written for you and just read the comments and learn it. By taking the reference of this and this time package in golang:
package main
import (
"fmt"
"strings"
"time"
)
func main() {
var year int
year = 2019
currentLocation := time.Now().Location() // got current location
if year != 0 {
firstOfMonth := time.Date(year, time.February, 1, 0, 0, 0, 0, currentLocation) // first date of the month
fmt.Println(firstOfMonth)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1).Format("2006-01-02 00:00:00 -0000") // last date of the month
fmt.Println(lastOfMonth)
onlyDate := strings.Split(lastOfMonth, " ")
fmt.Println(onlyDate[0])
}
}
go playground link
Edited
package main
import (
"fmt"
"time"
)
func main() {
month := 1
fmt.Println(time.Month(month))
}
converting int month into time link playground
Hope it will help you:).

How to get one month in seconds

Failing to do something fairly simple and can't find an answer
nor from uncle Google nor from here
I'm trying to get one full month in seconds (an int)
Something more elegant then this:
s := 3600 * 24 * 30
also tried :
m := time.Hour * 24 * 30
but that returns type 'time.Duration' which I also can't convert to an int
NOTE: Don't really care for the pressie days of each month (28-31)
but if it's possible to use specific month as input it will be super.
I'm not a Goxpert so Go (see what I did there?) easy on me..
Thanks in advance.
Use time.Duration Seconds() :
package main
import (
"fmt"
"time"
)
func main() {
m := time.Hour * 24 * 30
fmt.Println("In float: ", m.Seconds())
fmt.Println("In int: ", int(m.Seconds()))
}
Example on playground
If you do care about the different days in months, you can use time.Sub to get the duration:
package main
import (
"fmt"
"time"
)
func secondsInMonth(y int, m time.Month) int {
start := time.Date(y, m, 0, 0, 0, 0, 0, time.UTC)
end := time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC)
return int(end.Sub(start).Seconds())
}
func main() {
month := time.February
year := 2016
fmt.Printf("Days in %s %d: %d\n", month, year, secondsInMonth(year, month))
}
If you dont care just do 30*24*60*60. Thats as elegant as it gets.
Agree with the previous answers, but if you really want to remove the 24 * 30 it is possible.
Just goofing around; time.Time has some nice comparison features so this is another option:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
// the current month in seconds - can vary
duration := now.AddDate(0, 1, 0).Sub(now)
fmt.Println("In int: ", int(duration.Seconds()))
// similar, but exactly 30 days
duration := now.AddDate(0, 0, 30).Sub(now)
fmt.Println("In int: ", int(duration.Seconds()))
}
Example on playground
The number of days in a month depends on the month and the year (leap or non-leap year). The number of seconds in a day varies when there is a transition to or from daylight savings time. Therefore, the number of seconds in a month will vary by month, year, and time zone.
For example, Los Angeles is in the Pacific Time Zone. January and March both have 31 days. Because of the daylight savings time transition in March, there is one hour less (3600 = 60*60 seconds) in March than January.
For example,
package main
import (
"fmt"
"time"
)
func secondsInMonth(month time.Month, year int, loc *time.Location) int {
u := time.Date(year, month, 1, 0, 0, 0, 0, loc)
v := time.Date(year, month+1, 1, 0, 0, 0, 0, loc)
return int(v.Sub(u).Seconds())
}
func main() {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println(err)
return
}
year := 2016
jan := secondsInMonth(time.January, year, loc)
feb := secondsInMonth(time.February, year, loc)
mar := secondsInMonth(time.March, year, loc)
apr := secondsInMonth(time.April, year, loc)
fmt.Println(jan, feb, mar, apr)
fmt.Println(jan - mar)
}
Output:
2678400 2505600 2674800 2592000
3600
Since you are only having 30 day months, we have a constant here and there is absolutely no need for any calculation. Using basic maths, we simplify
30*24*60*60
to
const secsInMonth = 2592000

Get the first and last day of a year from a timestamp

Given a time object, t := time.Now(), is there a way I could get a timestamp for the first and last days of that year?
I could do something like d := t.YearDay() to get the number of days through the year, and then t.AddDate(0, 0, -d) to get the start of the year, and t.AddDate(0, 0, 365-d) to get the end of the year, but that seems to brittle as it doesn't deal with leap years etc.
You can use your Time struct object and create new Time struct object using time.Date and time.Time.Year() functions. So, if current time is t := time.Now() then last day in this year in UTC will be lt := time.Date(t.Year(), time.December, 31, 0, 0, 0, 0, time.UTC)
Here's an example:
package main
import "fmt"
import "time"
func main() {
t := time.Now()
last := time.Date(t.Year(), time.December,31, 0, 0, 0, 0, time.UTC)
first := time.Date(t.Year(), time.January,1, 0, 0, 0, 0, time.UTC)
fmt.Println("Current: ", t)
fmt.Println("First: ", first)
fmt.Println("Last: ", last)
}
http://play.golang.org/p/jahjd_mi6K

Resources