github.com/go-spring/spring-base@v1.1.3/clock/format.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package clock
    18  
    19  import (
    20  	"bytes"
    21  	"time"
    22  )
    23  
    24  const (
    25  	stdNone        = ""
    26  	stdLongYear    = "2006"
    27  	stdYear        = "06"
    28  	stdMonth       = "Jan"
    29  	stdZeroMonth   = "01"
    30  	stdZeroDay     = "02"
    31  	stdZeroYearDay = "002"
    32  	stdHour        = "15"
    33  	stdZeroHour12  = "03"
    34  	stdZeroMinute  = "04"
    35  	stdZeroSecond  = "05"
    36  )
    37  
    38  // Format returns a textual representation of the time value formatted
    39  // according to layout.
    40  func Format(t time.Time, layout string) string {
    41  	layout = ToStdLayout(layout)
    42  	if layout == "" {
    43  		return ""
    44  	}
    45  	return t.Format(layout)
    46  }
    47  
    48  // ToStdLayout converts "yyyy-MM-dd H:m:s" to "2006-01-02 15:04:05".
    49  func ToStdLayout(layout string) string {
    50  	buf := bytes.NewBuffer(nil)
    51  	for layout != "" {
    52  		prefix, std, suffix := nextStdChunk(layout)
    53  		if prefix != "" {
    54  			buf.WriteString(prefix)
    55  		}
    56  		if std != "" {
    57  			buf.WriteString(std)
    58  		}
    59  		layout = suffix
    60  	}
    61  	return buf.String()
    62  }
    63  
    64  func nextStdChunk(layout string) (prefix string, std string, suffix string) {
    65  	for i := 0; i < len(layout); i++ {
    66  		switch b := layout[i]; b {
    67  		case 'y': // yy yyyy
    68  			if len(layout) >= i+4 && layout[i:i+4] == "yyyy" {
    69  				return layout[0:i], stdLongYear, layout[i+4:]
    70  			}
    71  			if len(layout) >= i+2 && layout[i:i+2] == "yy" {
    72  				return layout[0:i], stdYear, layout[i+2:]
    73  			}
    74  		case 'M': // MM MMM
    75  			if len(layout) >= i+3 && layout[i:i+3] == "MMM" {
    76  				return layout[0:i], stdMonth, layout[i+3:]
    77  			}
    78  			if len(layout) >= i+2 && layout[i:i+2] == "MM" {
    79  				return layout[0:i], stdZeroMonth, layout[i+2:]
    80  			}
    81  		case 'd': // dd
    82  			if len(layout) >= i+2 && layout[i:i+2] == "dd" {
    83  				return layout[0:i], stdZeroDay, layout[i+2:]
    84  			}
    85  		case 'D': // d
    86  			if len(layout) >= i+1 && layout[i:i+1] == "D" {
    87  				return layout[0:i], stdZeroYearDay, layout[i+1:]
    88  			}
    89  		case 'H': // H
    90  			if len(layout) >= i+1 && layout[i:i+1] == "H" {
    91  				return layout[0:i], stdHour, layout[i+1:]
    92  			}
    93  		case 'h': // h
    94  			if len(layout) >= i+1 && layout[i:i+1] == "h" {
    95  				return layout[0:i], stdZeroHour12, layout[i+1:]
    96  			}
    97  		case 'm': // m
    98  			if len(layout) >= i+1 && layout[i:i+1] == "m" {
    99  				return layout[0:i], stdZeroMinute, layout[i+1:]
   100  			}
   101  		case 's': // s
   102  			if len(layout) >= i+1 && layout[i:i+1] == "s" {
   103  				return layout[0:i], stdZeroSecond, layout[i+1:]
   104  			}
   105  		}
   106  	}
   107  	return layout, stdNone, ""
   108  }