Emphasize time constants

In most cases users will format and parse times using the constants
provided in package time. Show these first before getting into custom
example-based formats.
This commit is contained in:
Mark McGranaghan 2014-04-15 18:04:23 -07:00
parent 56c1fd9bc1
commit a8ee83af78
4 changed files with 72 additions and 90 deletions

View File

@ -10,21 +10,33 @@ func main() {
p := fmt.Println
// Here's a basic example of formatting a time
// according to RFC3339.
// according to RFC3339, using the corresponding format
// constant.
t := time.Now()
p(t.Format("2006-01-02T15:04:05Z07:00"))
p(t.Format(time.RFC3339))
// `Format` uses an example-based layout approach; it
// takes a formatted version of the reference time
// Time parsing uses the same format values as `Format`
// does.
t1, e := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
p(t1)
// `Format` and `Parse` uses example-based formats. They
// take 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. The example time must be exactly as shown: the
// year 2006, 15 for the hour, Monday for the day of the
// week, etc. Here are a few more examples of time
// formatting.
// general pattern with which to format/parse the given
// time/string. The example time must be exactly as shown:
// the year 2006, 15 for the hour, Monday for the day of
// the week, etc. Usually you'll use a constant from
// `time` for these formats, but you can also supply
// custom formats.
p(t.Format("3:04PM"))
p(t.Format("Mon Jan _2 15:04:05 2006"))
p(t.Format("2006-01-02T15:04:05.999999-07:00"))
form := "3 04 PM"
t2, e := time.Parse(form, "8 41 PM")
p(t2)
// For purely numeric representations you can also
// use standard string formatting with the extracted
@ -33,25 +45,9 @@ func main() {
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.
withNanos := "2006-01-02T15:04:05.999999999-07:00"
t1, e := time.Parse(
withNanos,
"2012-11-01T22:08:41.117442+00:00")
p(t1)
kitchen := "3:04PM"
t2, e := time.Parse(kitchen, "8:41PM")
p(t2)
// `Parse` will return an error on malformed input
// explaining the parsing problem.
ansic := "Mon Jan _2 15:04:05 2006"
_, e = time.Parse(ansic, "8:41PM")
p(e)
// There are several predefined formats that you can
// use for both formatting and parsing.
p(t.Format(time.Kitchen))
}

View File

@ -1,2 +1,2 @@
64395057e85c58a94c5298067a45216ba1682c9d
CevEHjELb6
f036cc1b8d2c2f5c5630c323001fb96efddbc7a8
siwR2J2ke0

View File

@ -1,10 +1,10 @@
$ go run time-formatting-parsing.go
2012-11-02T09:35:03-07:00
9:35AM
Fri Nov 2 09:35:03 2012
2012-11-02T09:35:03.982519-07:00
2012-11-02T09:35:03-00:00
2012-11-01 22:08:41.117442 +0000 UTC
2014-04-15T18:00:15-07:00
2012-11-01 22:08:41 +0000 +0000
6:00PM
Tue Apr 15 18:00:15 2014
2014-04-15T18:00:15.161182-07:00
0000-01-01 20:41:00 +0000 UTC
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ...
9:35AM
2014-04-15T18:00:15-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": \
cannot parse "8:41PM" as "Mon"

View File

@ -40,7 +40,7 @@ pattern-based layouts.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/CevEHjELb6"><img title="Run code" src="play.png" class="run" /></a>
<a href="http://play.golang.org/p/siwR2J2ke0"><img title="Run code" src="play.png" class="run" /></a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
@ -76,13 +76,14 @@ pattern-based layouts.</p>
<tr>
<td class="docs">
<p>Here&rsquo;s a basic example of formatting a time
according to RFC3339.</p>
according to RFC3339, using the corresponding format
constant.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">t</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="s">&quot;2006-01-02T15:04:05Z07:00&quot;</span><span class="p">))</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">))</span>
</pre></div>
</td>
@ -90,14 +91,32 @@ according to RFC3339.</p>
<tr>
<td class="docs">
<p><code>Format</code> uses an example-based layout approach; it
takes a formatted version of the reference time
<p>Time parsing uses the same format values as <code>Format</code>
does.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">t1</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Parse</span><span class="p">(</span>
<span class="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">,</span>
<span class="s">&quot;2012-11-01T22:08:41+00:00&quot;</span><span class="p">)</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t1</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Format</code> and <code>Parse</code> uses example-based formats. They
take a formatted version of the reference time
<code>Mon Jan 2 15:04:05 MST 2006</code> to determine the
general pattern with which to format the given
time. The example time must be exactly as shown: the
year 2006, 15 for the hour, Monday for the day of the
week, etc. Here are a few more examples of time
formatting.</p>
general pattern with which to format/parse the given
time/string. The example time must be exactly as shown:
the year 2006, 15 for the hour, Monday for the day of
the week, etc. Usually you&rsquo;ll use a constant from
<code>time</code> for these formats, but you can also supply
custom formats.</p>
</td>
<td class="code leading">
@ -105,6 +124,9 @@ formatting.</p>
<div class="highlight"><pre> <span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="s">&quot;3:04PM&quot;</span><span class="p">))</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="s">&quot;Mon Jan _2 15:04:05 2006&quot;</span><span class="p">))</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="s">&quot;2006-01-02T15:04:05.999999-07:00&quot;</span><span class="p">))</span>
<span class="nx">form</span> <span class="o">:=</span> <span class="s">&quot;3 04 PM&quot;</span>
<span class="nx">t2</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Parse</span><span class="p">(</span><span class="nx">form</span><span class="p">,</span> <span class="s">&quot;8 41 PM&quot;</span><span class="p">)</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t2</span><span class="p">)</span>
</pre></div>
</td>
@ -127,53 +149,17 @@ components of the time value.</p>
</td>
</tr>
<tr>
<td class="docs">
<p>Time parsing uses the same example-based approach
as <code>Format</code>ing. These examples parse times rendered
with some of the layouts used above.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">withNanos</span> <span class="o">:=</span> <span class="s">&quot;2006-01-02T15:04:05.999999999-07:00&quot;</span>
<span class="nx">t1</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Parse</span><span class="p">(</span>
<span class="nx">withNanos</span><span class="p">,</span>
<span class="s">&quot;2012-11-01T22:08:41.117442+00:00&quot;</span><span class="p">)</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t1</span><span class="p">)</span>
<span class="nx">kitchen</span> <span class="o">:=</span> <span class="s">&quot;3:04PM&quot;</span>
<span class="nx">t2</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Parse</span><span class="p">(</span><span class="nx">kitchen</span><span class="p">,</span> <span class="s">&quot;8:41PM&quot;</span><span class="p">)</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">t2</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Parse</code> will return an error on malformed input
explaining the parsing problem.</p>
</td>
<td class="code leading">
<td class="code">
<div class="highlight"><pre> <span class="nx">ansic</span> <span class="o">:=</span> <span class="s">&quot;Mon Jan _2 15:04:05 2006&quot;</span>
<span class="nx">_</span><span class="p">,</span> <span class="nx">e</span> <span class="p">=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Parse</span><span class="p">(</span><span class="nx">ansic</span><span class="p">,</span> <span class="s">&quot;8:41PM&quot;</span><span class="p">)</span>
<span class="nx">p</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>There are several predefined formats that you can
use for both formatting and parsing.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Kitchen</span><span class="p">))</span>
<span class="p">}</span>
</pre></div>
@ -191,15 +177,15 @@ use for both formatting and parsing.</p>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> go run <span class="nb">time</span>-formatting-parsing.go
<span class="go">2012-11-02T09:35:03-07:00</span>
<span class="go">9:35AM</span>
<span class="go">Fri Nov 2 09:35:03 2012</span>
<span class="go">2012-11-02T09:35:03.982519-07:00</span>
<span class="go">2012-11-02T09:35:03-00:00</span>
<span class="go">2012-11-01 22:08:41.117442 +0000 UTC</span>
<span class="go">2014-04-15T18:00:15-07:00</span>
<span class="go">2012-11-01 22:08:41 +0000 +0000</span>
<span class="go">6:00PM</span>
<span class="go">Tue Apr 15 18:00:15 2014</span>
<span class="go">2014-04-15T18:00:15.161182-07:00</span>
<span class="go">0000-01-01 20:41:00 +0000 UTC</span>
<span class="go">parsing time &quot;8:41PM&quot; as &quot;Mon Jan _2 15:04:05 2006&quot;: ...</span>
<span class="go">9:35AM</span>
<span class="go">2014-04-15T18:00:15-00:00</span>
<span class="go">parsing time &quot;8:41PM&quot; as &quot;Mon Jan _2 15:04:05 2006&quot;: \</span>
<span class="go"> cannot parse &quot;8:41PM&quot; as &quot;Mon&quot;</span>
</pre></div>
</td>