github.com/nsqio/nsq@v1.3.0/apps/nsq_to_file/strftime.go (about)

     1  // COPIED FROM https://github.com/jehiah/go-strftime
     2  package main
     3  
     4  import (
     5  	"time"
     6  )
     7  
     8  // taken from time/format.go
     9  var conversion = map[string]string{
    10  	/*stdLongMonth      */ "B": "January",
    11  	/*stdMonth          */ "b": "Jan",
    12  	// stdNumMonth       */ "m": "1",
    13  	/*stdZeroMonth      */ "m": "01",
    14  	/*stdLongWeekDay    */ "A": "Monday",
    15  	/*stdWeekDay        */ "a": "Mon",
    16  	// stdDay            */ "d": "2",
    17  	// stdUnderDay       */ "d": "_2",
    18  	/*stdZeroDay        */ "d": "02",
    19  	/*stdHour           */ "H": "15",
    20  	// stdHour12         */ "I": "3",
    21  	/*stdZeroHour12     */ "I": "03",
    22  	// stdMinute         */ "M": "4",
    23  	/*stdZeroMinute     */ "M": "04",
    24  	// stdSecond         */ "S": "5",
    25  	/*stdZeroSecond     */ "S": "05",
    26  	/*stdLongYear       */ "Y": "2006",
    27  	/*stdYear           */ "y": "06",
    28  	/*stdPM             */ "p": "PM",
    29  	// stdpm             */ "p": "pm",
    30  	/*stdTZ             */ "Z": "MST",
    31  	// stdISO8601TZ      */ "z": "Z0700",  // prints Z for UTC
    32  	// stdISO8601ColonTZ */ "z": "Z07:00", // prints Z for UTC
    33  	/*stdNumTZ          */ "z": "-0700", // always numeric
    34  	// stdNumShortTZ     */ "b": "-07",    // always numeric
    35  	// stdNumColonTZ     */ "b": "-07:00", // always numeric
    36  	"%": "%",
    37  }
    38  
    39  // This is an alternative to time.Format because no one knows
    40  // what date 040305 is supposed to create when used as a 'layout' string
    41  // this takes standard strftime format options. For a complete list
    42  // of format options see http://strftime.org/
    43  func strftime(format string, t time.Time) string {
    44  	layout := ""
    45  	length := len(format)
    46  	for i := 0; i < length; i++ {
    47  		if format[i] == '%' && i <= length-2 {
    48  			if layoutCmd, ok := conversion[format[i+1:i+2]]; ok {
    49  				layout = layout + layoutCmd
    50  				i++
    51  				continue
    52  			}
    53  		}
    54  		layout = layout + format[i:i+1]
    55  	}
    56  	return t.Format(layout)
    57  }