
After go1.16, go will use module mode by default, even when the repository is checked out under GOPATH or in a one-off directory. Add go.mod, go.sum to keep this repo buildable without opting out of the module mode. > go mod init github.com/mmcgrana/gobyexample > go mod tidy > go mod vendor In module mode, the 'vendor' directory is special and its contents will be actively maintained by the go command. pygments aren't the dependency the go will know about, so it will delete the contents from vendor directory. Move it to `third_party` directory now. And, vendor the blackfriday package. Note: the tutorial contents are not affected by the change in go1.16 because all the examples in this tutorial ask users to run the go command with the explicit list of files to be compiled (e.g. `go run hello-world.go` or `go build command-line-arguments.go`). When the source list is provided, the go command does not have to compute the build list and whether it's running in GOPATH mode or module mode becomes irrelevant.
212 lines
9.8 KiB
Plaintext
Generated
212 lines
9.8 KiB
Plaintext
Generated
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Go by Example: Time Formatting / Parsing</title>
|
|
<link rel=stylesheet href="site.css">
|
|
</head>
|
|
<script>
|
|
onkeydown = (e) => {
|
|
|
|
if (e.key == "ArrowLeft") {
|
|
window.location.href = 'epoch';
|
|
}
|
|
|
|
|
|
if (e.key == "ArrowRight") {
|
|
window.location.href = 'random-numbers';
|
|
}
|
|
|
|
}
|
|
</script>
|
|
<body>
|
|
<div class="example" id="time-formatting-parsing">
|
|
<h2><a href="./">Go by Example</a>: Time Formatting / Parsing</h2>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Go supports time formatting and parsing via
|
|
pattern-based layouts.</p>
|
|
|
|
</td>
|
|
<td class="code empty leading">
|
|
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
<a href="http://play.golang.org/p/BoZYtr_2j66"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
|
<span class="s">"fmt"</span>
|
|
<span class="s">"time"</span>
|
|
<span class="p">)</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
|
<span class="nx">p</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Here’s a basic example of formatting a time
|
|
according to RFC3339, using the corresponding layout
|
|
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="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">))</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
<p>Time parsing uses the same layout values as <code>Format</code>.</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">"2012-11-01T22:08:41+00:00"</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> use example-based layouts. Usually
|
|
you’ll use a constant from <code>time</code> for these layouts, but
|
|
you can also supply custom layouts. Layouts must use the
|
|
reference time <code>Mon Jan 2 15:04:05 MST 2006</code> to show the
|
|
pattern with which to format/parse a 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.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<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">"3:04PM"</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">"Mon Jan _2 15:04:05 2006"</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">"2006-01-02T15:04:05.999999-07:00"</span><span class="p">))</span>
|
|
<span class="nx">form</span> <span class="o">:=</span> <span class="s">"3 04 PM"</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">"8 41 PM"</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>For purely numeric representations you can also
|
|
use standard string formatting with the extracted
|
|
components of the time value.</p>
|
|
|
|
</td>
|
|
<td class="code leading">
|
|
|
|
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"%d-%02d-%02dT%02d:%02d:%02d-00:00\n"</span><span class="p">,</span>
|
|
<span class="nx">t</span><span class="p">.</span><span class="nx">Year</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Month</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Day</span><span class="p">(),</span>
|
|
<span class="nx">t</span><span class="p">.</span><span class="nx">Hour</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Minute</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nx">Second</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">
|
|
|
|
<div class="highlight"><pre> <span class="nx">ansic</span> <span class="o">:=</span> <span class="s">"Mon Jan _2 15:04:05 2006"</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">"8:41PM"</span><span class="p">)</span>
|
|
<span class="nx">p</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
<td class="docs">
|
|
|
|
</td>
|
|
<td class="code">
|
|
|
|
<div class="highlight"><pre><span class="gp">$</span> go run time-formatting-parsing.go
|
|
<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">2014-04-15T18:00:15-00:00</span>
|
|
<span class="go">parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ...</span>
|
|
</pre></div>
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
<p class="next">
|
|
Next example: <a href="random-numbers">Random Numbers</a>.
|
|
</p>
|
|
|
|
<p class="footer">
|
|
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/time-formatting-parsing">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
|
</p>
|
|
</div>
|
|
<script>
|
|
var codeLines = [];
|
|
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\u003D fmt.Println\u000A');codeLines.push(' t :\u003D time.Now()\u000A p(t.Format(time.RFC3339))\u000A');codeLines.push(' t1, e :\u003D time.Parse(\u000A time.RFC3339,\u000A \"2012-11-01T22:08:41+00:00\")\u000A p(t1)\u000A');codeLines.push(' p(t.Format(\"3:04PM\"))\u000A p(t.Format(\"Mon Jan _2 15:04:05 2006\"))\u000A p(t.Format(\"2006-01-02T15:04:05.999999-07:00\"))\u000A form :\u003D \"3 04 PM\"\u000A t2, e :\u003D time.Parse(form, \"8 41 PM\")\u000A p(t2)\u000A');codeLines.push(' fmt.Printf(\"%d-%02d-%02dT%02d:%02d:%02d-00:00\\n\",\u000A t.Year(), t.Month(), t.Day(),\u000A t.Hour(), t.Minute(), t.Second())\u000A');codeLines.push(' ansic :\u003D \"Mon Jan _2 15:04:05 2006\"\u000A _, e \u003D time.Parse(ansic, \"8:41PM\")\u000A p(e)\u000A}\u000A');codeLines.push('');
|
|
</script>
|
|
<script src="site.js" async></script>
|
|
</body>
|
|
</html>
|