16
16
17
17
package io .r2dbc .postgresql .codec ;
18
18
19
+ import java .time .Instant ;
19
20
import java .time .format .DateTimeFormatter ;
20
21
import java .time .format .DateTimeFormatterBuilder ;
21
22
import java .time .format .DateTimeParseException ;
22
23
import java .time .format .SignStyle ;
24
+ import java .time .temporal .Temporal ;
23
25
import java .time .temporal .TemporalQuery ;
24
26
27
+ import static java .time .ZoneOffset .UTC ;
28
+ import static java .time .format .TextStyle .SHORT ;
25
29
import static java .time .temporal .ChronoField .DAY_OF_MONTH ;
30
+ import static java .time .temporal .ChronoField .ERA ;
26
31
import static java .time .temporal .ChronoField .HOUR_OF_DAY ;
27
32
import static java .time .temporal .ChronoField .MINUTE_OF_HOUR ;
28
33
import static java .time .temporal .ChronoField .MONTH_OF_YEAR ;
29
34
import static java .time .temporal .ChronoField .NANO_OF_SECOND ;
30
35
import static java .time .temporal .ChronoField .SECOND_OF_MINUTE ;
31
- import static java .time .temporal .ChronoField .YEAR ;
36
+ import static java .time .temporal .ChronoField .YEAR_OF_ERA ;
32
37
33
38
class PostgresqlDateTimeFormatter {
34
39
35
40
private static final DateTimeFormatter FULL_OFFSET =
36
41
new DateTimeFormatterBuilder ()
37
- .appendValue (YEAR , 4 , 10 , SignStyle .EXCEEDS_PAD )
42
+ .appendValue (YEAR_OF_ERA , 4 , 10 , SignStyle .NEVER )
38
43
.appendLiteral ('-' )
39
44
.appendValue (MONTH_OF_YEAR , 2 )
40
45
.appendLiteral ('-' )
@@ -52,11 +57,15 @@ class PostgresqlDateTimeFormatter {
52
57
.optionalStart ()
53
58
.appendOffset ("+HH:MM:ss" , "+00:00:00" )
54
59
.optionalEnd ()
60
+ .optionalStart ()
61
+ .appendLiteral (' ' )
62
+ .appendText (ERA , SHORT )
63
+ .optionalEnd ()
55
64
.toFormatter ();
56
65
57
66
private static final DateTimeFormatter SHORT_OFFSET =
58
67
new DateTimeFormatterBuilder ()
59
- .appendValue (YEAR , 4 , 10 , SignStyle .EXCEEDS_PAD )
68
+ .appendValue (YEAR_OF_ERA , 4 , 10 , SignStyle .NEVER )
60
69
.appendLiteral ('-' )
61
70
.appendValue (MONTH_OF_YEAR , 2 )
62
71
.appendLiteral ('-' )
@@ -74,6 +83,10 @@ class PostgresqlDateTimeFormatter {
74
83
.optionalStart ()
75
84
.appendOffset ("+HH:mm" , "+00:00" )
76
85
.optionalEnd ()
86
+ .optionalStart ()
87
+ .appendLiteral (' ' )
88
+ .appendText (ERA , SHORT )
89
+ .optionalEnd ()
77
90
.toFormatter ();
78
91
79
92
/**
@@ -104,4 +117,11 @@ static <T> T parse(CharSequence text, TemporalQuery<T> query) {
104
117
}
105
118
}
106
119
120
+ static String format (Temporal temporal ) {
121
+ if (temporal instanceof Instant ) {
122
+ temporal = ((Instant ) temporal ).atOffset (UTC );
123
+ }
124
+ return SHORT_OFFSET .format (temporal );
125
+ }
126
+
107
127
}
0 commit comments