From 4154bf0817e6304ceca83548af2a80fa16fcbc74 Mon Sep 17 00:00:00 2001 From: unclejack Date: Mon, 15 Sep 2014 21:44:58 +0300 Subject: [PATCH] add the timeutils package Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- timeutils/MAINTAINERS | 1 + timeutils/json.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 timeutils/MAINTAINERS create mode 100644 timeutils/json.go diff --git a/timeutils/MAINTAINERS b/timeutils/MAINTAINERS new file mode 100644 index 0000000..6dde476 --- /dev/null +++ b/timeutils/MAINTAINERS @@ -0,0 +1 @@ +Cristian Staretu (@unclejack) diff --git a/timeutils/json.go b/timeutils/json.go new file mode 100644 index 0000000..19f107b --- /dev/null +++ b/timeutils/json.go @@ -0,0 +1,23 @@ +package timeutils + +import ( + "errors" + "time" +) + +const ( + // Define our own version of RFC339Nano because we want one + // that pads the nano seconds part with zeros to ensure + // the timestamps are aligned in the logs. + RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" + JSONFormat = `"` + time.RFC3339Nano + `"` +) + +func FastMarshalJSON(t time.Time) (string, error) { + if y := t.Year(); y < 0 || y >= 10000 { + // RFC 3339 is clear that years are 4 digits exactly. + // See golang.org/issue/4556#c15 for more discussion. + return "", errors.New("Time.MarshalJSON: year outside of range [0,9999]") + } + return t.Format(JSONFormat), nil +}