2023-09-01 19:50:12 +03:00
|
|
|
package dbtime
|
|
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
|
|
// Now returns a standardized timezone used for database resources.
|
|
|
|
|
func Now() time.Time {
|
|
|
|
|
return Time(time.Now().UTC())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Time returns a time compatible with Postgres. Postgres only stores dates with
|
|
|
|
|
// microsecond precision.
|
2024-08-21 14:18:58 +02:00
|
|
|
// FIXME(dannyk): refactor all calls to Time() to expect the input time to be modified to UTC; there are currently a
|
|
|
|
|
//
|
|
|
|
|
// few calls whose behavior would change subtly.
|
|
|
|
|
// See https://github.com/coder/coder/pull/14274#discussion_r1718427461
|
2023-09-01 19:50:12 +03:00
|
|
|
func Time(t time.Time) time.Time {
|
|
|
|
|
return t.Round(time.Microsecond)
|
|
|
|
|
}
|
2025-01-16 17:34:53 +02:00
|
|
|
|
|
|
|
|
// StartOfDay returns the first timestamp of the day of the input timestamp in its location.
|
|
|
|
|
func StartOfDay(t time.Time) time.Time {
|
|
|
|
|
year, month, day := t.Date()
|
|
|
|
|
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
|
|
|
|
}
|
2026-07-02 16:53:36 +01:00
|
|
|
|
|
|
|
|
// StartOfMonth returns the first timestamp of the month of the input timestamp in its location.
|
|
|
|
|
func StartOfMonth(t time.Time) time.Time {
|
|
|
|
|
year, month, _ := t.Date()
|
|
|
|
|
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
|
|
|
|
|
}
|