github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/fmtstr/format_timestamp.go (about)

     1  // Copyright 2023 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fmtstr
    16  
    17  import (
    18  	"sort"
    19  	"strconv"
    20  	"strings"
    21  	"time"
    22  )
    23  
    24  func newTimestampFormatMapping(goFormat, generalFormat string) *TimestampFormatMapping {
    25  	return &TimestampFormatMapping{
    26  		GoFormat:      goFormat,
    27  		GeneralFormat: generalFormat,
    28  	}
    29  }
    30  
    31  type TimestampFormatMapping struct {
    32  	GoFormat      string
    33  	GeneralFormat string
    34  }
    35  
    36  func newTimestampFormatMappings() []*TimestampFormatMapping {
    37  	return []*TimestampFormatMapping{
    38  		newTimestampFormatMapping("January", "MMMM"),
    39  		newTimestampFormatMapping("Jan", "MMM"),
    40  		newTimestampFormatMapping("1", "M"),
    41  		newTimestampFormatMapping("01", "MM"),
    42  		newTimestampFormatMapping("Monday", "EEEE"),
    43  		newTimestampFormatMapping("Mon", "EEE"),
    44  		newTimestampFormatMapping("2", "d"),
    45  		newTimestampFormatMapping("02", "dd"),
    46  		newTimestampFormatMapping("15", "HH"),
    47  		newTimestampFormatMapping("3", "K"),
    48  		newTimestampFormatMapping("03", "KK"),
    49  		newTimestampFormatMapping("4", "m"),
    50  		newTimestampFormatMapping("04", "mm"),
    51  		newTimestampFormatMapping("5", "s"),
    52  		newTimestampFormatMapping("05", "ss"),
    53  		newTimestampFormatMapping("2006", "yyyy"),
    54  		newTimestampFormatMapping("06", "yy"),
    55  		newTimestampFormatMapping("PM", "aa"),
    56  		newTimestampFormatMapping("MST", "Z"),
    57  		newTimestampFormatMapping("Z0700", "'Z'XX"),
    58  		newTimestampFormatMapping("Z070000", "'Z'XX"),
    59  		newTimestampFormatMapping("Z07", "'Z'X"),
    60  		newTimestampFormatMapping("Z07:00", "'Z'XXX"),
    61  		newTimestampFormatMapping("Z07:00:00", "'Z'XXX"),
    62  		newTimestampFormatMapping("-0700", "XX"),
    63  		newTimestampFormatMapping("-07", "X"),
    64  		newTimestampFormatMapping("-07:00", "XXX"),
    65  		newTimestampFormatMapping("999999999", "SSS"),
    66  	}
    67  }
    68  
    69  func FormatTimestamp(t *time.Time, format string) string {
    70  	mappings := newTimestampFormatMappings()
    71  	goFormat := GeneralToGoFormat(mappings, format)
    72  	// format with week
    73  	if strings.Contains(goFormat, "ww") {
    74  		weekNumber := GetWeek(t)
    75  		formattedTime := t.Format(goFormat)
    76  		return strings.ReplaceAll(formattedTime, "ww", strconv.Itoa(weekNumber))
    77  	}
    78  	return t.Format(goFormat)
    79  }
    80  
    81  func GetWeek(t *time.Time) int {
    82  	_, week := t.ISOWeek()
    83  	return week
    84  }
    85  
    86  func GeneralToGoFormat(mappings []*TimestampFormatMapping, format string) string {
    87  	// After sorting, avoid replacement errors when using replace later
    88  	sort.Slice(mappings, func(i, j int) bool {
    89  		return len(mappings[i].GeneralFormat) > len(mappings[j].GeneralFormat)
    90  	})
    91  	for _, m := range mappings {
    92  		if m.GeneralFormat == "M" && (strings.Contains(format, "Mon") ||
    93  			strings.Contains(format, "AM") || strings.Contains(format, "PM")) {
    94  			continue
    95  		}
    96  		if m.GeneralFormat == "d" && strings.Contains(format, "Monday") {
    97  			continue
    98  		}
    99  		format = strings.ReplaceAll(format, m.GeneralFormat, m.GoFormat)
   100  	}
   101  	return format
   102  }