github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/local_time.go (about)

     1  package chrono
     2  
     3  // LocalTime is a time without a time zone or date component.
     4  // It represents a time within the 24-hour clock system with nanosecond precision, according to ISO 8601.
     5  //
     6  // Additional flexibility is provided whereby times after 23:59:59.999999999 are also considered valid.
     7  // This feature supports various usecases where times such as 25:00 (instead of 01:00) represent
     8  // business hours that extend beyond midnight. LocalTime supports a maximum hour of 99.
     9  type LocalTime struct {
    10  	v int64
    11  }
    12  
    13  // LocalTimeOf returns a LocalTime that represents the specified hour, minute, second, and nanosecond offset within the specified second.
    14  // A valid time is between 00:00:00 and 99:59:59.999999999. If an invalid time is specified, this function panics.
    15  func LocalTimeOf(hour, min, sec, nsec int) LocalTime {
    16  	out, err := makeTime(hour, min, sec, nsec)
    17  	if err != nil {
    18  		panic(err.Error())
    19  	}
    20  	return LocalTime{v: out}
    21  }
    22  
    23  // BusinessHour returns the hour specified by t.
    24  // If the hour is greater than 23, that hour is returned without normalization.
    25  func (t LocalTime) BusinessHour() int {
    26  	return timeBusinessHour(t.v)
    27  }
    28  
    29  // Clock returns the hour, minute and second represented by t.
    30  // If hour is greater than 23, the returned value is normalized so as to fit within
    31  // the 24-hour clock as specified by ISO 8601, e.g. 25 is returned as 01.
    32  func (t LocalTime) Clock() (hour, min, sec int) {
    33  	hour, min, sec, _ = fromTime(t.v)
    34  	return
    35  }
    36  
    37  // Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
    38  func (t LocalTime) Nanosecond() int {
    39  	return timeNanoseconds(t.v)
    40  }
    41  
    42  // Sub returns the duration t-u.
    43  func (t LocalTime) Sub(u LocalTime) Extent {
    44  	return Extent(t.v - u.v)
    45  }
    46  
    47  // Add returns the time t+v. If the result exceeds the maximum representable time, this function panics.
    48  // If the result would be earlier than 00:00:00, the returned time is moved to the previous day,
    49  // e.g. 01:00:00 - 2 hours = 23:00:00.
    50  func (t LocalTime) Add(v Extent) LocalTime {
    51  	out, err := addTime(t.v, int64(v))
    52  	if err != nil {
    53  		panic(err.Error())
    54  	}
    55  	return LocalTime{v: out}
    56  }
    57  
    58  // CanAdd returns false if Add would panic if passed the same argument.
    59  func (t LocalTime) CanAdd(v Extent) bool {
    60  	_, err := addTime(t.v, int64(v))
    61  	return err == nil
    62  }
    63  
    64  // Compare compares t with t2. If t is before t2, it returns -1;
    65  // if t is after t2, it returns 1; if they're the same, it returns 0.
    66  func (t LocalTime) Compare(t2 LocalTime) int {
    67  	return compareTimes(t.v, t2.v)
    68  }
    69  
    70  func (t LocalTime) String() string {
    71  	hour, min, sec, nsec := fromTime(t.v)
    72  	return simpleTimeStr(hour, min, sec, nsec, nil)
    73  }
    74  
    75  // In returns the OffsetTime represeting t with the specified offset.
    76  func (t LocalTime) In(offset Offset) OffsetTime {
    77  	return OffsetTime{v: t.v, o: int64(offset)}
    78  }
    79  
    80  // UTC returns the OffsetTime represeting t at the UTC offset.
    81  func (t LocalTime) UTC() OffsetTime {
    82  	return OffsetTime{v: t.v}
    83  }
    84  
    85  // Format returns a textual representation of the time value formatted according to the layout defined by the argument.
    86  // See the constants section of the documentation to see how to represent the layout format.
    87  // Date format specifiers encountered in the layout results in a panic.
    88  func (t LocalTime) Format(layout string) string {
    89  	out, err := formatDateTimeOffset(layout, nil, &t.v, nil)
    90  	if err != nil {
    91  		panic(err.Error())
    92  	}
    93  	return out
    94  }
    95  
    96  // Parse a formatted string and store the value it represents in t.
    97  // See the constants section of the documentation to see how to represent the layout format.
    98  // Date format specifiers encountered in the layout results in a panic.
    99  func (t *LocalTime) Parse(layout, value string) error {
   100  	v := t.v
   101  	if err := parseDateAndTime(layout, value, nil, &v, nil); err != nil {
   102  		return err
   103  	}
   104  
   105  	t.v = v
   106  	return nil
   107  }