go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/api/gerrit/time.go (about) 1 // Copyright 2018 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gerrit 16 17 import ( 18 "fmt" 19 "time" 20 21 "go.chromium.org/luci/common/errors" 22 ) 23 24 const ( 25 // GerritTimestampLayout is the timestamp format used in Gerrit. 26 // 27 // Timestamp is given in UTC and has the format 'yyyy-mm-dd hh:mm:ss.fffffffff' where 28 // 'ffffffffff' represents nanoseconds. See: 29 // https://gerrit-review.googlesource.com/Documentation/rest-api.html#timestamp 30 GerritTimestampLayout = "2006-01-02 15:04:05.000000000" 31 ) 32 33 // FormatTime formats time.Time for Gerrit consumption. 34 func FormatTime(t time.Time) string { 35 return fmt.Sprintf(`"%s"`, t.UTC().Format(GerritTimestampLayout)) 36 } 37 38 // ParseTime returns time.Time givne its Gerrit representation. 39 func ParseTime(s string) (time.Time, error) { 40 const msg = "failed to parse Gerrit timestamp %q" 41 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 42 return time.Time{}, errors.Reason(msg, s).Err() 43 } 44 t, err := time.Parse(GerritTimestampLayout, s[1:len(s)-1]) 45 if err != nil { 46 return time.Time{}, errors.Annotate(err, msg, s).Err() 47 } 48 return t, nil 49 } 50 51 // Timestamp is time.Time for Gerrit JSON interop. 52 type Timestamp struct { 53 time.Time 54 } 55 56 // MarshalJSON implements json.Marshaler. 57 func (t *Timestamp) MarshalJSON() ([]byte, error) { 58 return []byte(FormatTime(t.Time)), nil 59 } 60 61 // UnmarshalJSON implements json.Unmarshaler. 62 func (t *Timestamp) UnmarshalJSON(b []byte) error { 63 parsedTime, err := ParseTime(string(b)) 64 if err == nil { 65 t.Time = parsedTime 66 } 67 return err 68 }