write ahead log

ロールフォワード用

golangの日付フォーマット指定が面白い

百聞は一見に如かずということでとりあえずサンプルを.

package main

import (
    "fmt"
    "time"
)

const (
    DATE_TIME_FORMAT = "2006/01/02/15:04:05"
)

func main() {
    now := time.Now().Format(DATE_TIME_FORMAT)
    fmt.Println(now)
}

実行結果

$ ./sample.exe
2016/04/26/17:48:42

日付フォーマット指定が
2006年1月2日の15時4分5秒
になっている.

これはgolangのtimeパッケージの規約のようだ.

timeパッケージのドキュメントを読むといたるところに出現する.

constで用意までされている.

   const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    // Handy time stamps.
    Stamp      = "Jan _2 15:04:05"
    StampMilli = "Jan _2 15:04:05.000"
    StampMicro = "Jan _2 15:04:05.000000"
    StampNano  = "Jan _2 15:04:05.000000000"

軽く整理すると以下のようになる.

指定 意味
2006
01
02
15
04
05

これはなかなか斬新である. 慣れるまでは違和感がすごいけど, 慣れると結構見やすいのかもしれない.

どちらにしても先進的な言語である.