// Package pgdatetime writes PostgreSQL's ISO date/time text format. // // The format is the one EncodeDateTime and AppendSeconds produce in // src/backend/utils/adt/datetime.c under the default ISO DateStyle: // // stamp = date [ SP time [ zone ] ] [ " BC" ] // date = year "-" 2DIGIT "-" 2DIGIT // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT [ "." 1*6DIGIT ] // // The year is zero-padded to four digits and then grows as wide as it needs, the // fractional part is omitted when it is zero and never carries trailing zeros, and " BC" // comes last, after any time zone. Go's layout language can express none of those, which // is why the format is written out here rather than left to time.Format. // // This is the only implementation of the format in pgx. pgtype's date, timestamp and // timestamptz text encoders and the simple protocol's query sanitizer all go through it, // so they cannot drift apart.
package pgdatetime import ( ) // AppendDate appends t's date. The fields are read from t in its own location. func ( []byte, time.Time) []byte { , := splitBCYear(.Year()) = appendDate(, , int(.Month()), .Day()) return appendEra(, ) } // AppendTimestamp appends t's date and time of day followed by zone, which goes before // the era suffix because PostgreSQL writes " BC" last. zone is "Z" for a value already // converted to UTC and "" for a type that carries no time zone at all. // // The fields are read from t in its own location, so a caller that wants the UTC instant // must convert first. Sub-microsecond precision is discarded, matching the resolution of // the wire format. func ( []byte, time.Time, string) []byte { , := splitBCYear(.Year()) = appendDate(, , int(.Month()), .Day()) = append(, ' ') = appendTime(, .Hour(), .Minute(), .Second(), .Nanosecond()) = append(, ...) return appendEra(, ) } // splitBCYear converts an astronomical year, in which 1 BC is 0 and 2 BC is -1, to the // year and era PostgreSQL writes. func ( int) ( int, bool) { if <= 0 { return 1 - , true } return , false } func ( []byte, bool) []byte { if { = append(, " BC"...) } return } // appendDate appends year-month-day. year is a displayed year from splitBCYear rather // than an astronomical one. func ( []byte, , , int) []byte { = appendYear(, ) = append(, '-') = append2Digits(, ) = append(, '-') return append2Digits(, ) } func ( []byte, , , , int) []byte { = append2Digits(, ) = append(, ':') = append2Digits(, ) = append(, ':') = append2Digits(, ) := / 1000 if == 0 { return } var [6]byte for := 5; >= 0; -- { [] = byte('0' + %10) /= 10 } // The server trims trailing zeros, so ".5" rather than ".500000". := len() for > 1 && [-1] == '0' { -- } = append(, '.') return append(, [:]...) } // append2Digits appends v as exactly two digits. Every field but the year is fixed width // and comes from a time.Time accessor, so v is always in [0, 99] and the general routine // below is not needed. func ( []byte, int) []byte { return append(, byte('0'+/10), byte('0'+%10)) } // appendYear appends v zero-padded to four digits, or wider when the year needs it. v is // a displayed year from splitBCYear and so is always positive. func ( []byte, int) []byte { var [20]byte := strconv.AppendInt([:0], int64(), 10) for := len(); < 4; ++ { = append(, '0') } return append(, ...) }