github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/timeutil/pgdate/fields.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package pgdate
    12  
    13  import "github.com/cockroachdb/redact"
    14  
    15  //go:generate stringer -type=field
    16  
    17  // A field is some piece of information that we can newFieldExtract out of a
    18  // date or time string.
    19  type field uint
    20  
    21  // The field values here are used in index into the fieldExtract.data
    22  // array, otherwise we'd use 1 << iota.
    23  const (
    24  	fieldYear field = iota
    25  	fieldMonth
    26  	fieldDay
    27  	fieldEra
    28  	fieldHour
    29  	fieldMinute
    30  	fieldSecond
    31  	fieldNanos
    32  	fieldMeridian
    33  	fieldTZHour
    34  	fieldTZMinute
    35  	fieldTZSecond
    36  
    37  	fieldMinimum = fieldYear
    38  	fieldMaximum = fieldTZSecond
    39  )
    40  
    41  const (
    42  	fieldValueAM  = -1
    43  	fieldValuePM  = 1
    44  	fieldValueBCE = -1
    45  	fieldValueCE  = 1
    46  )
    47  
    48  // AsSet returns a singleton set containing the field.
    49  func (f field) AsSet() fieldSet {
    50  	return 1 << f
    51  }
    52  
    53  // Pretty wraps the generated String() function to return only the
    54  // name: "Year" vs "fieldYear".
    55  func (f field) Pretty() string {
    56  	return f.String()[5:]
    57  }
    58  
    59  // SafePretty wraps the generated String() function to return only the
    60  // name: "Year" vs "fieldYear" as a RedactableString.
    61  func (f field) SafePretty() redact.RedactableString {
    62  	// Note: this cast is safe because the String() method is generated
    63  	// by the stringer and only returns literal strings.
    64  	return redact.RedactableString(f.String()[5:])
    65  }
    66  
    67  // A fieldSet is an immutable aggregate of fields.
    68  // The zero value is an empty set.
    69  type fieldSet int
    70  
    71  // newFieldSet constructs a fieldSet from zero or more fields.
    72  func newFieldSet(fields ...field) fieldSet {
    73  	var ret fieldSet
    74  	for _, f := range fields {
    75  		ret |= f.AsSet()
    76  	}
    77  	return ret
    78  }
    79  
    80  // Add returns a fieldSet containing the field.
    81  func (s fieldSet) Add(field field) fieldSet {
    82  	return s.AddAll(field.AsSet())
    83  }
    84  
    85  // AddAll returns a fieldSet combining the other fieldSet.
    86  func (s fieldSet) AddAll(other fieldSet) fieldSet {
    87  	return s | other
    88  }
    89  
    90  // Clear removes the field from the set.  It is not an error to
    91  // clear a field which is not set.
    92  func (s fieldSet) Clear(field field) fieldSet {
    93  	return s.ClearAll(field.AsSet())
    94  }
    95  
    96  // ClearAll removes all fields from other in the set.  It is not an
    97  // error to clear fields which are not set.
    98  func (s fieldSet) ClearAll(other fieldSet) fieldSet {
    99  	return s & ^other
   100  }
   101  
   102  // Has returns true if the given field is present in the set.
   103  func (s fieldSet) Has(field field) bool {
   104  	return s&field.AsSet() != 0
   105  }
   106  
   107  // HasAll returns true if the field set contains all of the
   108  // other fields.
   109  func (s fieldSet) HasAll(other fieldSet) bool {
   110  	return s&other == other
   111  }
   112  
   113  // HasAny returns true if the field set contains any of the
   114  // other fields.
   115  func (s fieldSet) HasAny(other fieldSet) bool {
   116  	return s&other != 0
   117  }
   118  
   119  func (s *fieldSet) String() string { return redact.StringWithoutMarkers(s) }
   120  
   121  // SafeFormat implements the redact.SafeFormatter interface.
   122  func (s *fieldSet) SafeFormat(w redact.SafePrinter, _ rune) {
   123  	w.SafeString("[ ")
   124  	for f := fieldMinimum; f <= fieldMaximum; f++ {
   125  		if s.Has(f) {
   126  			w.Printf("%v ", f.SafePretty())
   127  		}
   128  	}
   129  	w.SafeRune(']')
   130  }