diff --git a/examples.txt b/examples.txt index 27558cd..ed47167 100644 --- a/examples.txt +++ b/examples.txt @@ -48,7 +48,9 @@ String Functions Regular Expressions # Bytes # JSON -# Time +# Protocol Buffers +Time +# Time Parsing / Formatting # Epochs # Elapsed Time # Random Numbers diff --git a/examples/elapsed-time/elapsed-time.go b/examples/elapsed-time/elapsed-time.go index 6e315ad..1ba3feb 100644 --- a/examples/elapsed-time/elapsed-time.go +++ b/examples/elapsed-time/elapsed-time.go @@ -8,3 +8,5 @@ func main() { time.Sleep(3 * time.Second) fmt.Println(time.Since(start)) } + +// todo: check out that blog post diff --git a/examples/time/time.go b/examples/time/time.go index 973606a..c9d8a3f 100644 --- a/examples/time/time.go +++ b/examples/time/time.go @@ -1,20 +1,61 @@ +// Go's offers extensive support for times and durations; +// here are some examples. + package main -import "time" import "fmt" +import "time" func main() { - now := time.Now() - fmt.Println(now) + p := fmt.Println + // We'll start by getting the current time. + now := time.Now() + p(now) + + // You can build a `time` struct by providing the + // year, month, day, etc. Times are always associated + // with a `Location`, i.e. time zone. then := time.Date( 2009, 11, 17, 20, 34, 58, 651387237, time.UTC) - fmt.Println(then) + p(then) + // You can extract the various components of the time + // value as expected. + p(then.Year()) + p(then.Month()) + p(then.Day()) + p(then.Hour()) + p(then.Minute()) + p(then.Second()) + p(then.Nanosecond()) + p(then.Location()) + + // The Monday-Sunday `Weekday` is also available. + p(then.Weekday()) + + // These methods compare two times, testing if the + // first occurs before, after, or at the same time + // as the second, respectively. + p(then.Before(now)) + p(then.After(now)) + p(then.Equal(now)) + + // The `Sub` methods returns a `Duration` representing + // the interval between two times. diff := now.Sub(then) - fmt.Println(diff) -} + p(diff) -// todo: extract parts -// todo: add duration -// todo: check before / after + // We can compute the length of the duration in + // various units. + p(diff.Hours()) + p(diff.Minutes()) + p(diff.Seconds()) + p(diff.Nanoseconds()) + + // You can use `Add` to advance a time by a given + // duration, or with a `-` to move backwards by a + // duration. + p(then.Add(diff)) + p(then.Add(-diff)) +} diff --git a/examples/time/time.sh b/examples/time/time.sh index 501807e..398043d 100644 --- a/examples/time/time.sh +++ b/examples/time/time.sh @@ -1,4 +1,25 @@ $ go run time.go -2012-09-23 11:28:59.551605 -0700 PDT +2012-10-31 15:50:13.793654 +0000 UTC 2009-11-17 20:34:58.651387237 +0000 UTC -24981h54m0.900217763s +2009 +November +17 +20 +34 +58 +651387237 +UTC +Tuesday +true +false +false +25891h15m15.142266763s +25891.25420618521 +1.5534752523711128e+06 +9.320851514226677e+07 +93208515142266763 +2012-10-31 15:50:13.793654 +0000 UTC +2006-12-05 01:19:43.509120474 +0000 UTC + +# Learn more about Go's time support in the +# [`time`](http://golang.org/pkg/time/) package docs.