How to update hour, min, sec in golang time? - go

Ex: How can I update hour in t time?
fmt.Println(t)
//=> 2006-01-02 15:04:05 +0000 UTC
Expect to get: 2006-01-02 00:00:00 +0000 UTC
Edited: similar to: time.Time Round to Day

Use:
t1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, t.Nanosecond(), t.Location())
Ref: https://golang.org/pkg/time/#Date

This seems to do it:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now().UTC()
t = t.Truncate(24 * time.Hour)
fmt.Println(t)
}
https://golang.org/pkg/time#Time.Truncate

Related

How to get a day of week from civil.Date

How can I get a day of week from civil.Date using the type civil.Date such as Monday, Sunday.
date := civil.Date{
Year: 2021,
Month: time.May,
Day: 1}
I am looking for the equivalent to Weekday() function of 'time' package.
Any alternative way is also welcomed.
How about
package main
import (
"fmt"
"os"
"time"
"cloud.google.com/go/civil"
)
func main() {
date := civil.Date{
Year: 2021,
Month: time.September,
Day: 5,
}
t, err := time.Parse(time.RFC3339, date.String()+"T00:00:00Z")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(t.Weekday().String())
}

Truncate a time duration by minutes

I want to subtract 30 minutes from orderDeliveryStartTime by using the Truncate function of the time library in Go. But it's subtracting 30 seconds instead. How can I subtract exactly 30 minutes from a time.Time?
package main
import (
"fmt"
"time"
)
func main() {
var pickingTimeConfig int
pickingTimeConfig = 30
layoutTime := "2006-01-02 15:04:05"
pickingTime := time.Duration(pickingTimeConfig) * time.Minute
fmt.Println(pickingTime.Nanoseconds())
vcmTimeLocation := time.FixedZone("UTC+7", 25200)
orderDeliveryStartTime := time.Date(2019, 4, 11, 13, 0, 30, 0, vcmTimeLocation)
fmt.Println(orderDeliveryStartTime.Format(layoutTime))
fmt.Println(orderDeliveryStartTime.Truncate(pickingTime).Format(layoutTime))
}
Actual Result:
1800000000000
2019-04-11 13:00:30
2019-04-11 13:00:00
Expected Result:
1800000000000
2019-04-11 13:00:30
2019-04-11 12:30:30
Simply use the Time.Add() method, passing -30 * time.Minute:
t2 := orderDeliveryStartTime.Add(-30 * time.Minute)
fmt.Println(t2.Format(layoutTime))
Outputs (try it on the Go Playground):
2019-04-11 13:00:30
2019-04-11 12:30:30

How i can to convert RFC3339 to UNIX in golang

I want to show some RFC3339 time as seconds. I found how to parse times string, but it not that
t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t)
// Unix returns t as a Unix time,
// the number of seconds elapsed since January 1, 1970 UTC.
fmt.Println(t.Unix())
}
Playground: https://play.golang.org/p/LG6G4lMIWt
Output:
2012-11-01 22:08:41 +0000 UTC
1351807721

Use timestamp from Mysql in humanize.Time() package

I have a mariaDB database with a timestamp field. I want the values from that field being parsed to time.Time() values. This is possible by adding the ?parseTime=true to the connection string. After fetching a row, I want to use the value (which is time.Time) with humanize.Time(). Unfortunately values within the past 60 minutes are converted by humanize.Time() as 1 hour from now. When I put directly a time.Time() value into humanize.Time(), it gives me x seconds ago.
So I don't know what I'm doing wrong here. I think I need to convert 2017-04-23 14:00:16 +0000 UTC to 2017-04-23 14:00:16.370758048 +0200 CEST, but how?
package main
import (
"fmt"
"log"
"time"
humanize "github.com/dustin/go-humanize"
)
// value from database: 2017-04-23 14:00:16 +0000 UTC
// typical time.Now() value: 2017-04-23 14:00:16.370758048 +0200 CEST
func main() {
layout := "2006-01-02 15:04:05 -0700 MST"
beforeParsing := "2017-04-23 14:00:16 +0000 UTC"
t, err := time.Parse(layout, beforeParsing)
if err != nil {
log.Fatal(err)
}
fmt.Println(t)
fmt.Println(humanize.Time(t))
}

Calculate number of days between two dates?

How can I calculate the number of days between two dates? In the code below I should get the number of hours, which means that I should only need to divide by 24. However, the result I get is something like -44929.000000. I'm only looking a day or two back so I would expect 24 or 48 hours.
package main
import (
"fmt"
"time"
)
func main() {
timeFormat := "2006-01-02"
t, _ := time.Parse(timeFormat, "2014-12-28")
fmt.Println(t)
// duration := time.Since(t)
duration := time.Now().Sub(t)
fmt.Printf("%f", duration.Hours())
}
Here's the executable Go code: http://play.golang.org/p/1MV6wnLVKh
Your program seems to work as intended. I'm getting 45.55 hours. Have you tried to run it locally?
Playground time is fixed, time.Now() will give you 2009-11-10 23:00:00 +0000 UTC always.
package main
import (
"fmt"
"time"
)
func main() {
date := time.Now()
fmt.Println(date)
format := "2006-01-02 15:04:05"
then,_ := time.Parse(format, "2007-09-18 11:58:06")
fmt.Println(then)
diff := date.Sub(then)
//func Since(t Time) Duration
//Since returns the time elapsed since t.
//It is shorthand for time.Now().Sub(t).
fmt.Println(diff.Hours())// number of Hours
fmt.Println(diff.Nanoseconds())// number of Nanoseconds
fmt.Println(diff.Minutes())// number of Minutes
fmt.Println(diff.Seconds())// number of Seconds
fmt.Println(int(diff.Hours()/24))// number of days
}
Here is the running code https://play.golang.org/p/Vbhh1cBKnh
the below code gives the list of all the days along with the number of days between the from date and to date:
you can click on the link for the code in
Go PlayGround:https://play.golang.org/p/MBThBpTqjdz
to := time.Now()
from := to.AddDate(0, -1, 0)
fmt.Println("toDate", to)
fmt.Println("fromDate", from)
days := to.Sub(from) / (24 * time.Hour)
fmt.Println("days", int(days))
noofdays := int(days)
for i := 0; i <= noofdays; i++ {
fmt.Println(from.AddDate(0, 0, i))
}
One caveat to be mindful of when using this technique of timeOne.Sub(timeTwo).Hours() / 24 is that daylights savings can cause a day to contain only 23 hours, throwing this calculation off slightly.
Happy programmer's day
package main
import (
"fmt"
"time"
)
func main() {
loc, _ := time.LoadLocation("UTC")
now := time.Now().In(loc)
firstDate := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, loc)
diff := now.Sub(firstDate)
fmt.Printf("The difference between %s and today %s es %d days\n", now.String(), firstDate.String(), int(diff.Hours()/24)+1)
// Just a joke
if ( int(diff.Hours()/24)+1 == 256 ) {
fmt.Printf("¡Happy programmer's day!")
} else {
fmt.Printf("On my computer it works...!?")
}
}

Resources