示例#1
0
// formatTs formats t with an optional offset into a format lib/pq understands,
// appending to the provided tmp buffer and reallocating if needed. The function
// will then return the resulting buffer. formatTs is mostly cribbed from
// github.com/lib/pq.
func formatTs(t time.Time, offset *time.Location, tmp []byte) (b []byte) {
	// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
	// minus sign preferred by Go.
	// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
	if offset != nil {
		t = t.In(offset)
	} else {
		t = t.UTC()
	}
	bc := false
	if t.Year() <= 0 {
		// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
		t = t.AddDate((-t.Year())*2+1, 0, 0)
		bc = true
	}

	if offset != nil {
		b = t.AppendFormat(tmp, pgTimeStampFormat)
	} else {
		b = t.AppendFormat(tmp, pgTimeStampFormatNoOffset)
	}

	if bc {
		b = append(b, " BC"...)
	}
	return b
}
示例#2
0
func (enc *textEncoder) addTime(final *textEncoder, t time.Time) {
	if enc.timeFmt == "" {
		return
	}
	final.bytes = append(final.bytes, ' ')
	final.bytes = t.AppendFormat(final.bytes, enc.timeFmt)
}
示例#3
0
func addTimestamp(b []byte, t time.Time) []byte {
	if t.IsZero() {
		b = append(b, nilValueByte)
	} else {
		b = t.AppendFormat(b, time.RFC3339Nano)
	}
	b = append(b, spaceByte)
	return b
}