code.vegaprotocol.io/vega@v0.79.0/datanode/entities/date_range.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package entities
    17  
    18  import (
    19  	"errors"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/libs/ptr"
    23  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  )
    25  
    26  type DateRange struct {
    27  	Start *time.Time
    28  	End   *time.Time
    29  }
    30  
    31  var (
    32  	ErrDateRangeIsRequired = errors.New("date range is required")
    33  	ErrMinimumDate         = errors.New("date range start must be after 2020-01-01")
    34  	ErrEndDateBeforeStart  = errors.New("date range start must be before end")
    35  	ErrDateRangeTooLong    = errors.New("date range is too long")
    36  	minimumDate            = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
    37  	maximumDuration        = time.Hour * 24 * 365 // 1 year maximum duration
    38  )
    39  
    40  func DateRangeFromProto(dateRangeInput *v2.DateRange) (dateRange DateRange) {
    41  	if dateRangeInput == nil {
    42  		return
    43  	}
    44  
    45  	if dateRangeInput.StartTimestamp != nil {
    46  		dateRange.Start = ptr.From(time.Unix(0, *dateRangeInput.StartTimestamp))
    47  	}
    48  
    49  	if dateRangeInput.EndTimestamp != nil {
    50  		dateRange.End = ptr.From(time.Unix(0, *dateRangeInput.EndTimestamp))
    51  	}
    52  
    53  	return
    54  }
    55  
    56  func (dr DateRange) Validate(required bool) error {
    57  	if !required && dr.Start == nil && dr.End == nil {
    58  		return nil
    59  	}
    60  
    61  	if required && dr.Start == nil && dr.End == nil {
    62  		return ErrDateRangeIsRequired
    63  	}
    64  
    65  	if dr.Start != nil && dr.Start.Before(minimumDate) {
    66  		return ErrMinimumDate
    67  	}
    68  
    69  	if dr.End != nil && dr.End.Before(minimumDate) {
    70  		return ErrMinimumDate
    71  	}
    72  
    73  	if dr.Start != nil && dr.End != nil && dr.Start.After(*dr.End) {
    74  		return ErrEndDateBeforeStart
    75  	}
    76  
    77  	end := time.Now()
    78  	if dr.End != nil {
    79  		end = *dr.End
    80  	}
    81  
    82  	start := minimumDate
    83  	if dr.Start != nil {
    84  		start = *dr.Start
    85  	}
    86  
    87  	if end.Sub(start) > maximumDuration {
    88  		return ErrDateRangeTooLong
    89  	}
    90  
    91  	return nil
    92  }