-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make timestamp fields of type time.Time and time.Duration
- Loading branch information
1 parent
5959a18
commit ed80c61
Showing
6 changed files
with
106 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sarama | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Timestamp struct { | ||
*time.Time | ||
} | ||
|
||
func (t Timestamp) encode(pe packetEncoder) error { | ||
timestamp := int64(-1) | ||
|
||
if !t.Before(time.Unix(0, 0)) { | ||
timestamp = t.UnixNano() / int64(time.Millisecond) | ||
} else if !t.IsZero() { | ||
return PacketEncodingError{fmt.Sprintf("invalid timestamp (%v)", t)} | ||
} | ||
|
||
pe.putInt64(timestamp) | ||
return nil | ||
} | ||
|
||
func (t Timestamp) decode(pd packetDecoder) error { | ||
millis, err := pd.getInt64() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// negative timestamps are invalid, in these cases we should return | ||
// a zero time | ||
timestamp := time.Time{} | ||
if millis >= 0 { | ||
timestamp = time.Unix(millis/1000, (millis%1000)*int64(time.Millisecond)) | ||
} | ||
|
||
*t.Time = timestamp | ||
return nil | ||
} |