Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: google/starlark-go
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 30460a334c73ad949b3172857ccd4744e5cb4a2f
Choose a base ref
..
head repository: google/starlark-go
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4829b973456275889f64099ff706e2c6404b8443
Choose a head ref
Showing with 96 additions and 5 deletions.
  1. +14 −5 lib/time/time.go
  2. +82 −0 lib/time/time_test.go
19 changes: 14 additions & 5 deletions lib/time/time.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
package time // import "go.starlark.net/lib/time"

import (
"errors"
"fmt"
"sort"
"time"
@@ -81,13 +82,13 @@ const contextKey = "time.now"
// SetNow sets the thread's optional clock function.
// If non-nil, it will be used in preference to NowFunc when the
// thread requests the current time by executing a call to time.now.
func SetNow(thread *starlark.Thread, nowFunc func() time.Time) {
func SetNow(thread *starlark.Thread, nowFunc func() (time.Time, error)) {
thread.SetLocal(contextKey, nowFunc)
}

// Now returns the clock function previously associated with this thread.
func Now(thread *starlark.Thread) func() time.Time {
nowFunc, _ := thread.Local(contextKey).(func() time.Time)
func Now(thread *starlark.Thread) func() (time.Time,error) {
nowFunc, _ := thread.Local(contextKey).(func() (time.Time,error))
return nowFunc
}

@@ -147,9 +148,17 @@ func fromTimestamp(thread *starlark.Thread, _ *starlark.Builtin, args starlark.T
}

func now(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
nowFunc := Now(thread)
nowErrFunc := Now(thread)
if nowErrFunc != nil {
t, err := nowErrFunc()
if err != nil {
return nil, err
}
return Time(t), nil
}
nowFunc := NowFunc
if nowFunc == nil {
nowFunc = NowFunc
return nil, errors.New("time.now() is not available")
}
return Time(nowFunc()), nil
}
82 changes: 82 additions & 0 deletions lib/time/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package time

import (
"errors"
"testing"
"time"

"go.starlark.net/starlark"
)

func TestPerThreadNowReturnsCorrectTime(t *testing.T) {
th := &starlark.Thread{}
date := time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC)
SetNow(th, func() (time.Time, error) {
return date, nil
})

res, err := starlark.Call(th, Module.Members["now"], nil, nil)
if err != nil {
t.Fatal(err)
}

retTime := time.Time(res.(Time))

if !retTime.Equal(date) {
t.Fatal("Expected time to be equal", retTime, date)
}
}

func TestPerThreadNowReturnsError(t *testing.T) {
th := &starlark.Thread{}
e := errors.New("no time")
SetNow(th, func() (time.Time, error) {
return time.Time{}, e
})

_, err := starlark.Call(th, Module.Members["now"], nil, nil)
if !errors.Is(err, e) {
t.Fatal("Expected equal error", e, err)
}
}

func TestGlobalNowReturnsCorrectTime(t *testing.T) {
th := &starlark.Thread{}

oldNow := NowFunc
defer func() {
NowFunc = oldNow
}()

date := time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC)
NowFunc = func() time.Time {
return date
}

res, err := starlark.Call(th, Module.Members["now"], nil, nil)
if err != nil {
t.Fatal(err)
}

retTime := time.Time(res.(Time))

if !retTime.Equal(date) {
t.Fatal("Expected time to be equal", retTime, date)
}
}

func TestGlobalNowReturnsErrorWhenNil(t *testing.T) {
th := &starlark.Thread{}

oldNow := NowFunc
defer func() {
NowFunc = oldNow
}()

NowFunc = nil

_, err := starlark.Call(th, Module.Members["now"], nil, nil)
if err == nil {
t.Fatal("Expected to get an error")
}
}