File size: 902 Bytes
fea99b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package datetime

import "strings"

// Format extends the time.Time.Format method to support the RFC 9557 format
func (t DateTime) Format(layout string) string {
	timezone := t.Location().String()

	// If the timezone is not set use the fallback layout
	if timezone == "" {
		return t.Time.Format(fallbackRFC9557FormatLayout(layout))
	}

	// Replace the timezone name layout part with the timezone name
	return strings.ReplaceAll(t.Time.Format(layout), layoutTZName, timezone)
}

// fallbackFormatLayout can be used to convert a RFC9557 layout to a ISO8601 layout when no timezone is provided
func fallbackRFC9557FormatLayout(layout string) string {
	switch layout {
	case RFC9557Layout:
		return ISO8601Layout
	case RFC9557MilliLayout:
		return ISO8601MilliLayout
	case RFC9557MicroLayout:
		return ISO8601MicroLayout
	case RFC9557NanoLayout:
		return ISO8601NanoLayout
	default:
		return layout
	}
}