github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/time_private.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekatime
     7  
     8  import (
     9  	"fmt"
    10  	"time"
    11  
    12  	"github.com/modern-go/reflect2"
    13  )
    14  
    15  //noinspection GoSnakeCaseUsage
    16  const (
    17  	_TIME_OFFSET_HOUR   uint8 = 0
    18  	_TIME_OFFSET_MINUTE uint8 = _TIME_OFFSET_HOUR + 5
    19  	_TIME_OFFSET_SECOND uint8 = _TIME_OFFSET_MINUTE + 6
    20  	_TIME_OFFSET_UNUSED uint8 = _TIME_OFFSET_SECOND + 6
    21  
    22  	_TIME_MASK_HOUR   Hour   = 0x1F
    23  	_TIME_MASK_MINUTE Minute = 0x3F
    24  	_TIME_MASK_SECOND Second = 0x3F
    25  
    26  	_TIME_MASK_TIME Time = (Time(1) << _TIME_OFFSET_UNUSED) - 1
    27  )
    28  
    29  //noinspection GoSnakeCaseUsage
    30  var (
    31  	_TIME_PART_AS_NUM_STR [60][]byte
    32  )
    33  
    34  // normalizeTime shifts Time, 'h', 'm' and 's' represents which if they are not
    35  // in their valid ranges. Returns the fixed values (if they has been).
    36  func normalizeTime(h Hour, m Minute, s Second) (Hour, Minute, Second) {
    37  	if !IsValidTime(h, m, s) {
    38  		t := time.Date(0, 0, 0, int(h), int(m), int(s), 0, time.UTC)
    39  		th, tm, ts := t.Clock()
    40  		h, m, s = Hour(th), Minute(tm), Second(ts)
    41  	}
    42  	return h, m, s
    43  }
    44  
    45  // initTimeNumStr initializes _TIME_PART_AS_NUM_STR,
    46  // that are used at the Time.String(), Time.AppendValue()
    47  // and other methods to get a string (or []byte) representation of Time.
    48  func initTimeNumStr() {
    49  	for i := 0; i <= 59; i++ {
    50  		_TIME_PART_AS_NUM_STR[i] =
    51  			reflect2.UnsafeCastString(fmt.Sprintf("%02d", i))
    52  	}
    53  }