github.com/searKing/golang/go@v1.2.117/time/strftime_tables.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 "time"
     8  
     9  type std int
    10  
    11  const (
    12  	_                        = iota
    13  	stdLongMonth             = iota + stdNeedDate  // "January"
    14  	stdMonth                                       // "Jan"
    15  	stdNumMonth                                    // "1"
    16  	stdZeroMonth                                   // "01"
    17  	stdLongWeekDay                                 // "Monday"
    18  	stdWeekDay                                     // "Mon"
    19  	stdDay                                         // "2"
    20  	stdUnderDay                                    // "_2"
    21  	stdZeroDay                                     // "02"
    22  	stdUnderYearDay                                // "__2"
    23  	stdZeroYearDay                                 // "002"
    24  	stdHour                  = iota + stdNeedClock // "15"
    25  	stdHour12                                      // "3"
    26  	stdZeroHour12                                  // "03"
    27  	stdMinute                                      // "4"
    28  	stdZeroMinute                                  // "04"
    29  	stdSecond                                      // "5"
    30  	stdZeroSecond                                  // "05"
    31  	stdLongYear              = iota + stdNeedDate  // "2006"
    32  	stdYear                                        // "06"
    33  	stdPM                    = iota + stdNeedClock // "PM"
    34  	stdpm                                          // "pm"
    35  	stdTZ                    = iota                // "MST"
    36  	stdISO8601TZ                                   // "Z0700"  // prints Z for UTC
    37  	stdISO8601SecondsTZ                            // "Z070000"
    38  	stdISO8601ShortTZ                              // "Z07"
    39  	stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC
    40  	stdISO8601ColonSecondsTZ                       // "Z07:00:00"
    41  	stdNumTZ                                       // "-0700"  // always numeric
    42  	stdNumSecondsTz                                // "-070000"
    43  	stdNumShortTZ                                  // "-07"    // always numeric
    44  	stdNumColonTZ                                  // "-07:00" // always numeric
    45  	stdNumColonSecondsTZ                           // "-07:00:00"
    46  	stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included
    47  	stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted
    48  
    49  	// extend for strftime
    50  	stdNop
    51  	stdCharNewLine                                          // New-line character ('\n')
    52  	stdCharHorizontalTab                                    // Horizontal-tab character ('\t')
    53  	stdCharPercentSign                                      // A % sign	 ('%')
    54  	stdZeroNumWeek                                          // numerical week representation (0 - Sunday ~ 6 - Saturday)
    55  	stdNumWeekDay                                           // numerical week representation (1 - Monday ~ 7- Sunday)
    56  	stdSundayFirstWeekOfYear                                // week of the year (Sunday first)
    57  	stdMonFirstWeekOfYear                                   // week of the year (Monday first)
    58  	stdFirstTwoDigitYear                                    // "20"
    59  	stdSecondsSinceEpoch                                    // The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
    60  	stdDayOfYear                                            // day of the year (range [001,366])
    61  	stdDateAndTime                                          // Date and time representation; Thu Aug 23 14:55:02 2001
    62  	stdShortSlashDate                                       // Short MM/DD/YY date, equivalent to %m/%d/%y; 08/23/01
    63  	stdShortDashDate                                        // Short YYYY-MM-DD date, equivalent to %Y-%m-%d; 2001-08-23
    64  	stdHour12ClockTime                                      // 12-hour clock time; 02:55:02 pm
    65  	stdHourClockTime                                        // Time representation; 14:55:02
    66  	stdHourHourMinuteTime                                   // 24-hour HH:MM time, equivalent to %H:%M; 14:55
    67  	stdISO8601WeekYear       = iota + stdNeedISOISO8601Week // last two digits of ISO 8601 week-based year
    68  	stdISO8601LongWeekYear                                  // ISO 8601 week-based year
    69  	stdISO8601Week                                          // ISO 8601 week
    70  	stdISO8601NumWeek                                       // ISO 8601 week number (01-53); 34
    71  	stdISO8601Time                                          // ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S; 14:55:02
    72  
    73  	stdNeedDate           = 1 << 8             // need month, day, year
    74  	stdNeedClock          = 2 << 8             // need hour, minute, second
    75  	stdNeedISOISO8601Week = 4 << 8             // need ISO8601 week and year
    76  	stdNeedEModifier      = 8 << 8             // need to use alternative numeric symbols (say, roman numerals) %Ec, %EC, %Ex, %EX, %Ey, %EY
    77  	stdNeedOModifier      = 16 << 8            // need to use a locale-dependent alternative representation %Od, %Oe, %OH, %OI, %Om, %OM, %OS, %Ou, %OU, %OV, %Ow, %OW, %Oy
    78  	stdArgShift           = 16                 // extra argument in high bits, above low stdArgShift
    79  	stdMask               = 1<<stdArgShift - 1 // mask out argument
    80  
    81  )
    82  
    83  func (s std) String() string {
    84  	s_ := int(s & stdMask)
    85  	label, has := stdChunkNames[s_]
    86  	if has {
    87  		return label
    88  	}
    89  	return strftimeChunkNames[s_]
    90  }
    91  
    92  func (s std) SimilarString() string {
    93  	s_ := int(s)
    94  	label, has := stdChunkNames[s_]
    95  	if has {
    96  		return label
    97  	}
    98  	label, has = stdSimilarChunkNames[s_]
    99  	if has {
   100  		return label
   101  	}
   102  	return strftimeChunkNames[s_]
   103  }
   104  
   105  func (s std) StrftimeString() string {
   106  	s_ := int(s & stdMask)
   107  	label, has := strftimeChunkNames[s_]
   108  	if has {
   109  		return label
   110  	}
   111  	return stdChunkNames[s_]
   112  }
   113  
   114  func (s std) SimilarStrftimeString() string {
   115  	s_ := int(s)
   116  	label, has := strftimeChunkNames[s_]
   117  	if has {
   118  		return label
   119  	}
   120  	label, has = strftimeSimilarChunkNames[s_]
   121  	if has {
   122  		return label
   123  	}
   124  	return stdChunkNames[s_]
   125  }
   126  
   127  // stdChunkNames maps from nextStdChunk results to the matched strings.
   128  var stdChunkNames = map[int]string{
   129  	0:                               "",
   130  	stdLongMonth:                    "January",
   131  	stdMonth:                        "Jan",
   132  	stdNumMonth:                     "1",
   133  	stdZeroMonth:                    "01",
   134  	stdLongWeekDay:                  "Monday",
   135  	stdWeekDay:                      "Mon",
   136  	stdDay:                          "2",
   137  	stdUnderDay:                     "_2",
   138  	stdZeroDay:                      "02",
   139  	stdUnderYearDay:                 "__2",
   140  	stdZeroYearDay:                  "002",
   141  	stdHour:                         "15",
   142  	stdHour12:                       "3",
   143  	stdZeroHour12:                   "03",
   144  	stdMinute:                       "4",
   145  	stdZeroMinute:                   "04",
   146  	stdSecond:                       "5",
   147  	stdZeroSecond:                   "05",
   148  	stdLongYear:                     "2006",
   149  	stdYear:                         "06",
   150  	stdPM:                           "PM",
   151  	stdpm:                           "pm",
   152  	stdTZ:                           "MST",
   153  	stdISO8601TZ:                    "Z0700",
   154  	stdISO8601SecondsTZ:             "Z070000",
   155  	stdISO8601ShortTZ:               "Z07",
   156  	stdISO8601ColonTZ:               "Z07:00",
   157  	stdISO8601ColonSecondsTZ:        "Z07:00:00",
   158  	stdNumTZ:                        "-0700",
   159  	stdNumSecondsTz:                 "-070000",
   160  	stdNumShortTZ:                   "-07",
   161  	stdNumColonTZ:                   "-07:00",
   162  	stdNumColonSecondsTZ:            "-07:00:00",
   163  	stdFracSecond0 | 1<<stdArgShift: ".0",
   164  	stdFracSecond0 | 2<<stdArgShift: ".00",
   165  	stdFracSecond0 | 3<<stdArgShift: ".000",
   166  	stdFracSecond0 | 4<<stdArgShift: ".0000",
   167  	stdFracSecond0 | 5<<stdArgShift: ".00000",
   168  	stdFracSecond0 | 6<<stdArgShift: ".000000",
   169  	stdFracSecond0 | 7<<stdArgShift: ".0000000",
   170  	stdFracSecond0 | 8<<stdArgShift: ".00000000",
   171  	stdFracSecond0 | 9<<stdArgShift: ".000000000",
   172  	stdFracSecond9 | 1<<stdArgShift: ".9",
   173  	stdFracSecond9 | 2<<stdArgShift: ".99",
   174  	stdFracSecond9 | 3<<stdArgShift: ".999",
   175  	stdFracSecond9 | 4<<stdArgShift: ".9999",
   176  	stdFracSecond9 | 5<<stdArgShift: ".99999",
   177  	stdFracSecond9 | 6<<stdArgShift: ".999999",
   178  	stdFracSecond9 | 7<<stdArgShift: ".9999999",
   179  	stdFracSecond9 | 8<<stdArgShift: ".99999999",
   180  	stdFracSecond9 | 9<<stdArgShift: ".999999999",
   181  	stdNop:                          "",
   182  	stdCharNewLine:                  "\n",
   183  	stdCharHorizontalTab:            "\t",
   184  	stdCharPercentSign:              "%",
   185  	stdDateAndTime:                  time.ANSIC,
   186  	stdShortSlashDate:               "2006/01/02",
   187  	stdShortDashDate:                "2006-01-02",
   188  	stdHour12ClockTime:              "03:04:05 pm",
   189  	stdHourClockTime:                "15:04:05",
   190  	stdHourHourMinuteTime:           "15:04",
   191  	stdISO8601Time:                  "15:04:05",
   192  }
   193  
   194  var stdSimilarChunkNames = map[int]string{
   195  	0:                        "",
   196  	stdNop:                   "",
   197  	stdZeroNumWeek:           stdChunkNames[stdWeekDay],
   198  	stdNumWeekDay:            stdChunkNames[stdWeekDay],
   199  	stdSundayFirstWeekOfYear: stdChunkNames[stdWeekDay],
   200  	stdMonFirstWeekOfYear:    stdChunkNames[stdWeekDay],
   201  	stdFirstTwoDigitYear:     stdChunkNames[stdLongYear],
   202  	stdSecondsSinceEpoch:     strftimeChunkNames[stdSecondsSinceEpoch],
   203  	stdDayOfYear:             strftimeChunkNames[stdDayOfYear],
   204  	stdISO8601WeekYear:       stdChunkNames[stdYear],
   205  	stdISO8601LongWeekYear:   stdChunkNames[stdLongYear],
   206  	stdISO8601Week:           strftimeChunkNames[stdISO8601Week],
   207  	stdISO8601NumWeek:        strftimeChunkNames[stdISO8601NumWeek],
   208  
   209  	stdDateAndTime | stdNeedEModifier:           stdChunkNames[stdDateAndTime],
   210  	stdFirstTwoDigitYear | stdNeedEModifier:     stdChunkNames[stdLongYear],
   211  	stdShortSlashDate | stdNeedEModifier:        stdChunkNames[stdShortSlashDate],
   212  	stdHourClockTime | stdNeedEModifier:         stdChunkNames[stdHourClockTime],
   213  	stdYear | stdNeedEModifier:                  stdChunkNames[stdYear],
   214  	stdLongYear | stdNeedEModifier:              stdChunkNames[stdLongYear],
   215  	stdZeroDay | stdNeedOModifier:               stdChunkNames[stdZeroDay],
   216  	stdUnderDay | stdNeedOModifier:              stdChunkNames[stdUnderDay],
   217  	stdHour | stdNeedOModifier:                  stdChunkNames[stdHour],
   218  	stdZeroHour12 | stdNeedOModifier:            stdChunkNames[stdZeroHour12],
   219  	stdZeroMonth | stdNeedOModifier:             stdChunkNames[stdZeroMonth],
   220  	stdZeroMinute | stdNeedOModifier:            stdChunkNames[stdZeroMinute],
   221  	stdZeroSecond | stdNeedOModifier:            stdChunkNames[stdZeroSecond],
   222  	stdNumWeekDay | stdNeedOModifier:            stdChunkNames[stdWeekDay],
   223  	stdSundayFirstWeekOfYear | stdNeedOModifier: stdChunkNames[stdWeekDay],
   224  	stdISO8601Week | stdNeedOModifier:           strftimeChunkNames[stdISO8601Week],
   225  	stdZeroNumWeek | stdNeedOModifier:           stdChunkNames[stdWeekDay],
   226  	stdMonFirstWeekOfYear | stdNeedOModifier:    stdChunkNames[stdWeekDay],
   227  	stdYear | stdNeedOModifier:                  stdChunkNames[stdYear],
   228  }
   229  
   230  // strftimeChunkNames maps from nextStrftimeChunk results to the matched strings.
   231  // see http://www.cplusplus.com/reference/ctime/strftime/
   232  // see https://man7.org/linux/man-pages/man3/strftime.3.html
   233  var strftimeChunkNames = map[int]string{
   234  	0:                        "",
   235  	stdWeekDay:               "%a", // Thu; Abbreviated weekday name
   236  	stdLongWeekDay:           "%A", // Thursday; Full weekday name
   237  	stdMonth:                 "%b", // Aug; Abbreviated month name, (same as %h)
   238  	stdLongMonth:             "%B", // August; Full month name
   239  	stdDateAndTime:           "%c", // Thu Aug 23 14:55:02 2001; Date and time representation, %a %b %e %H:%M:%S %Y
   240  	stdFirstTwoDigitYear:     "%C", // 20; Year divided by 100 and truncated to integer (00-99)
   241  	stdZeroDay:               "%d", // 23; Day of the month, zero-padded (01-31)
   242  	stdShortSlashDate:        "%D", // 08/23/01; Short MM/DD/YY date, equivalent to %m/%d/%y, (same as %x)
   243  	stdUnderDay:              "%e", // 23; Day of the month, space-padded ( 1-31)
   244  	stdShortDashDate:         "%F", // 2001-08-23; Short YYYY-MM-DD date, equivalent to %Y-%m-%d
   245  	stdISO8601WeekYear:       "%g", // 01; Week-based year, last two digits (00-99)
   246  	stdISO8601LongWeekYear:   "%G", // 2001; Week-based year
   247  	stdHour:                  "%H", // 14; Hour in 24h format (00-23), (same as %k)
   248  	stdZeroHour12:            "%I", // 02; Hour in 12h format (01-12), (same as %l)
   249  	stdDayOfYear:             "%j", // 235; Day of the year (001-366)
   250  	stdZeroMonth:             "%m", // 08; Month as a decimal number (01-12)
   251  	stdZeroMinute:            "%M", // 55; Minute (00-59)
   252  	stdCharNewLine:           "%n", // '\n'; New-line character ('\n')
   253  	stdPM:                    "%p", // PM; AM or PM designation
   254  	stdpm:                    "%P", // om; am or pm designation
   255  	stdHour12ClockTime:       "%r", // 02:55:02 pm; 12-hour clock time
   256  	stdHourHourMinuteTime:    "%R", // 14:55; 24-hour HH:MM time, equivalent to %H:%M
   257  	stdSecondsSinceEpoch:     "%s", // ; The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
   258  	stdZeroSecond:            "%S", // 02; Second (00-61)
   259  	stdCharHorizontalTab:     "%t", // '\t'; Horizontal-tab character ('\t')
   260  	stdISO8601Time:           "%T", // 14:55:02; ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
   261  	stdNumWeekDay:            "%u", // 4; ISO 8601 weekday as number with Monday as 1 (1-7)
   262  	stdSundayFirstWeekOfYear: "%U", // 33; Week number with the first Sunday as the first day of week one (00-53)
   263  	stdISO8601NumWeek:        "%v", // 34; ISO 8601 week number (01-53)
   264  	stdISO8601Week:           "%V", // 34; ISO 8601 week number (01-53)
   265  	stdZeroNumWeek:           "%w", // 4; Weekday as a decimal number with Sunday as 0 (0-6)
   266  	stdMonFirstWeekOfYear:    "%W", // 34; Week number with the first Monday as the first day of week one (00-53)
   267  	stdHourClockTime:         "%X", // 14:55:02; Time representation
   268  	stdYear:                  "%y", // 01; Year, last two digits (00-99)
   269  	stdLongYear:              "%Y", // 2001; Year
   270  	stdNumTZ:                 "%z", // +100; ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100), If timezone cannot be determined, no characters
   271  	stdTZ:                    "%Z", // CDT; Timezone name or abbreviation, If timezone cannot be determined, no characters
   272  	stdCharPercentSign:       "%%", // '%'; A % sign
   273  	stdISO8601TZ:             "%z",
   274  	stdNop:                   "",
   275  }
   276  
   277  var strftimeSimilarChunkNames = map[int]string{
   278  	stdNumMonth:                                 strftimeChunkNames[stdMonth],
   279  	stdDay:                                      strftimeChunkNames[stdZeroDay],
   280  	stdUnderYearDay:                             strftimeChunkNames[stdZeroDay],
   281  	stdZeroYearDay:                              strftimeChunkNames[stdDayOfYear],
   282  	stdHour12:                                   strftimeChunkNames[stdZeroHour12],
   283  	stdMinute:                                   strftimeChunkNames[stdZeroMinute],
   284  	stdSecond:                                   strftimeChunkNames[stdZeroSecond],
   285  	stdISO8601SecondsTZ:                         strftimeChunkNames[stdNumTZ],
   286  	stdISO8601ShortTZ:                           strftimeChunkNames[stdNumTZ],
   287  	stdISO8601ColonTZ:                           strftimeChunkNames[stdNumTZ],
   288  	stdISO8601ColonSecondsTZ:                    strftimeChunkNames[stdNumTZ],
   289  	stdNumSecondsTz:                             strftimeChunkNames[stdNumTZ],
   290  	stdNumShortTZ:                               strftimeChunkNames[stdNumTZ],
   291  	stdNumColonTZ:                               strftimeChunkNames[stdNumTZ],
   292  	stdNumColonSecondsTZ:                        strftimeChunkNames[stdNumTZ],
   293  	stdDateAndTime | stdNeedEModifier:           "%Ec",
   294  	stdFirstTwoDigitYear | stdNeedEModifier:     "%EC",
   295  	stdShortSlashDate | stdNeedEModifier:        "%Ex",
   296  	stdHourClockTime | stdNeedEModifier:         "%EX",
   297  	stdYear | stdNeedEModifier:                  "%Ey",
   298  	stdLongYear | stdNeedEModifier:              "%EY",
   299  	stdZeroDay | stdNeedOModifier:               "%Od",
   300  	stdUnderDay | stdNeedOModifier:              "%Oe",
   301  	stdHour | stdNeedOModifier:                  "%OH",
   302  	stdZeroHour12 | stdNeedOModifier:            "%OI",
   303  	stdZeroMonth | stdNeedOModifier:             "%Om",
   304  	stdZeroMinute | stdNeedOModifier:            "%OM",
   305  	stdZeroSecond | stdNeedOModifier:            "%OS",
   306  	stdNumWeekDay | stdNeedOModifier:            "%Ou",
   307  	stdSundayFirstWeekOfYear | stdNeedOModifier: "%OU",
   308  	stdISO8601Week | stdNeedOModifier:           "%OV",
   309  	stdZeroNumWeek | stdNeedOModifier:           "%Ow",
   310  	stdMonFirstWeekOfYear | stdNeedOModifier:    "%OW",
   311  	stdYear | stdNeedOModifier:                  "%Oy",
   312  	stdFracSecond0 | 1<<stdArgShift:             "",
   313  	stdFracSecond0 | 2<<stdArgShift:             "",
   314  	stdFracSecond0 | 3<<stdArgShift:             "",
   315  	stdFracSecond0 | 4<<stdArgShift:             "",
   316  	stdFracSecond0 | 5<<stdArgShift:             "",
   317  	stdFracSecond0 | 6<<stdArgShift:             "",
   318  	stdFracSecond0 | 7<<stdArgShift:             "",
   319  	stdFracSecond0 | 8<<stdArgShift:             "",
   320  	stdFracSecond0 | 9<<stdArgShift:             "",
   321  	stdFracSecond9 | 1<<stdArgShift:             "",
   322  	stdFracSecond9 | 2<<stdArgShift:             "",
   323  	stdFracSecond9 | 3<<stdArgShift:             "",
   324  	stdFracSecond9 | 4<<stdArgShift:             "",
   325  	stdFracSecond9 | 5<<stdArgShift:             "",
   326  	stdFracSecond9 | 6<<stdArgShift:             "",
   327  	stdFracSecond9 | 7<<stdArgShift:             "",
   328  	stdFracSecond9 | 8<<stdArgShift:             "",
   329  	stdFracSecond9 | 9<<stdArgShift:             "",
   330  	stdNop:                                      "",
   331  }