Goè¨èªã§strftime
ã»strptime
ç¸å½ã®é¢æ°ãæä¾ããã©ã¤ãã©ãªãå®è£
ãã¾ããã
t, _ := timefmt.Parse("2020/07/24 09:07:29", "%Y/%m/%d %H:%M:%S") fmt.Println(t) // 2020-07-24 09:07:29 +0000 UTC str := timefmt.Format(t, "%Y/%m/%d %H:%M:%S") fmt.Println(str) // 2020/07/24 09:07:29 str = timefmt.Format(t, "%a, %d %b %Y %T %z") fmt.Println(str) // Fri, 24 Jul 2020 09:07:29 +0000
ãªãä½ã£ãã
Goè¨èªã®æ¨æºã©ã¤ãã©ãªã«ã¯æ¥æã¨æååãå¤æããé¢æ°ãããã¾ããã2006å¹´1æ2æ¥ã®15:04:05ã§ãã©ã¼ããããæå®ããã¨ããç¬èªã®ä»æ§ãã¨ã£ã¦ãã¾ãã
ãããPythonãRubyã®ããã«strftime(3)
ã»strptime(3)
ã®ãã©ã¼ããããGoè¨èªã§ã使ãããã¨ãã人ã¯å¤ããæ§ã
ãªã©ã¤ãã©ãªãä½ããã¦ãã¾ããã
- imperfectgo/go-strftime
- lestrrat-go/strftime
- jehiah/go-strftime
- fastly/go-utils/strftime
- tebeka/strftime
- pbnjay/strptime
- knz/strtime
ãããã®å 人ã«ã¯æ¬æã表ãã¾ãããã©ã®ã©ã¤ãã©ãªã«ãæºè¶³ã§ããªãã¨ãããããã¾ããã
%F %T
ã%r
ã¨ãã£ãè¤æ°ã®æ å ±ãå«ããã®ãå®è£ ããã¦ããªã%-y-%-m-%-d
ã%_y-%_m-%_d
ã®ããã«paddingãæ¶ãããã¹ãã¼ã¹ã«ããããããã¨ãã§ããªã%10A, %10B %2k:%M
ã®ãããªå¹ ã®æå®ã%^a %^b
ã®ãããªå¤§æåã¸ã®å¤æãã§ããªã- cgoã使ã£ã¦ãããã¯ãã¹ãã«ãã§ããªã
- æååã¸ã®å¤æã¨æååããã®å¤æã¯ãåãã©ã¤ãã©ãªã¼ã§æä¾ããã
strconv.Atoi
ã¨strconv.Itoa
ãããããã«
ã©ã®ã©ã¤ãã©ãªãæ¹åããã«ãå¾®å¦ãªã¨ããããããèªåã§ä½ããã¨æãè³ã£ãããã§ãã
以ä¸ã表åãã®çç±ã§ãããæ¬å½ã®çç±ã¯gojqã«å¿
è¦ã ã£ãããã§ãã
jqã«ã¯strftimeã¨strptimeãããã¾ãã®ã§ããããgojqã§å®ç¾ããããã«ã¯Goè¨èªã§ãåãé¢æ°ãå¿
é ã§ãã
ã¤ã¾ãåã«æ¥æã¨æååãå¤æãããããã§ã¯ãªã㦠(ãããªãæ¨æºã©ã¤ãã©ãªã使ãã°ãã)ãstrftime
ã»strptime
ãã®ãã®ãå¿
è¦ã ã£ãã®ã§ãã
ããã¾ã§lestrrat-go/strftimeã¨pbnjay/strptimeã使ã£ã¦ãã¾ããã
lestrrat-go/strftimeã«ã¯ã»ã¨ãã©ä¸æºã¯ããã¾ããã§ããããpbnjay/strptimeã¯æ©è½çã«ä¸ååãªã㨠(%c
ã§ãã¼ã¹ã§ããªããªã©) ããå¤ãã©ã¤ãã©ãªã§ã©ã¤ã»ã³ã¹ãã¡ã¤ã«ãç½®ããã¦ããªããã¨ãªã©ä¸å®è¦ç´ ãå¤ããæ°ããã©ã¤ãã©ãªãä½ãã®ã«ã¯ååãªçç±ã¨ãªãã¾ããã
timefmtã¯ãParse(source, format string)
ã¨Format(t time.Time, format string)
ãæä¾ãã¦ãã¾ãã
strftime(3)
ã»strptime(3)
ã®ã»ã¼å
¨ã¦ã®ãã©ã¼ãããæå®åãpaddingã®èª¿æ´ãå¹
ã®æå®ã«å¯¾å¿ãã¦ãã¾ãã
ããã©ã¼ãã³ã¹
timefmtã©ã¤ãã©ãªãä½ãå§ããã¨ãã¯ãããã©ã¼ãã³ã¹ã¯ããã¾ã§éè¦ãã¦ãã¾ããã§ããã
strftime
ã®ã©ã¤ãã©ãªã®ãã³ããã¼ã¯çµæã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¾ãã
å¤ãã®ãã©ã¼ãããã§ç¾ç¶æéã®ã©ã¤ãã©ãªã ã¨æãã¾ãã 以ä¸ã®æ¹éã§å®è£ ãã¦ããã¨ããããªãã®é度ãã§ã¾ããã
- ãã©ã¼ãããæå®æååãone-passã§è¾¿ã
- ææ¥ãæå»ãªã©äºæ¡ä»¥ä¸ã®æ°åã®æåååãé«éåãã
bytes.Buffer
ã§ã¯ãªã[]byte
ã使ã- æååã®çµåãé¿ãã
strconv.AppendInt
ã使ã - ã¡ã¢ãªã¼ã®ç¢ºä¿ã極åæ¸ãã
ä»ã®ã©ã¤ãã©ãªã§ã¯æ¨æºã©ã¤ãã©ãªã®ãã©ã¼ãããã«å¤æãã¦ããããã¡ã¢ãªã¼ç¢ºä¿ã«æ°ã使ã£ã¦ããªãã£ãããã¦ãã¾ããã
æ¨æºã©ã¤ãã©ãªã¯äºæåãä¸æåèªã¾ãªãã¨ã©ã®æå®åãåãããªãã®ãé
ããªãåå ã ã¨æãã¾ãã
ãã®ç¹ã¯strftime
ã®ãã©ã¼ãããã¯åªãã¦ãã¾ããã
timefmtã¯ãã以ä¸ãã¥ã¼ãã³ã°ããäºå®ã¯ããã¾ããã
ä»ã®timefmt.Format
ãé
ãã¨ããå ´é¢ã§ã¯ãfmt.Sprintf
ã®ãããªé¢æ°ã使ã£ã¦ã¯ãããªãã»ã©ããã©ã¼ãã³ã¹ã«å³ããå ´é¢ã§ãããã
loggerã®ããã«æå»ãåºå®ãã©ã¼ãããã§é«éã«åºåããå ´é¢ã§ã¯ããã©ã¼ãããæå®æååã使ããã«ç´æ¥æã§æ¸ãã¦ãã¾ãã°è¯ãã§ãããã
ãã®ããã«æ°æ¥½ã«æ§ãã¦ãããã¨ã«ãã£ã¦ããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ã®æ²¼ã«è¸ã¿è¾¼ã¿ãããªãã¨ããã®ã¯ãã©ã¤ãã©ãªãã·ã³ãã«ã«ä¿ã¤ä¸ã¤ã®ã³ãã ã¨æãã¾ãã
ã¾ã¨ã
Goè¨èªã§æå»ã¨æååãç¸äºã«å¤æããã©ã¤ãã©ãªtimefmtãä½ãã¾ããã
ä½ã£ã¦ããã¦ãªãã§ãããã»ã¨ãã©ã®å ´é¢ã§ã¯æ¨æºã©ã¤ãã©ãªã使ã£ã¦ããã¨è¯ãã§ãããã
strftime
èªä½ã欲ããå ´é¢ããæ¨æºã©ã¤ãã©ãªããå°ãéãã©ã¤ãã©ãªã欲ããã¨ãã«å½¹ã«ç«ã¤ã¨æãã¾ãã
gojq
ã¸ã®çµã¿è¾¼ã¿ã¯æåããjq
ã¨ã®äºææ§ã¯ä¸ãã£ãã®ã§æºè¶³ãã¦ãã¾ãã
strftime
ã«ã¯å¤ãã®æå®åãããããªããªãè¦ããããªãæ¹ãããã£ããããã¨æãã¾ãã
æä½é %Y-%m-%d %H:%M:%S
ãããã§æ¸ããããã«ãªãã¾ãããã
追å 㧠%a %b %e %I %p %Z
ããããè¦ãã¦ããã¨ä¾¿å©ã§ãã
ä»åtimefmtãä½ã£ããã¨ã§å
¨ã¦è¦ãã¦ãã¾ãã¾ããã
ãã¯ãåå®è£
ã¯æ·±ãç解ã¸ã®è¿éã§ããã