forked from mmcgrana/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-formatting-parsing
202 lines (156 loc) · 8.91 KB
/
time-formatting-parsing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Time Formatting / Parsing</title>
<link rel=stylesheet href="site.css">
</head>
<script type="text/javascript">
if (window.location.host == "gobyexample.com") {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-34996217-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</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/BFw556nWcM"><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>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
<span class="kn">import</span> <span class="s">"time"</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://twitter.com/mmcgrana">@mmcgrana</a> | <a href="mailto:[email protected]">feedback</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>
</body>
</html>