Return iso dates for everything date related from the api #130
|
@ -0,0 +1,54 @@
|
|||
// Vikunja is a todo-list application to facilitate your life.
|
||||
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeStamp int64
|
||||
|
||||
func (ts *TimeStamp) ToTime() time.Time {
|
||||
return time.Unix(int64(*ts), 0)
|
||||
}
|
||||
|
||||
func FromTime(t time.Time) TimeStamp {
|
||||
return TimeStamp(t.Unix())
|
||||
}
|
||||
|
||||
func (ts *TimeStamp) MarshalJSON() ([]byte, error) {
|
||||
var s string
|
||||
s = `"` + ts.ToTime().String() + `"`
|
||||
return []byte(s), nil
|
||||
}
|
||||
|
||||
func (ts *TimeStamp) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*ts = TimeStamp(t.Unix())
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue