Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

pgdatetime

v5.13.0
github.com/jackc/pgx/v5/internal/pgdatetime

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

3 matches

Top ↑

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.

Functions

AppendDate

func AppendDate(buf []byte, t time.Time) []byte

AppendDate appends t's date. The fields are read from t in its own location.

AppendTimestamp

func AppendTimestamp(buf []byte, t time.Time, zone string) []byte

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.

Dependency graph