Return iso dates for everything date related from the api #130

Merged
konrad merged 15 commits from feature/datetime into master 2020-02-08 12:48:51 +00:00
1 changed files with 54 additions and 0 deletions
Showing only changes of commit 6f8eb5523d - Show all commits

54
pkg/timeutil/time.go Normal file
View File

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