···1717// Represents the a Datetime in string format, as would pass Lexicon syntax validation: the intersection of RFC-3339 and ISO-8601 syntax.
1818//
1919// Always use [ParseDatetime] instead of wrapping strings directly, especially when working with network input.
2020+//
2121+// Syntax is specified at: https://atproto.com/specs/lexicon#datetime
2022type Datetime string
21232224func ParseDatetime(raw string) (Datetime, error) {
···2931 }
3032 if strings.HasSuffix(raw, "-00:00") {
3133 return "", fmt.Errorf("Datetime can't use '-00:00' for UTC timezone, must use '+00:00', per ISO-8601")
3434+ }
3535+ // ensure that the datetime actually parses using golang time lib
3636+ _, err := time.Parse(time.RFC3339Nano, raw)
3737+ if err != nil {
3838+ return "", err
3239 }
3340 return Datetime(raw), nil
3441}
35423636-// Parses a string to a golang time.Time in a single step.
4343+// Validates and converts a string to a golang [time.Time] in a single step.
3744func ParseDatetimeTime(raw string) (time.Time, error) {
3838- var zero time.Time
3945 d, err := ParseDatetime(raw)
4046 if err != nil {
4747+ var zero time.Time
4148 return zero, err
4249 }
4343- return d.Time()
5050+ return d.Time(), nil
4451}
45524646-// Parses the Datetime string in to a golang time.Time.
5353+// Parses the Datetime string in to a golang [time.Time].
4754//
4848-// There are a small number of strings which will pass initial syntax validation but fail when actually parsing, so this function can return an error. Use [ParseDatetimeTime] to fully parse in a single function call.
4949-func (d Datetime) Time() (time.Time, error) {
5050- return time.Parse(time.RFC3339Nano, d.String())
5555+// This method assumes that [ParseDatetime] was used to create the Datetime, which already verified parsing, and thus that [time.Parse] will always succeed. In the event of an error, zero/nil will be returned.
5656+func (d Datetime) Time() time.Time {
5757+ var zero time.Time
5858+ ret, err := time.Parse(time.RFC3339Nano, d.String())
5959+ if err != nil {
6060+ return zero
6161+ }
6262+ return ret
5163}
52645365// Creates a new valid Datetime string matching the current time, in prefered syntax.