From 82cfcc7ce27279787839da0e9ec384a13a7cb1e2 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 23 Oct 2026 18:06:28 -0600 Subject: [PATCH] Added birthdays for the week/day derived from month/day only. Not tested with leap year birthdays (and probably won't work for obvious reasons). This query is a bit crazy. --- internal/models/birthdays.go | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/models/birthdays.go b/internal/models/birthdays.go index 0644e52..2e63545 100644 --- a/internal/models/birthdays.go +++ b/internal/models/birthdays.go @@ -2,6 +2,7 @@ package models import ( "context" + "time" "github.com/jmoiron/sqlx" "github.com/mattn/go-sqlite3" @@ -16,6 +17,9 @@ type Birthday struct { Month int `db:"month"` Day int `db:"day"` Delta int `db:"leap_delta"` + + // Birthdate; computed field. + Birthdate *time.Time `db:"birthdate"` } type BirthdayModel struct { @@ -51,5 +55,44 @@ func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, da return nil, errors.NewError("not implemented") } -//func (bm *BirthdayModel) ListBirthdays +func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) { + var bdays []*Birthday + q := `select + id, userid, chatid, name, leap_delta, + date(concat(strftime('%Y', date('now')), '-', + format('%02d', month), '-', + format('%02d', day))) as birthdate + from birthdays where + chatid = ? and ( + birthdate >= date('now', 'weekday 0') and + birthdate <= date('now', 'weekday 0', '+7 days'));` + if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil { + return nil, err + } + + return bdays, nil +} + +func (bm *BirthdayModel) BirthdaysToday(chatid int64) ([]*Birthday, error) { + var bdays []*Birthday + q := `select + id, userid, chatid, name, leap_delta, + date(concat(strftime('%Y', date('now')), '-', + format('%02d', month), '-', + format('%02d', day))) as birthdate + from birthdays where + birthdate == date('now');` + + if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil { + return nil, err + } + + return bdays, nil +} + //month >= cast(strftime('%m', date('now', 'weekday 0')) as integer) and month <= cast(strftime('%m', date(date('now', 'weekday 0'), '+7 days')) as integer) + +/* +select * from birthdays where (day >= cast(strftime('%e', date('now', 'weekday 0')) as integer) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31)) or (day >= min(cast(strftime('%e', date('now', 'weekday 0')) as integer), 1) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31)); + +*/