19
19
20
20
package org .elasticsearch .common .time ;
21
21
22
- import org .elasticsearch .bootstrap .JavaVersion ;
23
-
24
22
import java .time .format .DateTimeFormatter ;
25
23
import java .time .format .DateTimeFormatterBuilder ;
26
24
import java .time .format .ResolverStyle ;
@@ -72,7 +70,7 @@ public TemporalAccessor resolve(Map<TemporalField,Long> fieldValues,
72
70
private static final EpochField NANOS_OF_SECOND = new EpochField (ChronoUnit .NANOS , ChronoUnit .SECONDS , ValueRange .of (0 , 999_999_999 )) {
73
71
@ Override
74
72
public boolean isSupportedBy (TemporalAccessor temporal ) {
75
- return temporal .isSupported (ChronoField .NANO_OF_SECOND ) && temporal . getLong ( ChronoField . NANO_OF_SECOND ) != 0 ;
73
+ return temporal .isSupported (ChronoField .NANO_OF_SECOND );
76
74
}
77
75
@ Override
78
76
public long getFrom (TemporalAccessor temporal ) {
@@ -117,32 +115,30 @@ public boolean isSupportedBy(TemporalAccessor temporal) {
117
115
}
118
116
@ Override
119
117
public long getFrom (TemporalAccessor temporal ) {
120
- return temporal .getLong (ChronoField .NANO_OF_SECOND );
118
+ return temporal .getLong (ChronoField .NANO_OF_SECOND ) % 1_000_000 ;
121
119
}
122
120
};
123
121
124
122
// this supports seconds without any fraction
125
123
private static final DateTimeFormatter SECONDS_FORMATTER1 = new DateTimeFormatterBuilder ()
126
124
.appendValue (SECONDS , 1 , 19 , SignStyle .NORMAL )
125
+ .optionalStart () // optional is used so isSupported will be called when printing
126
+ .appendFraction (NANOS_OF_SECOND , 0 , 9 , true )
127
+ .optionalEnd ()
127
128
.toFormatter (Locale .ROOT );
128
129
129
130
// this supports seconds ending in dot
130
131
private static final DateTimeFormatter SECONDS_FORMATTER2 = new DateTimeFormatterBuilder ()
131
- .append ( SECONDS_FORMATTER1 )
132
+ .appendValue ( SECONDS , 1 , 19 , SignStyle . NORMAL )
132
133
.appendLiteral ('.' )
133
134
.toFormatter (Locale .ROOT );
134
135
135
- // this supports seconds with a fraction and is also used for printing
136
- private static final DateTimeFormatter SECONDS_FORMATTER3 = new DateTimeFormatterBuilder ()
137
- .append (SECONDS_FORMATTER1 )
138
- .optionalStart () // optional is used so isSupported will be called when printing
139
- .appendFraction (NANOS_OF_SECOND , 1 , 9 , true )
140
- .optionalEnd ()
141
- .toFormatter (Locale .ROOT );
142
-
143
136
// this supports milliseconds without any fraction
144
137
private static final DateTimeFormatter MILLISECONDS_FORMATTER1 = new DateTimeFormatterBuilder ()
145
138
.appendValue (MILLIS , 1 , 19 , SignStyle .NORMAL )
139
+ .optionalStart ()
140
+ .appendFraction (NANOS_OF_MILLI , 0 , 6 , true )
141
+ .optionalEnd ()
146
142
.toFormatter (Locale .ROOT );
147
143
148
144
// this supports milliseconds ending in dot
@@ -151,32 +147,13 @@ public long getFrom(TemporalAccessor temporal) {
151
147
.appendLiteral ('.' )
152
148
.toFormatter (Locale .ROOT );
153
149
154
- // this supports milliseconds with a fraction and is also used for printing
155
- private static final DateTimeFormatter MILLISECONDS_FORMATTER3 = new DateTimeFormatterBuilder ()
156
- .append (MILLISECONDS_FORMATTER1 )
157
- .optionalStart () // optional is used so isSupported will be called when printing
158
- .appendFraction (NANOS_OF_MILLI , 1 , 6 , true )
159
- .optionalEnd ()
160
- .toFormatter (Locale .ROOT );
161
-
162
- static final DateFormatter SECONDS_FORMATTER = new JavaDateFormatter ("epoch_second" , SECONDS_FORMATTER3 ,
150
+ static final DateFormatter SECONDS_FORMATTER = new JavaDateFormatter ("epoch_second" , SECONDS_FORMATTER1 ,
163
151
builder -> builder .parseDefaulting (ChronoField .NANO_OF_SECOND , 999_999_999L ),
164
- SECONDS_FORMATTER1 , SECONDS_FORMATTER2 , SECONDS_FORMATTER3 );
165
-
166
- static final DateFormatter MILLIS_FORMATTER = getEpochMillisFormatter ();
152
+ SECONDS_FORMATTER1 , SECONDS_FORMATTER2 );
167
153
168
- private static DateFormatter getEpochMillisFormatter () {
169
- // the third formatter fails under java 8 as a printer, so fall back to this one
170
- final DateTimeFormatter printer ;
171
- if (JavaVersion .current ().getVersion ().get (0 ) == 8 ) {
172
- printer = MILLISECONDS_FORMATTER1 ;
173
- } else {
174
- printer = MILLISECONDS_FORMATTER3 ;
175
- }
176
- return new JavaDateFormatter ("epoch_millis" , printer ,
177
- builder -> builder .parseDefaulting (EpochTime .NANOS_OF_MILLI , 999_999L ),
178
- MILLISECONDS_FORMATTER1 , MILLISECONDS_FORMATTER2 , MILLISECONDS_FORMATTER3 );
179
- }
154
+ static final DateFormatter MILLIS_FORMATTER = new JavaDateFormatter ("epoch_millis" , MILLISECONDS_FORMATTER1 ,
155
+ builder -> builder .parseDefaulting (EpochTime .NANOS_OF_MILLI , 999_999L ),
156
+ MILLISECONDS_FORMATTER1 , MILLISECONDS_FORMATTER2 );
180
157
181
158
private abstract static class EpochField implements TemporalField {
182
159
0 commit comments