diff --git a/pkg/sql/logictest/testdata/logic_test/datetime b/pkg/sql/logictest/testdata/logic_test/datetime index 5faa5ed51482..55955e782a79 100644 --- a/pkg/sql/logictest/testdata/logic_test/datetime +++ b/pkg/sql/logictest/testdata/logic_test/datetime @@ -2064,6 +2064,18 @@ SELECT make_timestamp(2013, 7, 15, 8, 15, 23.5231231244234)::string make_timestamp 2013-07-15 08:15:23.523123 +# Test with seconds < 1. + +query T +select make_timestamp(1, 1, 1, 0, 0, 0); +---- +0001-01-01 00:00:00 +0000 +0000 + +query T +select make_timestamp(1, 1, 1, 0, 0, 0.1234); +---- +0001-01-01 00:00:00.1234 +0000 +0000 + statement error pgcode 22008 pq: make_timestamp\(\): year value of 0 is not valid SELECT make_timestamp(0, 7, 15, 8, 15, 23.5); @@ -2098,6 +2110,18 @@ SELECT make_timestamptz(2013, 7, 15, 8, 15, 23.5, 'America/New_York') make_timestamptz 2013-07-15 07:15:23.5 -0500 EST +# Test with seconds < 1. + +query T +select make_timestamptz(2020, 1, 1, 0, 0, 0, 'America/New_York'); +---- +2020-01-01 00:00:00 -0500 EST + +query T +select make_timestamptz(2020, 1, 1, 0, 0, 0.1234, 'America/New_York'); +---- +2020-01-01 00:00:00.1234 -0500 EST + statement error pgcode 22008 pq: make_timestamptz\(\): year value of 0 is not valid SELECT make_timestamptz(0, 7, 15, 8, 15, 23.5); diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go index d57524e2e996..db3ece9e7e7b 100644 --- a/pkg/sql/sem/builtins/builtins.go +++ b/pkg/sql/sem/builtins/builtins.go @@ -11998,8 +11998,8 @@ func makeTimestampStatementBuiltinOverload(withOutputTZ bool, withInputTZ bool) hour := int(tree.MustBeDInt(args[3])) min := int(tree.MustBeDInt(args[4])) sec := float64(tree.MustBeDFloat(args[5])) - truncatedSec := math.Floor(sec) - nsec := math.Mod(sec, truncatedSec) * float64(time.Second) + truncatedSec, remainderSec := math.Modf(sec) + nsec := remainderSec * float64(time.Second) t := time.Date(year, month, day, hour, min, int(truncatedSec), int(nsec), location) if withOutputTZ { return tree.MakeDTimestampTZ(t, time.Microsecond)