...

Source file src/github.com/rs/zerolog/internal/json/time.go

Documentation: github.com/rs/zerolog/internal/json

     1  package json
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  )
     7  
     8  const (
     9  	// Import from zerolog/global.go
    10  	timeFormatUnix      = ""
    11  	timeFormatUnixMs    = "UNIXMS"
    12  	timeFormatUnixMicro = "UNIXMICRO"
    13  	timeFormatUnixNano  = "UNIXNANO"
    14  )
    15  
    16  // AppendTime formats the input time with the given format
    17  // and appends the encoded string to the input byte slice.
    18  func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
    19  	switch format {
    20  	case timeFormatUnix:
    21  		return e.AppendInt64(dst, t.Unix())
    22  	case timeFormatUnixMs:
    23  		return e.AppendInt64(dst, t.UnixNano()/1000000)
    24  	case timeFormatUnixMicro:
    25  		return e.AppendInt64(dst, t.UnixNano()/1000)
    26  	case timeFormatUnixNano:
    27  		return e.AppendInt64(dst, t.UnixNano())
    28  	}
    29  	return append(t.AppendFormat(append(dst, '"'), format), '"')
    30  }
    31  
    32  // AppendTimes converts the input times with the given format
    33  // and appends the encoded string list to the input byte slice.
    34  func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
    35  	switch format {
    36  	case timeFormatUnix:
    37  		return appendUnixTimes(dst, vals)
    38  	case timeFormatUnixMs:
    39  		return appendUnixNanoTimes(dst, vals, 1000000)
    40  	case timeFormatUnixMicro:
    41  		return appendUnixNanoTimes(dst, vals, 1000)
    42  	case timeFormatUnixNano:
    43  		return appendUnixNanoTimes(dst, vals, 1)
    44  	}
    45  	if len(vals) == 0 {
    46  		return append(dst, '[', ']')
    47  	}
    48  	dst = append(dst, '[')
    49  	dst = append(vals[0].AppendFormat(append(dst, '"'), format), '"')
    50  	if len(vals) > 1 {
    51  		for _, t := range vals[1:] {
    52  			dst = append(t.AppendFormat(append(dst, ',', '"'), format), '"')
    53  		}
    54  	}
    55  	dst = append(dst, ']')
    56  	return dst
    57  }
    58  
    59  func appendUnixTimes(dst []byte, vals []time.Time) []byte {
    60  	if len(vals) == 0 {
    61  		return append(dst, '[', ']')
    62  	}
    63  	dst = append(dst, '[')
    64  	dst = strconv.AppendInt(dst, vals[0].Unix(), 10)
    65  	if len(vals) > 1 {
    66  		for _, t := range vals[1:] {
    67  			dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10)
    68  		}
    69  	}
    70  	dst = append(dst, ']')
    71  	return dst
    72  }
    73  
    74  func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
    75  	if len(vals) == 0 {
    76  		return append(dst, '[', ']')
    77  	}
    78  	dst = append(dst, '[')
    79  	dst = strconv.AppendInt(dst, vals[0].UnixNano()/div, 10)
    80  	if len(vals) > 1 {
    81  		for _, t := range vals[1:] {
    82  			dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/div, 10)
    83  		}
    84  	}
    85  	dst = append(dst, ']')
    86  	return dst
    87  }
    88  
    89  // AppendDuration formats the input duration with the given unit & format
    90  // and appends the encoded string to the input byte slice.
    91  func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
    92  	if useInt {
    93  		return strconv.AppendInt(dst, int64(d/unit), 10)
    94  	}
    95  	return e.AppendFloat64(dst, float64(d)/float64(unit))
    96  }
    97  
    98  // AppendDurations formats the input durations with the given unit & format
    99  // and appends the encoded string list to the input byte slice.
   100  func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
   101  	if len(vals) == 0 {
   102  		return append(dst, '[', ']')
   103  	}
   104  	dst = append(dst, '[')
   105  	dst = e.AppendDuration(dst, vals[0], unit, useInt)
   106  	if len(vals) > 1 {
   107  		for _, d := range vals[1:] {
   108  			dst = e.AppendDuration(append(dst, ','), d, unit, useInt)
   109  		}
   110  	}
   111  	dst = append(dst, ']')
   112  	return dst
   113  }
   114  

View as plain text