summaryrefslogtreecommitdiff
path: root/pkg/caddyscript/lib/time.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-02-06 18:46:52 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2020-02-06 18:46:52 -0700
commit8b28c36d480070c8cf954cb27bec3b44bd7d12ca (patch)
tree4219adc87173ea3af7603e36e8ef44bb8a791f40 /pkg/caddyscript/lib/time.go
parent4a07a5d41e0f54d1a1ec998b9d956ccf2a880d90 (diff)
Remove Starlark, for now
This is temporary as we prepare for a stable v2 release. We don't want to make promises we don't know we can keep, and the Starlark integration deserves much more focused attention which resources and funding do not currently permit. When the project is financially stable, I will be able to revisit this properly and add flexible, robust Starlark scripting support to Caddy 2.
Diffstat (limited to 'pkg/caddyscript/lib/time.go')
-rw-r--r--pkg/caddyscript/lib/time.go144
1 files changed, 0 insertions, 144 deletions
diff --git a/pkg/caddyscript/lib/time.go b/pkg/caddyscript/lib/time.go
deleted file mode 100644
index 0e2f4e4..0000000
--- a/pkg/caddyscript/lib/time.go
+++ /dev/null
@@ -1,144 +0,0 @@
-// Copyright 2015 Matthew Holt and The Caddy Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package caddyscript
-
-import (
- "fmt"
- ti "time"
-
- "go.starlark.net/starlark"
-)
-
-// Time represents a time type for caddyscript.
-type Time struct {
- value int64 // time since epoch in nanoseconds
-}
-
-// AttrNames defines what properties and methods are available on the Time type.
-func (r Time) AttrNames() []string {
- return []string{"now", "parse", "add", "subtract", "minute", "hour", "day", "value"}
-}
-
-// Attr defines what happens when props or methods are called on the Time type.
-func (r Time) Attr(name string) (starlark.Value, error) {
- switch name {
- case "now":
- b := starlark.NewBuiltin("now", r.Now)
- b = b.BindReceiver(r)
- return b, nil
- case "parse_duration":
- b := starlark.NewBuiltin("parse_duration", r.ParseDuration)
- b = b.BindReceiver(r)
- return b, nil
- case "add":
- b := starlark.NewBuiltin("add", r.Add)
- b = b.BindReceiver(r)
- return b, nil
- case "subtract":
- b := starlark.NewBuiltin("subtract", r.Subtract)
- b = b.BindReceiver(r)
- return b, nil
- case "minute":
- b := starlark.NewBuiltin("minute", r.Minute)
- b = b.BindReceiver(r)
- return b, nil
- case "hour":
- b := starlark.NewBuiltin("hour", r.Hour)
- b = b.BindReceiver(r)
- return b, nil
- case "day":
- b := starlark.NewBuiltin("day", r.Day)
- b = b.BindReceiver(r)
- return b, nil
- case "value":
- return starlark.MakeInt64(r.value), nil
- }
-
- return nil, nil
-}
-
-func (r Time) Freeze() {}
-func (r Time) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: Time") }
-func (r Time) String() string { return fmt.Sprint(r.value) }
-func (r Time) Type() string { return "Time" }
-func (r Time) Truth() starlark.Bool { return true }
-
-// Hour returns the current hour of a unix timestamp in range [0, 23].
-func (r Time) Hour(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- t := ti.Unix(0, r.value)
- return starlark.MakeInt(t.Hour()), nil
-}
-
-// Minute returns the current minute of the hour for a unix timestamp in range [0, 59].
-func (r Time) Minute(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- t := ti.Unix(0, r.value)
- return starlark.MakeInt(t.Minute()), nil
-}
-
-// Day returns the current day in a week of a unix timestamp... [Sunday = 0...]
-func (r Time) Day(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- t := ti.Unix(0, r.value)
- return starlark.MakeInt(int(t.Weekday())), nil
-}
-
-// Now returns the current time as a unix timestamp.
-func (r Time) Now(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- val := ti.Now().UnixNano()
- r.value = val
- return r, nil
-}
-
-// ParseDuration parses a go duration string to a time type.
-func (r Time) ParseDuration(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- var dur string
- err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &dur)
- if err != nil {
- return starlark.None, fmt.Errorf("could not unpack args: %v", err.Error())
- }
-
- if parsed, err := ti.ParseDuration(dur); err == nil {
- val := parsed.Nanoseconds()
- r.value = val
- return r, nil
- }
-
- return starlark.None, fmt.Errorf("time.parse_duration: argument cannot be parsed as a valid go time duration")
-}
-
-// Add adds time to a time type.
-func (r Time) Add(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- var t Time
- err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &t)
- if err != nil {
- return starlark.None, fmt.Errorf("could not unpack args: %v", err.Error())
- }
-
- val := r.value + t.value
- r.value = val
- return r, nil
-}
-
-// Subtract adds time to a time type.
-func (r Time) Subtract(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
- var t Time
- err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &t)
- if err != nil {
- return starlark.None, fmt.Errorf("could not unpack args: %v", err.Error())
- }
-
- val := r.value - t.value
- r.value = val
- return r, nil
-}