commit 27bb555fa35ed980a6c28cd22a2c1a506b510cd9 Author: Quentin Renard Date: Mon Dec 23 12:14:11 2019 +0100 Added time diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..87a3112 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Quentin Renard + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c26a8f3 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +`Astikit` is a set of golang helpers that don't require any external dependencies. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8f3c2bc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/asticode/go-astikit + +go 1.13 diff --git a/time.go b/time.go new file mode 100644 index 0000000..ec18be6 --- /dev/null +++ b/time.go @@ -0,0 +1,56 @@ +package astikit + +import ( + "context" + "strconv" + "time" +) + +// Sleep is a cancellable sleep +func Sleep(ctx context.Context, d time.Duration) (err error) { + for { + select { + case <-time.After(d): + return + case <-ctx.Done(): + err = ctx.Err() + return + } + } +} + +// Timestamp represents a timestamp you can marshal and umarshal +type Timestamp struct { + time.Time +} + +// NewTimestamp creates a new timestamp +func NewTimestamp(t time.Time) *Timestamp { + return &Timestamp{Time: t} +} + +// UnmarshalJSON implements the JSONUnmarshaler interface +func (t *Timestamp) UnmarshalJSON(text []byte) error { + return t.UnmarshalText(text) +} + +// UnmarshalText implements the TextUnmarshaler interface +func (t *Timestamp) UnmarshalText(text []byte) (err error) { + var i int + if i, err = strconv.Atoi(string(text)); err != nil { + return + } + t.Time = time.Unix(int64(i), 0) + return +} + +// MarshalJSON implements the JSONMarshaler interface +func (t Timestamp) MarshalJSON() ([]byte, error) { + return t.MarshalText() +} + +// MarshalText implements the TextMarshaler interface +func (t Timestamp) MarshalText() (text []byte, err error) { + text = []byte(strconv.Itoa(int(t.UTC().Unix()))) + return +} diff --git a/time_test.go b/time_test.go new file mode 100644 index 0000000..d7cb49c --- /dev/null +++ b/time_test.go @@ -0,0 +1,47 @@ +package astikit + +import ( + "context" + "encoding/json" + "errors" + "sync" + "testing" + "time" +) + +func TestSleep(t *testing.T) { + var ctx, cancel = context.WithCancel(context.Background()) + var err error + var wg = &sync.WaitGroup{} + wg.Add(1) + go func() { + defer wg.Done() + err = Sleep(ctx, time.Minute) + }() + cancel() + wg.Wait() + if !errors.Is(err, context.Canceled) { + t.Errorf("err should be %s, got %s", context.Canceled, err) + } +} + +func TestTimestamp(t *testing.T) { + const j = `{"value":1495290215}` + v := struct { + Value Timestamp `json:"value"` + }{} + err := json.Unmarshal([]byte(j), &v) + if err != nil { + t.Errorf("err should be nil, got %s", err) + } + if v.Value.Unix() != 1495290215 { + t.Errorf("timestamp should be %v, got %v", 1495290215, v.Value.Unix()) + } + b, err := json.Marshal(v) + if err != nil { + t.Errorf("err should be nil, got %s", err) + } + if string(b) != j { + t.Errorf("json should be %s, got %s", j, b) + } +}