github.com/blend/go-sdk@v1.20220411.3/testutil/time.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package testutil
     9  
    10  import (
    11  	"time"
    12  )
    13  
    14  type assertions interface {
    15  	Nil(interface{}, ...interface{}) bool
    16  }
    17  
    18  // NowRounded returns the current time, but rounded to a given precision and
    19  // then placed into a timezone given by a location name from the IANA Time Zone
    20  // database (or "Local").
    21  //
    22  // This is useful in situations where timestamps are written to and then read
    23  // back from a foreign system, like a database. For example, a `TIMESTAMP WITH TIME ZONE`
    24  // column in `postgres` will truncate to microsecond precision and will return
    25  // a "bare" timezone even if the timezone written was UTC.
    26  func NowRounded(it assertions, locationName string, precision time.Duration) time.Time {
    27  	loc, err := time.LoadLocation(locationName)
    28  	it.Nil(err)
    29  	// Round to the nearest `precision` (e.g. microsecond) to ensure accuracy
    30  	// across Go / PostgreSQL boundaries and across different platforms.
    31  	return time.Now().UTC().Truncate(precision).In(loc)
    32  }