回答編集履歴

3

質問に対する追記

2021/02/21 00:38

投稿

task4233
task4233

スコア106

test CHANGED
@@ -221,3 +221,151 @@
221
221
  // 2009/11/10 23:00:00 failed to getDate: failed to getDate(hogefugapiyo, 3)
222
222
 
223
223
  ```
224
+
225
+
226
+
227
+ ※2021/02/21 9:21追記
228
+
229
+ ## ①日にち単位での計算の時に、その日の10時からN日といったカウントを実現したいので、nowを設定するときにその日の10時と設定する方法も知っていたら教えて欲しいなと思っています!
230
+
231
+
232
+
233
+ `time.Date()`の引数に`time.Now()`を入れれば良いと思います。
234
+
235
+ [Playgroundのサンプルコード](https://play.golang.org/p/fF_vuGuilET)
236
+
237
+
238
+
239
+ ```go
240
+
241
+ package main
242
+
243
+
244
+
245
+ import (
246
+
247
+ "fmt"
248
+
249
+ "log"
250
+
251
+ "time"
252
+
253
+ )
254
+
255
+
256
+
257
+ func main() {
258
+
259
+ now := time.Now()
260
+
261
+ // func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
262
+
263
+ // 10時を明示的に指定
264
+
265
+ tenOClock := time.Date(now.Year(), now.Month(), now.Day(), 10, 0, 0, 0, time.Local)
266
+
267
+
268
+
269
+ fmt.Println(now)
270
+
271
+ fmt.Println(tenOClock)
272
+
273
+
274
+
275
+ // サーバの環境情報がJSTになってない場合は、明示的にJSTを指定する
276
+
277
+ jst, err := time.LoadLocation("Asia/Tokyo")
278
+
279
+ if err != nil {
280
+
281
+ log.Fatalf("failed to LoadLocation: %s", err.Error())
282
+
283
+ }
284
+
285
+ fmt.Println(tenOClock.In(jst))
286
+
287
+ }
288
+
289
+
290
+
291
+ // Output:
292
+
293
+ // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
294
+
295
+ // 2009-11-10 10:00:00 +0000 UTC
296
+
297
+ // 2009-11-10 19:00:00 +0900 JST
298
+
299
+ ```
300
+
301
+
302
+
303
+ ## ②ForeverDateですが、本当のMaxというよりは9999年12月31日0時0分0秒でセットしたいので、その方法でも教えて欲しいです!
304
+
305
+
306
+
307
+ 同様に、`time.Date()`で日付を設定してください。
308
+
309
+
310
+
311
+ [Playgroundのサンプルコード](https://play.golang.org/p/fUs6kD28Z4g)
312
+
313
+
314
+
315
+ ```go
316
+
317
+ package main
318
+
319
+
320
+
321
+ import (
322
+
323
+ "fmt"
324
+
325
+ "log"
326
+
327
+ "time"
328
+
329
+ )
330
+
331
+
332
+
333
+ func main() {
334
+
335
+ // func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
336
+
337
+ // ForeverDateを明示的に指定
338
+
339
+ foreverDate := time.Date(9999, 12, 31, 0, 0, 0, 0, time.Local)
340
+
341
+
342
+
343
+ fmt.Println(foreverDate)
344
+
345
+
346
+
347
+ // サーバの環境情報がJSTになってない場合は、明示的にJSTを指定する
348
+
349
+ jst, err := time.LoadLocation("Asia/Tokyo")
350
+
351
+ if err != nil {
352
+
353
+ log.Fatalf("failed to LoadLocation: %s", err.Error())
354
+
355
+ }
356
+
357
+ foreverDateJST := time.Date(9999, 12, 31, 0, 0, 0, 0, jst)
358
+
359
+ fmt.Println(foreverDateJST)
360
+
361
+ }
362
+
363
+
364
+
365
+ // Output:
366
+
367
+ // 9999-12-31 00:00:00 +0000 UTC
368
+
369
+ // 9999-12-31 00:00:00 +0900 JST
370
+
371
+ ```

2

メソッド名の修正

2021/02/21 00:38

投稿

task4233
task4233

スコア106

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ## 1. time.Time型での日付の計算
8
8
 
9
- [Add()](https://golang.org/pkg/time/#Time.Add)や[After()](https://golang.org/pkg/time/#Time.After)などを使ってください。
9
+ [Add()](https://golang.org/pkg/time/#Time.Add)や[AddDate()](https://golang.org/pkg/time/#Time.AddDate)などを使ってください。
10
10
 
11
11
  詳しくは、[timeパッケージのドキュメント](https://golang.org/pkg/time/#Time)を参照してください。
12
12
 

1

exampleの追記

2021/02/20 03:52

投稿

task4233
task4233

スコア106

test CHANGED
@@ -16,9 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- 簡単な実装例を載せておきます。
20
-
21
- [Go Playground](https://play.golang.org/p/s1mFegkB8Hk)
19
+ [簡単な実装例](https://play.golang.org/p/s1mFegkB8Hk)を載せておきます。
22
20
 
23
21
 
24
22
 
@@ -86,6 +84,18 @@
86
84
 
87
85
  }
88
86
 
87
+
88
+
89
+ // Output:
90
+
91
+ // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
92
+
93
+ // 2010-02-10 23:00:00 +0000 UTC
94
+
95
+ // 2009-12-03 23:00:00 +0000 UTC
96
+
97
+ // 292277026596-12-04 15:30:07 +0000 UTC
98
+
89
99
  ```
90
100
 
91
101
 
@@ -202,4 +212,12 @@
202
212
 
203
213
  }
204
214
 
215
+
216
+
217
+ // Output:
218
+
219
+ // 2009/11/10 23:00:00 2010-02-10 23:00:00 +0000 UTC
220
+
221
+ // 2009/11/10 23:00:00 failed to getDate: failed to getDate(hogefugapiyo, 3)
222
+
205
223
  ```