github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/weekday/weekday.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package weekday
    16  
    17  import "github.com/matrixorigin/matrixone/pkg/container/types"
    18  
    19  // vectorize weekday function
    20  var (
    21  	DateToWeekday     func([]types.Date, []int64) []int64
    22  	DatetimeToWeekday func([]types.Datetime, []int64) []int64
    23  )
    24  
    25  func init() {
    26  	DateToWeekday = dateToWeekday
    27  	DatetimeToWeekday = datetimeToWeekday
    28  }
    29  
    30  // Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday)
    31  func weekdayToIndex(weekday types.Weekday) int64 {
    32  	return int64((weekday + 6) % 7)
    33  }
    34  
    35  func dateToWeekday(xs []types.Date, rs []int64) []int64 {
    36  	for i, x := range xs {
    37  		rs[i] = weekdayToIndex(x.DayOfWeek())
    38  	}
    39  	return rs
    40  }
    41  
    42  func datetimeToWeekday(xs []types.Datetime, rs []int64) []int64 {
    43  	for i, x := range xs {
    44  		rs[i] = weekdayToIndex(x.ToDate().DayOfWeek())
    45  	}
    46  	return rs
    47  }