-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtime.html
265 lines (208 loc) · 11.8 KB
/
time.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 时间</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'xml';
}
if (e.key == "ArrowRight") {
window.location.href = 'epoch';
}
}
</script>
<body>
<div class="example" id="time">
<h2><a href="./">Go by Example 中文版</a>: 时间</h2>
<table>
<tr>
<td class="docs">
<p>Go 为时间(time)和时间段(duration)提供了大量的支持;这儿有是一些例子。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/w7A-KyYZPpy"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"time"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">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>
</td>
</tr>
<tr>
<td class="docs">
<p>从获取当前时间时间开始。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">now</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Now</span><span class="p">()</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">now</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>通过提供年月日等信息,你可以构建一个 <code>time</code>。
时间总是与 <code>Location</code> 有关,也就是时区。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">then</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Date</span><span class="p">(</span>
<span class="mi">2009</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">58</span><span class="p">,</span> <span class="mi">651387237</span><span class="p">,</span> <span class="nx">time</span><span class="p">.</span><span class="nx">UTC</span><span class="p">)</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你可以提取出时间的各个组成部分。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Year</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Month</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Day</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Hour</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Minute</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Second</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Nanosecond</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Location</span><span class="p">())</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>支持通过 <code>Weekday</code> 输出星期一到星期日。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Weekday</span><span class="p">())</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这些方法用来比较两个时间,分别测试一下是否为之前、之后或者是同一时刻,精确到秒。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Before</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">After</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Equal</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>方法 <code>Sub</code> 返回一个 <code>Duration</code> 来表示两个时间点的间隔时间。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">diff</span> <span class="o">:=</span> <span class="nx">now</span><span class="p">.</span><span class="nf">Sub</span><span class="p">(</span><span class="nx">then</span><span class="p">)</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们可以用各种单位来表示时间段的长度。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nf">Hours</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nf">Minutes</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nf">Seconds</span><span class="p">())</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nf">Nanoseconds</span><span class="p">())</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你可以使用 <code>Add</code> 将时间后移一个时间段,或者使用一个 <code>-</code> 来将时间前移一个时间段。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="nx">diff</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nf">Add</span><span class="p">(</span><span class="o">-</span><span class="nx">diff</span><span class="p">))</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run time.go
<span class="go">2012-10-31 15:50:13.793654 +0000 UTC
</span><span class="go">2009-11-17 20:34:58.651387237 +0000 UTC
</span><span class="go">2009
</span><span class="go">November
</span><span class="go">17
</span><span class="go">20
</span><span class="go">34
</span><span class="go">58
</span><span class="go">651387237
</span><span class="go">UTC
</span><span class="go">Tuesday
</span><span class="go">true
</span><span class="go">false
</span><span class="go">false
</span><span class="go">25891h15m15.142266763s
</span><span class="go">25891.25420618521
</span><span class="go">1.5534752523711128e+06
</span><span class="go">9.320851514226677e+07
</span><span class="go">93208515142266763
</span><span class="go">2012-10-31 15:50:13.793654 +0000 UTC
</span><span class="go">2006-12-05 01:19:43.509120474 +0000 UTC</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>接下来,我们将研究与 Unix 纪元相关的概念。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="epoch">时间戳</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</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(' now :\u003D time.Now()\u000A p(now)\u000A');codeLines.push(' then :\u003D time.Date(\u000A 2009, 11, 17, 20, 34, 58, 651387237, time.UTC)\u000A p(then)\u000A');codeLines.push(' p(then.Year())\u000A p(then.Month())\u000A p(then.Day())\u000A p(then.Hour())\u000A p(then.Minute())\u000A p(then.Second())\u000A p(then.Nanosecond())\u000A p(then.Location())\u000A');codeLines.push(' p(then.Weekday())\u000A');codeLines.push(' p(then.Before(now))\u000A p(then.After(now))\u000A p(then.Equal(now))\u000A');codeLines.push(' diff :\u003D now.Sub(then)\u000A p(diff)\u000A');codeLines.push(' p(diff.Hours())\u000A p(diff.Minutes())\u000A p(diff.Seconds())\u000A p(diff.Nanoseconds())\u000A');codeLines.push(' p(then.Add(diff))\u000A p(then.Add(-diff))\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>