Skip to content

Commit

Permalink
🎨 add zero time handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iChemy committed Jun 24, 2024
1 parent 68fa95b commit e80bc38
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,29 @@ func AddAnd(lhs, rhs Expr) Expr {
// * 期間中に始まる (time_start >= since AND time_start < until)
// * 期間中に終わる (time_end >= since AND time_end < until)
// * 期間より前に始まり期間より後に終わる (time_start < since AND time_end >= until)
//
// ただしsinceがゼロのとき time_start <= until
// untilがゼロのとき since <= time_end
// 双方がゼロのき nil を返す
func FilterDuration(since, until time.Time) (Expr, error) {
if !since.IsZero() && !until.IsZero() {
if since.After(until) {
return nil, errors.New("invalid time range")
}
if since.IsZero() && until.IsZero() {
return nil, nil
} else if since.IsZero() {
return &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: until,
}, nil
} else if until.IsZero() {
return &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: since,
}, nil
}

if since.After(until) {
return nil, errors.New("invalid time range")
}

// 期間中に始まる
Expand Down Expand Up @@ -136,21 +154,19 @@ func FilterDuration(since, until time.Time) (Expr, error) {
},
}

startBeforeMin := &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: since,
}
endAfterMax := &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: until,
}
// 期間より前に始まり期間より後に終わる
throughout := &LogicOpExpr{
LogicOp: And,
Lhs: startBeforeMin,
Rhs: endAfterMax,
Lhs: &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: since,
},
Rhs: &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: until,
},
}

return &LogicOpExpr{
Expand Down

0 comments on commit e80bc38

Please sign in to comment.