code.vegaprotocol.io/vega@v0.79.0/datanode/vegatime/time.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 vegatime
    17  
    18  import (
    19  	"time"
    20  
    21  	types "code.vegaprotocol.io/vega/protos/vega"
    22  )
    23  
    24  var now func() time.Time
    25  
    26  func init() {
    27  	now = time.Now
    28  }
    29  
    30  // SetNowFunc exists for testing purpose
    31  // e.g: set the vegatime.Now function to return a specific time
    32  //
    33  //	vegatime.SetNowFunc(func() time.T { vegatime.Unix(123423, 0) })
    34  //
    35  // reset the vegatime.Now function
    36  //
    37  //	vegatime.SetNowFunc(nil)
    38  //
    39  // this will reset the vegatime.Now function to use time.Now() again.
    40  func SetNowFunc(f func() time.Time) {
    41  	if f == nil {
    42  		now = time.Now
    43  	} else {
    44  		now = f
    45  	}
    46  }
    47  
    48  // Now return the current time in UTC timezone.
    49  func Now() time.Time {
    50  	return now().UTC()
    51  }
    52  
    53  // Unix create a new time from sec and nsec in UTC timezone.
    54  func Unix(sec int64, nsec int64) time.Time {
    55  	return time.Unix(sec, nsec).UTC()
    56  }
    57  
    58  // UnixNano equivalent to time.Unix(sec, nsec) but to be used with the
    59  // result of time.Time{}.UnixNano() in UTC timezone.
    60  func UnixNano(nsec int64) time.Time {
    61  	return time.Unix(nsec/int64(time.Second), nsec%int64(time.Second)).UTC()
    62  }
    63  
    64  // Parse parse a string expected to be a time in the time.RFC3339Nano format.
    65  func Parse(t string) (time.Time, error) {
    66  	return time.ParseInLocation(time.RFC3339Nano, t, time.UTC)
    67  }
    68  
    69  // Format format the time using the time.RFC3339Nano formatting.
    70  func Format(t time.Time) string {
    71  	return t.In(time.UTC).Format(time.RFC3339Nano)
    72  }
    73  
    74  // RoundToNearest round an actual time to the nearest interval.
    75  func RoundToNearest(t time.Time, interval types.Interval) time.Time {
    76  	switch interval {
    77  	case types.Interval_INTERVAL_I1M:
    78  		return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, time.UTC)
    79  	case types.Interval_INTERVAL_I5M:
    80  		return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), (t.Minute()/5)*5, 0, 0, time.UTC)
    81  	case types.Interval_INTERVAL_I15M:
    82  		return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), (t.Minute()/15)*15, 0, 0, time.UTC)
    83  	case types.Interval_INTERVAL_I1H:
    84  		return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, time.UTC)
    85  	case types.Interval_INTERVAL_I6H:
    86  		return time.Date(t.Year(), t.Month(), t.Day(), (t.Hour()/6)*6, 0, 0, 0, time.UTC)
    87  	case types.Interval_INTERVAL_I1D:
    88  		return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
    89  	default:
    90  		return t
    91  	}
    92  }