LoginSignup
3
2

More than 3 years have passed since last update.

Goの日付フォーマットでミリ秒、マイクロ秒、ナノ秒を取得するには

Posted at

Goのtimeパッケージで日時をフォーマットする際は、米国の日付表記の順番を記載すると対応する文字列が取得可能となっている。

main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2020, 2, 3, 14, 26, 31, 123456789, time.Local)
    fmt.Println(t.Format("2006/01/02 15:04:05"))
}

実行した結果はこんな感じ。

terminal
$ go run get_nanoseconds/main.go
2020/02/03 14:26:31

ミリ秒、マイクロ秒、ナノ秒を取得する

timeのフォーマットに使えるフォーマット用文字列は下記の通りとなっている。

https://golang.org/src/time/format.go より

const (
    _                        = iota
    stdLongMonth             = iota + stdNeedDate  // "January"
    stdMonth                                       // "Jan"
    stdNumMonth                                    // "1"
    stdZeroMonth                                   // "01"
    stdLongWeekDay                                 // "Monday"
    stdWeekDay                                     // "Mon"
    stdDay                                         // "2"
    stdUnderDay                                    // "_2"
    stdZeroDay                                     // "02"
    stdUnderYearDay                                // "__2"
    stdZeroYearDay                                 // "002"
    stdHour                  = iota + stdNeedClock // "15"
    stdHour12                                      // "3"
    stdZeroHour12                                  // "03"
    stdMinute                                      // "4"
    stdZeroMinute                                  // "04"
    stdSecond                                      // "5"
    stdZeroSecond                                  // "05"
    stdLongYear              = iota + stdNeedDate  // "2006"
    stdYear                                        // "06"
    stdPM                    = iota + stdNeedClock // "PM"
    stdpm                                          // "pm"
    stdTZ                    = iota                // "MST"
    stdISO8601TZ                                   // "Z0700"  // prints Z for UTC
    stdISO8601SecondsTZ                            // "Z070000"
    stdISO8601ShortTZ                              // "Z07"
    stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC
    stdISO8601ColonSecondsTZ                       // "Z07:00:00"
    stdNumTZ                                       // "-0700"  // always numeric
    stdNumSecondsTz                                // "-070000"
    stdNumShortTZ                                  // "-07"    // always numeric
    stdNumColonTZ                                  // "-07:00" // always numeric
    stdNumColonSecondsTZ                           // "-07:00:00"
    stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included
    stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted

    stdNeedDate  = 1 << 8             // need month, day, year
    stdNeedClock = 2 << 8             // need hour, minute, second
    stdArgShift  = 16                 // extra argument in high bits, above low stdArgShift
    stdMask      = 1<<stdArgShift - 1 // mask out argument
)

0埋めした値を取得

0埋めしたミリ秒、マイクロ秒、ナノ秒を取得する場合はこの内、stdFracSecond0を利用して.000・・・という文字列を設定する。

main.go
// 抜粋
func main() {
    t := time.Date(2020, 2, 3, 14, 26, 31, 123456700, time.Local)

    fmt.Println(t.Format("2006/01/02 15:04:05.000")) // ミリ秒
    fmt.Println(t.Format("2006/01/02 15:04:05.000000")) // マイクロ秒
    fmt.Println(t.Format("2006/01/02 15:04:05.000000000")) // ナノ秒
}

実行結果

terminal
$ go run get_nanoseconds/main.go
2020/02/03 14:26:31.123
2020/02/03 14:26:31.123456
2020/02/03 14:26:31.123456700

0埋めしない値を取得

上記例では.000・・・という形で記載したが、stdFracSecond9で定義されている記述の.999・・・と書くことでもミリ秒以下を取得可能である。
この場合、表示最終桁から0が続いている場合は0埋めされずに表示から除外される。

main.go
// 抜粋
func main() {
    t := time.Date(2020, 2, 3, 14, 26, 31, 123456700, time.Local)

    fmt.Println(t.Format("2006/01/02 15:04:05.999999999"))
}

実行結果

terminal
$ go run get_nanoseconds/main.go
2020/02/03 14:26:31.1234567

サンプル

下記にもサンプルコードを記載しておきます。
https://github.com/ayatothos/go-samples/blob/master/get_nanoseconds/main.go

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2