16
16
17
17
import io .netty .buffer .ByteBuf ;
18
18
import org .joda .time .Period ;
19
+ import org .joda .time .format .PeriodFormatter ;
20
+ import org .joda .time .format .PeriodFormatterBuilder ;
19
21
20
22
import javax .annotation .Nonnull ;
21
23
@@ -30,6 +32,48 @@ public class IntervalType
30
32
private static final int TYPE_LEN = 16 ;
31
33
private static final int TYPE_MOD = -1 ;
32
34
public static final IntervalType INTERVAL = new IntervalType ();
35
+ private static final PeriodFormatter DAY_FORMATTER = new PeriodFormatterBuilder ()
36
+ .appendYears ()
37
+ .appendSuffix (" year" , " years" )
38
+ .appendSeparator (" " )
39
+ .appendMonths ()
40
+ .appendSuffix (" mon" , " mons" )
41
+ .appendSeparator (" " )
42
+ .appendWeeks ()
43
+ .appendSuffix (" weeks" )
44
+ .appendSeparator (" " )
45
+ .appendDays ()
46
+ .appendSuffix (" day" , " days" )
47
+ .toFormatter ();
48
+
49
+ private static final PeriodFormatter TIME_FORMATTER = new PeriodFormatterBuilder ()
50
+ .printZeroAlways ()
51
+ .minimumPrintedDigits (2 )
52
+ .appendHours ()
53
+ .appendSeparator (":" )
54
+ .minimumPrintedDigits (2 )
55
+ .printZeroAlways ()
56
+ .appendMinutes ()
57
+ .appendSeparator (":" )
58
+ .minimumPrintedDigits (2 )
59
+ .printZeroAlways ()
60
+ .appendSecondsWithOptionalMillis ()
61
+ .toFormatter ();
62
+
63
+ private static final PeriodFormatter PG_INTERVAL_FORMATTER = new PeriodFormatterBuilder ()
64
+ .appendYears ()
65
+ .appendSuffix (" years " )
66
+ .appendMonths ()
67
+ .appendSuffix (" mons " )
68
+ .appendDays ()
69
+ .appendSuffix (" days " )
70
+ .appendHours ()
71
+ .appendSuffix (" hours " )
72
+ .appendMinutes ()
73
+ .appendSuffix (" mins " )
74
+ .appendSecondsWithOptionalMillis ()
75
+ .appendSuffix (" secs" )
76
+ .toFormatter ();
33
77
34
78
private IntervalType ()
35
79
{
@@ -39,9 +83,7 @@ private IntervalType()
39
83
@ Override
40
84
public int typArray ()
41
85
{
42
- // TODO support interval
43
- // return PGArray.INTERVAL_ARRAY.oid();
44
- throw new UnsupportedOperationException ("no implementation" );
86
+ return PGArray .INTERVAL_ARRAY .oid ();
45
87
}
46
88
47
89
@ Override
@@ -107,15 +149,26 @@ public Period readBinaryValue(ByteBuf buffer, int valueLength)
107
149
@ Override
108
150
public byte [] encodeAsUTF8Text (@ Nonnull Period value )
109
151
{
110
- // StringBuilder sb = new StringBuilder();
111
- // IntervalType.PERIOD_FORMATTER.printTo(sb, (ReadablePeriod) value);
112
- return value .toString ().getBytes (StandardCharsets .UTF_8 );
152
+ StringBuilder sb = new StringBuilder ();
153
+ sb .append (DAY_FORMATTER .print (value ));
154
+ sb .append (" " );
155
+ // the negative sign need to be placed before the time, like -00:00:01
156
+ if (value .getHours () < 0 || value .getMinutes () < 0 || value .getSeconds () < 0 || value .getMillis () < 0 ) {
157
+ sb .append ("-" );
158
+ }
159
+ Period absValue = new Period (
160
+ Math .abs (value .getHours ()),
161
+ Math .abs (value .getMinutes ()),
162
+ Math .abs (value .getSeconds ()),
163
+ Math .abs (value .getMillis ()));
164
+ sb .append (TIME_FORMATTER .print (absValue ));
165
+
166
+ return sb .toString ().replace ("00:00:00" , "" ).trim ().getBytes (StandardCharsets .UTF_8 );
113
167
}
114
168
115
169
@ Override
116
170
public Period decodeUTF8Text (byte [] bytes )
117
171
{
118
- // return IntervalType.PERIOD_FORMATTER.parsePeriod(new String(bytes, StandardCharsets.UTF_8));
119
- return new Period (new String (bytes , StandardCharsets .UTF_8 ));
172
+ return PG_INTERVAL_FORMATTER .parsePeriod (new String (bytes , StandardCharsets .UTF_8 ));
120
173
}
121
174
}
0 commit comments