Skip to content

Commit 49dccf1

Browse files
ConradIrwinodeke-em
authored andcommitted
time: add Time.Unix{Milli,Micro} and to-Time helpers UnixMicro, UnixMilli
Adds helper functions for users working with other systems which represent time in milliseconds or microseconds since the Unix epoch. Fixes #44196 Change-Id: Ibc4490b52ddec94ebd0c692cb7b52a33e4536759 Reviewed-on: https://go-review.googlesource.com/c/go/+/293349 Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 2de1f42 commit 49dccf1

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/time/time.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,24 @@ func (t Time) Unix() int64 {
11351135
return t.unixSec()
11361136
}
11371137

1138+
// UnixMilli returns t as a Unix time, the number of milliseconds elapsed since
1139+
// January 1, 1970 UTC. The result is undefined if the Unix time in
1140+
// milliseconds cannot be represented by an int64 (a date more than 292 million
1141+
// years before or after 1970). The result does not depend on the
1142+
// location associated with t.
1143+
func (t Time) UnixMilli() int64 {
1144+
return t.unixSec()*1e3 + int64(t.nsec())/1e6
1145+
}
1146+
1147+
// UnixMicro returns t as a Unix time, the number of microseconds elapsed since
1148+
// January 1, 1970 UTC. The result is undefined if the Unix time in
1149+
// microseconds cannot be represented by an int64 (a date before year -290307 or
1150+
// after year 294246). The result does not depend on the location associated
1151+
// with t.
1152+
func (t Time) UnixMicro() int64 {
1153+
return t.unixSec()*1e6 + int64(t.nsec())/1e3
1154+
}
1155+
11381156
// UnixNano returns t as a Unix time, the number of nanoseconds elapsed
11391157
// since January 1, 1970 UTC. The result is undefined if the Unix time
11401158
// in nanoseconds cannot be represented by an int64 (a date before the year
@@ -1309,6 +1327,18 @@ func Unix(sec int64, nsec int64) Time {
13091327
return unixTime(sec, int32(nsec))
13101328
}
13111329

1330+
// UnixMilli returns the local Time corresponding to the given Unix time,
1331+
// msec milliseconds since January 1, 1970 UTC.
1332+
func UnixMilli(msec int64) Time {
1333+
return Unix(msec/1e3, (msec%1e3)*1e6)
1334+
}
1335+
1336+
// UnixMicro returns the local Time corresponding to the given Unix time,
1337+
// usec milliseconds since January 1, 1970 UTC.
1338+
func UnixMicro(usec int64) Time {
1339+
return Unix(usec/1e6, (usec%1e6)*1e3)
1340+
}
1341+
13121342
// IsDST reports whether the time in the configured location is in Daylight Savings Time.
13131343
func (t *Time) IsDST() bool {
13141344
_, _, _, _, isDST := t.loc.lookup(t.Unix())

src/time/time_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,28 @@ func TestNanosecondsToUTCAndBack(t *testing.T) {
202202
}
203203
}
204204

205+
func TestUnixMilli(t *testing.T) {
206+
f := func(msec int64) bool {
207+
t := UnixMilli(msec)
208+
return t.UnixMilli() == msec
209+
}
210+
cfg := &quick.Config{MaxCount: 10000}
211+
if err := quick.Check(f, cfg); err != nil {
212+
t.Fatal(err)
213+
}
214+
}
215+
216+
func TestUnixMicro(t *testing.T) {
217+
f := func(usec int64) bool {
218+
t := UnixMicro(usec)
219+
return t.UnixMicro() == usec
220+
}
221+
cfg := &quick.Config{MaxCount: 10000}
222+
if err := quick.Check(f, cfg); err != nil {
223+
t.Fatal(err)
224+
}
225+
}
226+
205227
// The time routines provide no way to get absolute time
206228
// (seconds since zero), but we need it to compute the right
207229
// answer for bizarre roundings like "to the nearest 3 ns".
@@ -959,6 +981,8 @@ var mallocTest = []struct {
959981
}{
960982
{0, `time.Now()`, func() { t = Now() }},
961983
{0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }},
984+
{0, `time.Now().UnixMilli()`, func() { u = Now().UnixMilli() }},
985+
{0, `time.Now().UnixMicro()`, func() { u = Now().UnixMicro() }},
962986
}
963987

964988
func TestCountMallocs(t *testing.T) {
@@ -1249,6 +1273,8 @@ var defaultLocTests = []struct {
12491273

12501274
{"Unix", func(t1, t2 Time) bool { return t1.Unix() == t2.Unix() }},
12511275
{"UnixNano", func(t1, t2 Time) bool { return t1.UnixNano() == t2.UnixNano() }},
1276+
{"UnixMilli", func(t1, t2 Time) bool { return t1.UnixMilli() == t2.UnixMilli() }},
1277+
{"UnixMicro", func(t1, t2 Time) bool { return t1.UnixMicro() == t2.UnixMicro() }},
12521278

12531279
{"MarshalBinary", func(t1, t2 Time) bool {
12541280
a1, b1 := t1.MarshalBinary()
@@ -1301,6 +1327,18 @@ func BenchmarkNowUnixNano(b *testing.B) {
13011327
}
13021328
}
13031329

1330+
func BenchmarkNowUnixMilli(b *testing.B) {
1331+
for i := 0; i < b.N; i++ {
1332+
u = Now().UnixMilli()
1333+
}
1334+
}
1335+
1336+
func BenchmarkNowUnixMicro(b *testing.B) {
1337+
for i := 0; i < b.N; i++ {
1338+
u = Now().UnixMicro()
1339+
}
1340+
}
1341+
13041342
func BenchmarkFormat(b *testing.B) {
13051343
t := Unix(1265346057, 0)
13061344
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)