strftime.go
func Strftime(t time.Time, f string) (string, error) {
	var result []string
	format := []rune(f)

	add := func(str string) {
		result = append(result, str)
	}

	for i := 0; i < len(format); i++ {
		switch format[i] {
		case '%':
			if i < len(format)-1 {
				switch format[i+1] {
				case 'a':
					add(shortDayNames[t.Weekday()])
				case 'A':
					add(longDayNames[t.Weekday()])
				case 'b', 'h':
					add(shortMonthNames[t.Month()])
				case 'B':
					add(longMonthNames[t.Month()])
				case 'c':
					add(t.Format("Mon Jan 2 15:04:05 2006"))
				case 'C':
					add(fmt.Sprintf("%02d", t.Year()/100))
				case 'd':
					add(fmt.Sprintf("%02d", t.Day()))
				case 'D', 'x':
					add(fmt.Sprintf("%02d/%02d/%02d", t.Month(), t.Day(), t.Year()%100))
				case 'e':
					add(fmt.Sprintf("%2d", t.Day()))
				case 'f':
					add(fmt.Sprintf("%09d", t.Nanosecond()))
				case 'F':
					add(fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day()))
				case 'H':
					add(fmt.Sprintf("%02d", t.Hour()))
				case 'I':
					if t.Hour() == 0 {
						add(fmt.Sprintf("%02d", 12))
					} else if t.Hour() > 12 {
						add(fmt.Sprintf("%02d", t.Hour()-12))
					} else {
						add(fmt.Sprintf("%02d", t.Hour()))
					}
				case 'j':
					add(fmt.Sprintf("%03d", t.YearDay()))
				case 'k':
					add(fmt.Sprintf("%2d", t.Hour()))
				case 'l':
					if t.Hour() == 0 {
						add(fmt.Sprintf("%2d", 12))
					} else if t.Hour() > 12 {
						add(fmt.Sprintf("%2d", t.Hour()-12))
					} else {
						add(fmt.Sprintf("%2d", t.Hour()))
					}
				case 'm':
					add(fmt.Sprintf("%02d", t.Month()))
				case 'M':
					add(fmt.Sprintf("%02d", t.Minute()))
				case 'n':
					add("\n")
				case 'p':
					if t.Hour() < 12 {
						add("AM")
					} else {
						add("PM")
					}
				case 'r':
					s, _ := Strftime(t, "%I:%M:%S %p")
					add(s)
				case 'R':
					add(fmt.Sprintf("%02d:%02d", t.Hour(), t.Minute()))
				case 's':
					add(fmt.Sprintf("%d", t.Unix()))
				case 'S':
					add(fmt.Sprintf("%02d", t.Second()))
				case 't':
					add("\t")
				case 'T', 'X':
					add(fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second()))
				case 'u':
					w := t.Weekday()
					if w == 0 {
						w = 7
					}
					add(fmt.Sprintf("%d", w))
				case 'U':
					add(fmt.Sprintf("%02d", weekNumber(t, 'U')))
				case 'w':
					add(fmt.Sprintf("%d", t.Weekday()))
				case 'W':
					add(fmt.Sprintf("%02d", weekNumber(t, 'W')))
				case 'y':
					add(fmt.Sprintf("%02d", t.Year()%100))
				case 'Y':
					add(fmt.Sprintf("%02d", t.Year()))
				case 'z':
					add(t.Format("-0700"))
				case 'Z':
					add(t.Format("MST"))
				case '%':
					add("%")
				default:
					return "", fmt.Errorf("invalid format code: %c", format[i+1])
				}
				i += 1
			}
		default:
			add(string(format[i]))
		}
	}

	return strings.Join(result, ""), nil
}
func Strftime(t time.Time, f string) (string, error) {
	var result []string
	format := []rune(f)

	add := func(str string) {
		result = append(result, str)
	}

	for i := 0; i < len(format); i++ {
		switch format[i] {
		case '%':
			if i < len(format)-1 {
				switch format[i+1] {
				case 'a':
					add(shortDayNames[t.Weekday()])
				case 'A':
					add(longDayNames[t.Weekday()])
				case 'b', 'h':
					add(shortMonthNames[t.Month()])
				case 'B':
					add(longMonthNames[t.Month()])
				case 'c':
					add(t.Format("Mon Jan 2 15:04:05 2006"))
				case 'C':
					add(fmt.Sprintf("%02d", t.Year()/100))
				case 'd':
					add(fmt.Sprintf("%02d", t.Day()))
				case 'D', 'x':
					add(fmt.Sprintf("%02d/%02d/%02d", t.Month(), t.Day(), t.Year()%100))
				case 'e':
					add(fmt.Sprintf("%2d", t.Day()))
				case 'f':
					add(fmt.Sprintf("%09d", t.Nanosecond()))
				case 'F':
					add(fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day()))
				case 'H':
					add(fmt.Sprintf("%02d", t.Hour()))
				case 'I':
					if t.Hour() == 0 {
						add(fmt.Sprintf("%02d", 12))
					} else if t.Hour() > 12 {
						add(fmt.Sprintf("%02d", t.Hour()-12))
					} else {
						add(fmt.Sprintf("%02d", t.Hour()))
					}
				case 'j':
					add(fmt.Sprintf("%03d", t.YearDay()))
				case 'k':
					add(fmt.Sprintf("%2d", t.Hour()))
				case 'l':
					if t.Hour() == 0 {
						add(fmt.Sprintf("%2d", 12))
					} else if t.Hour() > 12 {
						add(fmt.Sprintf("%2d", t.Hour()-12))
					} else {
						add(fmt.Sprintf("%2d", t.Hour()))
					}
				case 'm':
					add(fmt.Sprintf("%02d", t.Month()))
				case 'M':
					add(fmt.Sprintf("%02d", t.Minute()))
				case 'n':
					add("\n")
				case 'p':
					if t.Hour() < 12 {
						add("AM")
					} else {
						add("PM")
					}
				case 'r':
					s, _ := Strftime(t, "%I:%M:%S %p")
					add(s)
				case 'R':
					add(fmt.Sprintf("%02d:%02d", t.Hour(), t.Minute()))
				case 's':
					add(fmt.Sprintf("%d", t.Unix()))
				case 'S':
					add(fmt.Sprintf("%02d", t.Second()))
				case 't':
					add("\t")
				case 'T', 'X':
					add(fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second()))
				case 'u':
					w := t.Weekday()
					if w == 0 {
						w = 7
					}
					add(fmt.Sprintf("%d", w))
				case 'U':
					add(fmt.Sprintf("%02d", weekNumber(t, 'U')))
				case 'w':
					add(fmt.Sprintf("%d", t.Weekday()))
				case 'W':
					add(fmt.Sprintf("%02d", weekNumber(t, 'W')))
				case 'y':
					add(fmt.Sprintf("%02d", t.Year()%100))
				case 'Y':
					add(fmt.Sprintf("%02d", t.Year()))
				case 'z':
					add(t.Format("-0700"))
				case 'Z':
					add(t.Format("MST"))
				case '%':
					add("%")
				default:
					return "", fmt.Errorf("invalid format code: %c", format[i+1])
				}
				i += 1
			}
		default:
			add(string(format[i]))
		}
	}

	return strings.Join(result, ""), nil
}
func Strftime ( t time . Time , f string ) ( string , error ) { var result [ ] string format := [ ] rune ( f ) add := func ( str string ) { result = append ( result , str ) } for i := 0 ; i < len ( format ) ; i ++ { switch format [ i ] { case '%' : if i < len ( format ) - 1 { switch format [ i + 1 ] { case 'a' : add ( shortDayNames [ t . Weekday ( ) ] ) case 'A' : add ( longDayNames [ t . Weekday ( ) ] ) case 'b' , 'h' : add ( shortMonthNames [ t . Month ( ) ] ) case 'B' : add ( longMonthNames [ t . Month ( ) ] ) case 'c' : add ( t . Format ( " " ) ) case 'C' : add ( fmt . Sprintf ( " " , t . Year ( ) / 100 ) ) case 'd' : add ( fmt . Sprintf ( " " , t . Day ( ) ) ) case 'D' , 'x' : add ( fmt . Sprintf ( " " , t . Month ( ) , t . Day ( ) , t . Year ( ) % 100 ) ) case 'e' : add ( fmt . Sprintf ( " " , t . Day ( ) ) ) case 'f' : add ( fmt . Sprintf ( " " , t . Nanosecond ( ) ) ) case 'F' : add ( fmt . Sprintf ( " " , t . Year ( ) , t . Month ( ) , t . Day ( ) ) ) case 'H' : add ( fmt . Sprintf ( " " , t . Hour ( ) ) ) case 'I' : if t . Hour ( ) == 0 { add ( fmt . Sprintf ( " " , 12 ) ) } else if t . Hour ( ) > 12 { add ( fmt . Sprintf ( " " , t . Hour ( ) - 12 ) ) } else { add ( fmt . Sprintf ( " " , t . Hour ( ) ) ) } case 'j' : add ( fmt . Sprintf ( " " , t . YearDay ( ) ) ) case 'k' : add ( fmt . Sprintf ( " " , t . Hour ( ) ) ) case 'l' : if t . Hour ( ) == 0 { add ( fmt . Sprintf ( " " , 12 ) ) } else if t . Hour ( ) > 12 { add ( fmt . Sprintf ( " " , t . Hour ( ) - 12 ) ) } else { add ( fmt . Sprintf ( " " , t . Hour ( ) ) ) } case 'm' : add ( fmt . Sprintf ( " " , t . Month ( ) ) ) case 'M' : add ( fmt . Sprintf ( " " , t . Minute ( ) ) ) case 'n' : add ( " \n " ) case 'p' : if t . Hour ( ) < 12 { add ( " " ) } else { add ( " " ) } case 'r' : s , _ := Strftime ( t , " " ) add ( s ) case 'R' : add ( fmt . Sprintf ( " " , t . Hour ( ) , t . Minute ( ) ) ) case 's' : add ( fmt . Sprintf ( " " , t . Unix ( ) ) ) case 'S' : add ( fmt . Sprintf ( " " , t . Second ( ) ) ) case 't' : add ( " \t " ) case 'T' , 'X' : add ( fmt . Sprintf ( " " , t . Hour ( ) , t . Minute ( ) , t . Second ( ) ) ) case 'u' : w := t . Weekday ( ) if w == 0 { w = 7 } add ( fmt . Sprintf ( " " , w ) ) case 'U' : add ( fmt . Sprintf ( " " , weekNumber ( t , 'U' ) ) ) case 'w' : add ( fmt . Sprintf ( " " , t . Weekday ( ) ) ) case 'W' : add ( fmt . Sprintf ( " " , weekNumber ( t , 'W' ) ) ) case 'y' : add ( fmt . Sprintf ( " " , t . Year ( ) % 100 ) ) case 'Y' : add ( fmt . Sprintf ( " " , t . Year ( ) ) ) case 'z' : add ( t . Format ( " " ) ) case 'Z' : add ( t . Format ( " " ) ) case '%' : add ( " " ) default : return " " , fmt . Errorf ( " " , format [ i + 1 ] ) } i += 1 } default : add ( string ( format [ i ] ) ) } } return strings . Join ( result , " " ) , nil }
// Strftime formats time.Date according to the directives in the given format string. The directives begins with a percent (%) character.
Strftime formats time . Date according to the directives in the given format string . The directives begins with a percent ( % ) character .