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

     1  // Copyright © 2020-2021. 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  	"bytes"
    10  )
    11  
    12  var (
    13  	// _WeekdayStr is just English names of days of week.
    14  	_WeekdayStr = [...]string{
    15  		"Unknown",
    16  		"Wednesday",
    17  		"Thursday",
    18  		"Friday",
    19  		"Saturday",
    20  		"Sunday",
    21  		"Monday",
    22  		"Tuesday",
    23  	}
    24  
    25  	_WeekdayBytes = [len(_WeekdayStr)][]byte{}
    26  )
    27  
    28  // asPartOfDate returns current Weekday but as part of Date object
    29  // (casted, bit shifted, ready for being bit added to some Date object).
    30  func (w Weekday) asPartOfDate() Date {
    31  	return Date(w+1) << _DATE_OFFSET_WEEKDAY
    32  }
    33  
    34  // byteSliceEncode returns the current weekday's []byte representation,
    35  // the same as String() returns but double quoted.
    36  func (w Weekday) byteSliceEncode() []byte {
    37  	if w < 0 || w > 6 {
    38  		//noinspection GoAssignmentToReceiver
    39  		w = -1
    40  	}
    41  	return _WeekdayBytes[w+1]
    42  }
    43  
    44  // byteSliceDecode decodes the weekday's value from 'data',
    45  // saving it into the current weekday's object. Always returns nil.
    46  // Saves -1 if 'data' does not contain valid weekday.
    47  func (w *Weekday) byteSliceDecode(data []byte) error {
    48  	if data != nil {
    49  		for i, n := 1, len(_WeekdayBytes); i < n; i++ {
    50  			if bytes.Equal(data, _WeekdayBytes[i]) {
    51  				*w = Weekday(i - 1)
    52  				return nil
    53  			}
    54  		}
    55  	}
    56  	*w = -1
    57  	return nil
    58  }
    59  
    60  // initWeekday fills weekdays []byte representation for fast encoding/decoding.
    61  func initWeekday() {
    62  	for i, n := 0, len(_WeekdayStr); i < n; i++ {
    63  		_WeekdayBytes[i] = []byte("\"" + _WeekdayStr[i] + "\"")
    64  	}
    65  }