github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/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  //go:generate stringer -type=field
    14  
    15  // A field is some piece of information that we can newFieldExtract out of a
    16  // date or time string.
    17  type field uint
    18  
    19  // The field values here are used in index into the fieldExtract.data
    20  // array, otherwise we'd use 1 << iota.
    21  const (
    22  	fieldYear field = iota
    23  	fieldMonth
    24  	fieldDay
    25  	fieldEra
    26  	fieldHour
    27  	fieldMinute
    28  	fieldSecond
    29  	fieldNanos
    30  	fieldMeridian
    31  	fieldTZHour
    32  	fieldTZMinute
    33  	fieldTZSecond
    34  
    35  	fieldMinimum = fieldYear
    36  	fieldMaximum = fieldTZSecond
    37  )
    38  
    39  const (
    40  	fieldValueAM  = -1
    41  	fieldValuePM  = 1
    42  	fieldValueBCE = -1
    43  	fieldValueCE  = 1
    44  )
    45  
    46  // AsSet returns a singleton set containing the field.
    47  func (f field) AsSet() fieldSet {
    48  	return 1 << f
    49  }
    50  
    51  // Pretty wraps the generated String() function to return only the
    52  // name: "Year" vs "fieldYear".
    53  func (f field) Pretty() string {
    54  	return f.String()[5:]
    55  }
    56  
    57  // A fieldSet is an immutable aggregate of fields.
    58  // The zero value is an empty set.
    59  type fieldSet int
    60  
    61  // newFieldSet constructs a fieldSet from zero or more fields.
    62  func newFieldSet(fields ...field) fieldSet {
    63  	var ret fieldSet
    64  	for _, f := range fields {
    65  		ret |= f.AsSet()
    66  	}
    67  	return ret
    68  }
    69  
    70  // Add returns a fieldSet containing the field.
    71  func (s fieldSet) Add(field field) fieldSet {
    72  	return s.AddAll(field.AsSet())
    73  }
    74  
    75  // AddAll returns a fieldSet combining the other fieldSet.
    76  func (s fieldSet) AddAll(other fieldSet) fieldSet {
    77  	return s | other
    78  }
    79  
    80  // Clear removes the field from the set.  It is not an error to
    81  // clear a field which is not set.
    82  func (s fieldSet) Clear(field field) fieldSet {
    83  	return s.ClearAll(field.AsSet())
    84  }
    85  
    86  // ClearAll removes all fields from other in the set.  It is not an
    87  // error to clear fields which are not set.
    88  func (s fieldSet) ClearAll(other fieldSet) fieldSet {
    89  	return s & ^other
    90  }
    91  
    92  // Has returns true if the given field is present in the set.
    93  func (s fieldSet) Has(field field) bool {
    94  	return s&field.AsSet() != 0
    95  }
    96  
    97  // HasAll returns true if the field set contains all of the
    98  // other fields.
    99  func (s fieldSet) HasAll(other fieldSet) bool {
   100  	return s&other == other
   101  }
   102  
   103  // HasAny returns true if the field set contains any of the
   104  // other fields.
   105  func (s fieldSet) HasAny(other fieldSet) bool {
   106  	return s&other != 0
   107  }
   108  
   109  func (s *fieldSet) String() string {
   110  	ret := "[ "
   111  	for f := fieldMinimum; f <= fieldMaximum; f++ {
   112  		if s.Has(f) {
   113  			ret += f.Pretty() + " "
   114  		}
   115  	}
   116  	ret += "]"
   117  	return ret
   118  }