github.com/searKing/golang/go@v1.2.117/time/strftime.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time
     6  
     7  import (
     8  	"strings"
     9  )
    10  
    11  func LayoutTimeToStrftime(layout string) string {
    12  	return layoutTimeToStrftime(layout, false)
    13  }
    14  
    15  func LayoutTimeToSimilarStrftime(layout string) string {
    16  	return layoutTimeToStrftime(layout, true)
    17  }
    18  
    19  func LayoutStrftimeToTime(layout string) string {
    20  	return layoutStrftimeToTime(layout, false)
    21  }
    22  
    23  func LayoutStrftimeToSimilarTime(layout string) string {
    24  	return layoutStrftimeToTime(layout, true)
    25  }
    26  
    27  func layoutTimeToStrftime(layout string, similar bool) string {
    28  	var buf strings.Builder
    29  	// Each iteration generates one std value.
    30  	for layout != "" {
    31  		prefix, std_, suffix := nextStdChunk(layout)
    32  		if prefix != "" {
    33  			buf.WriteString(prefix)
    34  		}
    35  		if std_ == 0 {
    36  			break
    37  		}
    38  
    39  		if similar {
    40  			buf.WriteString(std(std_).SimilarStrftimeString())
    41  		} else {
    42  			buf.WriteString(std(std_).StrftimeString())
    43  		}
    44  		layout = suffix
    45  	}
    46  	return buf.String()
    47  }
    48  
    49  func layoutStrftimeToTime(layout string, similar bool) string {
    50  	var buf strings.Builder
    51  	// Each iteration generates one std value.
    52  	for layout != "" {
    53  		prefix, std_, suffix := nextStrftimeChunk(layout)
    54  		if prefix != "" {
    55  			buf.WriteString(prefix)
    56  		}
    57  		if std_ == 0 {
    58  			break
    59  		}
    60  
    61  		if similar {
    62  			buf.WriteString(std(std_).SimilarString())
    63  		} else {
    64  			buf.WriteString(std(std_).String())
    65  		}
    66  		layout = suffix
    67  	}
    68  	return buf.String()
    69  }
    70  
    71  // nextStdChunk finds the first occurrence of a std string in
    72  // layout and returns the text before, the std string, and the text after.
    73  func nextStrftimeChunk(layout string) (prefix string, std int, suffix string) {
    74  	for i := 0; i < len(layout); i++ {
    75  		switch c := int(layout[i]); c {
    76  		case '%': // %
    77  			j := i + 1
    78  			switch c := int(layout[j]); c {
    79  			case 'a': // Mon
    80  				return layout[:i], stdWeekDay, layout[j+1:]
    81  			case 'A': // Monday
    82  				return layout[0:i], stdLongWeekDay, layout[j+1:]
    83  			case 'b', 'h': // Jan
    84  				return layout[0:i], stdMonth, layout[j+1:]
    85  			case 'B': // January
    86  				return layout[0:i], stdLongMonth, layout[j+1:]
    87  			case 'c': // "Mon Jan _2 15:04:05 2006" (assumes "C" locale)
    88  				return layout[0:i], stdDateAndTime, layout[j+1:]
    89  			case 'C': // 20
    90  				return layout[0:i], stdFirstTwoDigitYear, layout[j+1:]
    91  			case 'd': // 02
    92  				return layout[0:i], stdZeroDay, layout[j+1:]
    93  			case 'D', 'x': // %m/%d/%y
    94  				return layout[0:i], stdShortSlashDate, layout[j+1:]
    95  			case 'e': // _2
    96  				return layout[0:i], stdUnderDay, layout[j+1:]
    97  			case 'E': // E modifier is to use a locale-dependent alternative representation
    98  				// %Ec, %EC, %Ex, %EX, %Ey, %EY
    99  				k := j + 1
   100  				switch c := int(layout[k]); c {
   101  				case 'c': // "Mon Jan _2 15:04:05 2006" (assumes "C" locale)
   102  					return layout[0:i], stdDateAndTime & stdNeedEModifier, layout[k+1:]
   103  				case 'C': // 20
   104  					return layout[0:i], stdFirstTwoDigitYear & stdNeedEModifier, layout[k+1:]
   105  				case 'x': // %m/%d/%y
   106  					return layout[0:i], stdShortSlashDate & stdNeedEModifier, layout[k+1:]
   107  				case 'X': // locale depended time representation (assumes "C" locale)
   108  					return layout[0:i], stdHourClockTime & stdNeedEModifier, layout[k+1:]
   109  				case 'y':
   110  					return layout[0:i], stdYear & stdNeedEModifier, layout[k+1:]
   111  				case 'Y':
   112  					return layout[0:i], stdLongYear & stdNeedEModifier, layout[k+1:]
   113  				}
   114  			case 'f': // fraction seconds in microseconds (Python)
   115  				std = stdFracSecond0
   116  				std |= 6 << stdArgShift // microseconds precision
   117  				return layout[0:i], std, layout[j+1:]
   118  			case 'F': // %Y-%m-%d
   119  				return layout[0:i], stdShortDashDate, layout[j+1:]
   120  			case 'g':
   121  				return layout[0:i], stdISO8601WeekYear, layout[j+1:]
   122  			case 'G':
   123  				return layout[0:i], stdISO8601LongWeekYear, layout[j+1:]
   124  			case 'H', 'k':
   125  				return layout[0:i], stdHour, layout[j+1:]
   126  			case 'I', 'l':
   127  				return layout[0:i], stdZeroHour12, layout[j+1:]
   128  			case 'j':
   129  				return layout[0:i], stdDayOfYear, layout[j+1:]
   130  			case 'm':
   131  				return layout[0:i], stdZeroMonth, layout[j+1:]
   132  			case 'M':
   133  				return layout[0:i], stdZeroMinute, layout[j+1:]
   134  			case 'n':
   135  				return layout[0:i], stdCharNewLine, layout[j+1:]
   136  			case 'O': // O modifier is to use alternative numeric symbols (say, roman numerals)
   137  				// %Od %Oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow %OW %Oy
   138  				k := j + 1
   139  				switch c := int(layout[k]); c {
   140  				case 'd': // 02
   141  					return layout[0:i], stdZeroDay & stdNeedOModifier, layout[k+1:]
   142  				case 'e': // _2
   143  					return layout[0:i], stdUnderDay & stdNeedOModifier, layout[k+1:]
   144  				case 'H':
   145  					return layout[0:i], stdHour & stdNeedOModifier, layout[k+1:]
   146  				case 'I':
   147  					return layout[0:i], stdZeroHour12 & stdNeedOModifier, layout[k+1:]
   148  				case 'm':
   149  					return layout[0:i], stdZeroMonth & stdNeedOModifier, layout[k+1:]
   150  				case 'M':
   151  					return layout[0:i], stdZeroMinute & stdNeedOModifier, layout[k+1:]
   152  				case 'S':
   153  					return layout[0:i], stdZeroSecond & stdNeedOModifier, layout[k+1:]
   154  				case 'u': // weekday as a decimal number, where Monday is 1
   155  					return layout[0:i], stdNumWeekDay & stdNeedOModifier, layout[k+1:]
   156  				case 'U': // week of the year as a decimal number (Sunday is the first day of the week)
   157  					return layout[0:i], stdSundayFirstWeekOfYear & stdNeedOModifier, layout[k+1:]
   158  				case 'V':
   159  					return layout[0:i], stdISO8601Week & stdNeedOModifier, layout[k+1:]
   160  				case 'w':
   161  					return layout[0:i], stdZeroNumWeek & stdNeedOModifier, layout[k+1:]
   162  				case 'W': // week of the year as a decimal number (Monday is the first day of the week)
   163  					return layout[0:i], stdMonFirstWeekOfYear & stdNeedOModifier, layout[k+1:]
   164  				case 'y':
   165  					return layout[0:i], stdYear & stdNeedOModifier, layout[k+1:]
   166  				}
   167  			case 'p':
   168  				return layout[0:i], stdPM, layout[j+1:]
   169  			case 'P':
   170  				return layout[0:i], stdpm, layout[j+1:]
   171  			case 'r': // "%I:%M:%S %p"
   172  				return layout[0:i], stdHour12ClockTime, layout[j+1:]
   173  			case 'R': // %H:%M"
   174  				return layout[0:i], stdHourHourMinuteTime, layout[j+1:]
   175  			case 's':
   176  				return layout[0:i], stdSecondsSinceEpoch, layout[j+1:]
   177  			case 'S':
   178  				return layout[0:i], stdZeroSecond, layout[j+1:]
   179  			case 't':
   180  				return layout[0:i], stdCharHorizontalTab, layout[j+1:]
   181  			case 'T': // %H:%M:%S
   182  				return layout[0:i], stdISO8601Time, layout[j+1:]
   183  			case 'u': // weekday as a decimal number, where Monday is 1
   184  				return layout[0:i], stdNumWeekDay, layout[j+1:]
   185  			case 'U': // week of the year as a decimal number (Sunday is the first day of the week)
   186  				return layout[0:i], stdSundayFirstWeekOfYear, layout[j+1:]
   187  			case 'v':
   188  				return layout[0:i], stdISO8601NumWeek, layout[j+1:]
   189  			case 'V':
   190  				return layout[0:i], stdISO8601Week, layout[j+1:]
   191  			case 'w':
   192  				return layout[0:i], stdZeroNumWeek, layout[j+1:]
   193  			case 'W': // week of the year as a decimal number (Monday is the first day of the week)
   194  				return layout[0:i], stdMonFirstWeekOfYear, layout[j+1:]
   195  			case 'X': // locale depended time representation (assumes "C" locale)
   196  				return layout[0:i], stdHourClockTime, layout[j+1:]
   197  			case 'y':
   198  				return layout[0:i], stdYear, layout[j+1:]
   199  			case 'Y':
   200  				return layout[0:i], stdLongYear, layout[j+1:]
   201  			case 'z':
   202  				return layout[0:i], stdISO8601ColonTZ, layout[j+1:]
   203  			case 'Z':
   204  				return layout[0:i], stdTZ, layout[j+1:]
   205  			case '%':
   206  				return layout[0:i], stdCharPercentSign, layout[j+1:]
   207  			}
   208  		}
   209  	}
   210  	return layout, 0, ""
   211  }