...

Source file src/github.com/google/go-github/v33/github/timestamp.go

Documentation: github.com/google/go-github/v33/github

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"strconv"
    10  	"time"
    11  )
    12  
    13  // Timestamp represents a time that can be unmarshalled from a JSON string
    14  // formatted as either an RFC3339 or Unix timestamp. This is necessary for some
    15  // fields since the GitHub API is inconsistent in how it represents times. All
    16  // exported methods of time.Time can be called on Timestamp.
    17  type Timestamp struct {
    18  	time.Time
    19  }
    20  
    21  func (t Timestamp) String() string {
    22  	return t.Time.String()
    23  }
    24  
    25  // UnmarshalJSON implements the json.Unmarshaler interface.
    26  // Time is expected in RFC3339 or Unix format.
    27  func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
    28  	str := string(data)
    29  	i, err := strconv.ParseInt(str, 10, 64)
    30  	if err == nil {
    31  		t.Time = time.Unix(i, 0)
    32  	} else {
    33  		t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str)
    34  	}
    35  	return
    36  }
    37  
    38  // Equal reports whether t and u are equal based on time.Equal
    39  func (t Timestamp) Equal(u Timestamp) bool {
    40  	return t.Time.Equal(u.Time)
    41  }
    42  

View as plain text