// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name byear.go // Created by DEV // Modified 21/02/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package bdates import "times" type ( BYear struct { atts BYearAtt first byte year byte months [12]BMonth } ) func NewBYear (year int) *BYear { if year < minBYear || year > maxBYear { panic(G.ErrInvalidYear) } at := BYearAtt(BYearAttSet) if times.IsYearLeap(year) { at|=BYearAttLeap } return &BYear { atts : at, first: byte(times.DayOfWeek(year,1,1)), year : byte(year-1900), } } func (y *BYear) Atts () BYearAtt { return y.atts } func (y *BYear) First () int { return int(y.first) } func (y *BYear) Year () int { return minBYear + int(y.year) } func (y *BYear) Clear (month ...int) { if len(month) == 0 { for i := 0; i < 12; i++ { y.months[i] = 0 } return } for _,i := range month { if !checkMonthNumber(i){ panic(G.ErrInvalidMonth) } y.months[i]=0 } } func (y *BYear) GetDay (month, day int) bool { if !checkMonthNumber(month) { panic(G.ErrInvalidMonth ) } if day < 1 || day > y.daysInMonth(month) { panic(G.ErrInvalidDay) } return (uint32(1 << day) & uint32(y.months[month])) != 0 } func (y *BYear) SetDay (month, day int, active bool) { if !checkMonthNumber(month) { panic(G.ErrInvalidMonth ) } if day < 1 || day > y.daysInMonth(month) { panic(G.ErrInvalidDay) } month-=1 if active { y.months[month] = y.months[month] | BMonth(1 << day) return } y.months[month] = y.months[month] &^ BMonth(1 << day) } func (y *BYear) GetMonth (month int) BMonth { if !checkMonthNumber(month) { panic(G.ErrInvalidMonth) } return y.months[month-1] } func (y BYear) daysInMonth (month int) int { d := [12]int{ 31,28,31,30,31,30,31,30,31,30,31 } if month == 2 && y.Atts().IsLeap() { return 29 } return d[month-1] }