From 5110cee76a697b8f5d35008b0d062c2ee7f25103 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Thu, 1 Nov 2012 15:26:20 -0700 Subject: [PATCH] publish time-formatting-parsing --- examples.txt | 2 +- examples/epoch/epoch.sh | 3 ++ .../time-formatting-parsing.go | 52 +++++++++++++++++++ .../time-formatting-parsing.sh | 9 ++++ tools/generate.go | 2 + 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 examples/time-formatting-parsing/time-formatting-parsing.go create mode 100644 examples/time-formatting-parsing/time-formatting-parsing.sh diff --git a/examples.txt b/examples.txt index bc1a60f..2efc77c 100644 --- a/examples.txt +++ b/examples.txt @@ -47,7 +47,7 @@ Regular Expressions JSON Time Epoch -# Time Parsing / Formatting +Time Formatting / Parsing Random Numbers Number Parsing URL Parsing diff --git a/examples/epoch/epoch.sh b/examples/epoch/epoch.sh index ec3092e..18c97df 100644 --- a/examples/epoch/epoch.sh +++ b/examples/epoch/epoch.sh @@ -5,3 +5,6 @@ $ go run epoch.go 1351700038292387000 2012-10-31 16:13:58 +0000 UTC 2012-10-31 16:13:58.292387 +0000 UTC + +# Next we'll look at another time-related task: time +# parsing and formatting. diff --git a/examples/time-formatting-parsing/time-formatting-parsing.go b/examples/time-formatting-parsing/time-formatting-parsing.go new file mode 100644 index 0000000..589753e --- /dev/null +++ b/examples/time-formatting-parsing/time-formatting-parsing.go @@ -0,0 +1,52 @@ +// Go supports time formatting and parsing via +// pattern-based layouts. + +package main + +import "fmt" +import "time" + +func main() { + p := fmt.Println + + // Here's a basic example of formatting a time + // according to RFC3339. + t := time.Now() + p(t.Format("2006-01-02T15:04:05Z07:00")) + + // `Format` uses an example-based layout approach; it + // takes a formatted version of the reference time + // `Mon Jan 2 15:04:05 MST 2006` to determine the + // general pattern with which to format the given + // time. Here are a few more examples of time + // formatting. + p(t.Format("4:05PM")) + p(t.Format("Mon Jan _2 15:04:05 2006")) + p(t.Format("2006-01-02T15:04:05.999999-07:00")) + + // For purely numeric representations you can also + // use standard string formatting with the extracted + // components of the time value. + fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", + t.Year(), t.Month(), t.Day(), + t.Hour(), t.Minute(), t.Second()) + + // Time parsing uses the same example-based approach + // as `Format`ing. These examples parse times rendered + // with some of the layouts used above. + t1, e := time.Parse( + "2006-01-02T15:04:05.999999999-07:00", + "2012-11-01T22:08:41.117442+00:00") + p(t1) + t2, e := time.Parse( + "4:05PM", + "8:41PM") + p(t2) + + // `Parse` will return an error on malformed input + // explaining the parsing problem. + _, e = time.Parse( + "Mon Jan _2 15:04:05 2006", + "8:41PM") + p(e) +} diff --git a/examples/time-formatting-parsing/time-formatting-parsing.sh b/examples/time-formatting-parsing/time-formatting-parsing.sh new file mode 100644 index 0000000..a205ea6 --- /dev/null +++ b/examples/time-formatting-parsing/time-formatting-parsing.sh @@ -0,0 +1,9 @@ +$ go run time-formatting-parsing.go +2012-11-01T15:17:41-07:00 +17:41PM +Thu Nov 1 15:17:41 2012 +2012-11-01T15:17:41.256589-07:00 +2012-11-01T15:17:41-00:00 +0001-01-01 00:00:00 +0000 UTC +0000-01-01 12:08:41 +0000 UTC +parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ... diff --git a/tools/generate.go b/tools/generate.go index 0602a11..e6d5e8c 100644 --- a/tools/generate.go +++ b/tools/generate.go @@ -129,6 +129,7 @@ func debug(msg string) { var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s") var todoPat = regexp.MustCompile("\\/\\/ todo: ") +var dashPat = regexp.MustCompile("\\-+") type Seg struct { Docs, DocsRendered string @@ -213,6 +214,7 @@ func parseExamples() []*Example { exampleId = strings.Replace(exampleId, " ", "-", -1) exampleId = strings.Replace(exampleId, "/", "-", -1) exampleId = strings.Replace(exampleId, "'", "", -1) + exampleId = dashPat.ReplaceAllString(exampleId, "-") example.Id = exampleId example.Segs = make([][]*Seg, 0) sourcePaths := mustGlob("examples/" + exampleId + "/*")