Added time

This commit is contained in:
Quentin Renard
2019-12-23 12:14:11 +01:00
commit 27bb555fa3
5 changed files with 128 additions and 0 deletions

21
LICENSE Normal file
View File

@@ -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.

1
README.md Normal file
View File

@@ -0,0 +1 @@
`Astikit` is a set of golang helpers that don't require any external dependencies.

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/asticode/go-astikit
go 1.13

56
time.go Normal file
View File

@@ -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
}

47
time_test.go Normal file
View File

@@ -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)
}
}