Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active July 13, 2019 21:46
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save joyrexus/a56717634a672dcdfd48 to your computer and use it in GitHub Desktop.
Save joyrexus/a56717634a672dcdfd48 to your computer and use it in GitHub Desktop.
time in go

Working with time in Go is pretty straightforward.

Times

Get the current local time:

now := time.Now()                                   // 02 Apr 15 14:03

Construct a time with Date(y, m, d, h, m, s, ns, loc):

y := 2009
m := time.November
d := 10
h := 23
m := 0
s := 0
t := time.Date(y, m, d, h, m, s, 0, time.UTC)       // 10 Nov 09 23:00

Durations

Given a Time, you can add a Duration.

To convert an integer number of time units (seconds, minutes, etc.) to a Duration, just multiply:

now := time.Now()                                   // 02 Apr 15 14:03

mins := 10
later := now.Add(time.Duration(mins) * time.Minute) // 02 Apr 15 14:13

You can also use AddDate(y, m, d) to add a given number of years, months, days:

y := 2
m := 2
d := 2

later := now.AddDate(y, m, d)                       // 04 Jun 17 14:13

Formatting

Various formatting options are pre-defined:

fmt.Println(now.Format(time.RFC822))                // 02 Apr 15 14:03 CDT
fmt.Println(now.Format(time.Kitchen))               // 2:13PM

You can also define your own format with an example layout:

const layout = "Jan 2, 2006 at 3:04pm"
fmt.Println(now.Format(layout))                     // Apr 2, 2015 at 2:13pm

Parsing

You also use layouts for parsing strings representing times:

const shortForm = "2006-01-02"
loc, _ := time.LoadLocation("America/Chicago")

t, _ := time.ParseInLocation(shortForm, "2015-01-19", loc)

The time t is now 2015-01-19 00:00:00 -0600 CST.

Encoding/Decoding

See codec.go example.

package main
import (
"fmt"
"time"
"encoding/binary"
)
func main() {
const shortForm = "2006-01-02"
loc, _ := time.LoadLocation("America/Chicago")
t, err := time.ParseInLocation(shortForm, "2015-01-19", loc)
if err != nil {
fmt.Println(err)
return
}
d := decode(encode(t))
fmt.Println(d)
fmt.Println(d.Format(shortForm))
}
// decode unmarshals a time.
func decode(b []byte) time.Time {
i := int64(binary.BigEndian.Uint64(b))
return time.Unix(i, 0)
}
// encode marshals a time.
func encode(t time.Time) []byte {
buf := make([]byte, 8)
u := uint64(t.Unix())
binary.BigEndian.PutUint64(buf, u)
return buf
}
/*
// see http://stackoverflow.com/a/23695774/546630
type ShortFormTime time.Time
func (s ShortFormTime) UnmarshalJSON(buf []byte) error {
. . .
}
func (s ShortFormTime) MarshalJSON() ([]byte, error) {
t := time.Time(s)
return []byte(t.Format(`"2006-01-02"`)), nil
}
*/
package main
import (
"fmt"
"time"
)
func main() {
t0 := time.Now()
fmt.Println("The time is now ...")
printTime(t0)
// To convert an integer number of units to a Duration, just multiply.
// see `http://golang.org/pkg/time/#Duration`.
mins := 10
days := 2
t1 := t0.Add(time.Duration(mins) * time.Minute)
fmt.Printf("\n... and in %v minutes the time will be ...\n", mins)
printTime(t1)
fmt.Printf("\n... and %v days after that ...\n", days)
t2 := t1.Add(time.Duration(days) * time.Hour * 24)
printTime(t2)
// Use `AddDate(y, m, d)` to add the given number of years, months, days.
years := 2
months := 2
fmt.Printf(
"\n... and %v years, %v months, and %v days after that ...\n",
years, months, days,
)
t3 := t2.AddDate(years, months, days)
printTime(t3)
}
func printTime(t time.Time) {
// see http://golang.org/pkg/time/#pkg-constants
fmt.Println(t.Format(time.RFC822))
// see http://golang.org/pkg/time/#Time.Format
const layout = "Jan 2, 2006 at 3:04pm (MST)"
fmt.Println(t.Format(layout))
}
@0-wiz-0
Copy link

0-wiz-0 commented Sep 11, 2015

y := 2009
m := time.November
d := 10
h := 23
m := 0
s := 0
t := time.Date(y, m, d, h, m, s, 0, time.UTC)       // 10 Nov 09 23:00

No, this will give you 10 (0th month) 09 23:00, or probably rather an error before that because you have two "m:=" lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment