Skip to content

Commit

Permalink
Add example (#3)
Browse files Browse the repository at this point in the history
* added example

* made test from example

* removed redundant

* removed redundant from docstring

* renamed
  • Loading branch information
vtopc authored Dec 22, 2019
1 parent 38f5575 commit 8a19f51
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,50 @@

[![Godoc Reference][godoc-img]][godoc]

Package epoch contains primitives for unmarshaling Unix time(int64) JSON's fields into build-in time.Time type.
Contains primitives for marshaling/unmarshaling Unix timestamp/epoch to/from build-in time.Time type in JSON.

## Seconds
Seconds since the Epoch(Unix time).
Inherits built-in time.Time type, thus has all it methods, but has custom serializer and
deserializer(converts integer into built-in time.Time and vice versa).

### Example

```go
package main

import (
"encoding/json"
"fmt"

"github.com/vtopc/epoch"
)

type Request struct {
Timestamp epoch.Seconds `json:"timestamp"`
}

func main() {
var v Request
err := json.Unmarshal([]byte(`{"timestamp":1136239445}`), &v)
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", v)
// Output: {Timestamp:2006-01-03 00:04:05 +0200 EET}

// Also as epoch.Seconds inherits all time.Time's methods one can do next:
fmt.Println(v.Timestamp.Year())
// Output: 2006
fmt.Println(v.Timestamp.UTC().String())
// Output: 2006-01-02 22:04:05 +0000 UTC
}
```

## Milliseconds
Same as epoch.Seconds, but for Epoch(Unix time) in milliseconds.


[godoc]: https://godoc.org/github.com/vtopc/epoch
[godoc-img]: https://godoc.org/github.com/vtopc/epoch?status.svg
27 changes: 27 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package epoch_test

import (
"encoding/json"
"fmt"

"github.com/vtopc/epoch"
)

type Request struct {
Timestamp epoch.Seconds `json:"timestamp"`
}

func ExampleSeconds() {
var v Request
err := json.Unmarshal([]byte(`{"timestamp":1136239445}`), &v)
if err != nil {
panic(err)
}

// Also as epoch.Seconds inherits all time.Time's methods:
fmt.Println(v.Timestamp.Year())
fmt.Println(v.Timestamp.UTC().String())
// Output:
// 2006
// 2006-01-02 22:04:05 +0000 UTC
}
8 changes: 0 additions & 8 deletions seconds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
// Seconds - seconds since the Epoch(Unix time).
// Inherits built-in time.Time type, thus has all it methods, but has custom serializer and
// deserializer(converts integer into built-in time.Time and vice versa).
//
// Examples: next JSON:
// {"time":1136239445}
// could be unmarshaled into next:
// struct {
// Timestamp epoch.Seconds `json:"timestamp"`
// }
// and vice versa.
type Seconds struct {
time.Time
}
Expand Down

0 comments on commit 8a19f51

Please sign in to comment.