forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/auto upgrade joda #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pgomulka
wants to merge
14
commits into
6.8
Choose a base branch
from
feature/auto_upgrade_joda
base: 6.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4ea5103
some migration works
pgomulka 6297234
some more tests
pgomulka 8620ce6
plugin not working
pgomulka 82f0c24
plugin not working
pgomulka 178f139
draft
pgomulka dc0be79
some stuff works
pgomulka d5711b8
working timezone convertion
pgomulka 348a226
timezone explosino to alternatives
pgomulka ffbf3e6
literal parsings
pgomulka 6270ad5
cleanup and fixing literals parsing
pgomulka 0b2773b
remove migration first version
pgomulka 2b9bfb0
cleanup
pgomulka 1221e83
cleanup
pgomulka d04a77b
name of day month and day
pgomulka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
303 changes: 303 additions & 0 deletions
303
server/src/main/java/org/elasticsearch/common/joda/CustomDateTimeFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,303 @@ | ||
| /* | ||
| * Copyright 2001-2014 Stephen Colebourne | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.elasticsearch.common.joda; | ||
|
|
||
| import java.io.IOException; | ||
| import java.text.DateFormat; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Locale; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicReferenceArray; | ||
|
|
||
| import org.joda.time.Chronology; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.DateTimeZone; | ||
| import org.joda.time.ReadablePartial; | ||
| import org.joda.time.format.DateTimeFormatter; | ||
| import org.joda.time.format.DateTimeFormatterBuilder; | ||
| import org.joda.time.format.ISODateTimeFormat; | ||
|
|
||
| public class CustomDateTimeFormat { | ||
|
|
||
| public static void parsePatternTo(JodaJavaBuilder builder, String pattern) { | ||
| int length = pattern.length(); | ||
| int[] indexRef = new int[1]; | ||
|
|
||
| for (int i=0; i<length; i++) { | ||
| indexRef[0] = i; | ||
| String token = parseToken(pattern, indexRef); | ||
| i = indexRef[0]; | ||
|
|
||
| int tokenLen = token.length(); | ||
| if (tokenLen == 0) { | ||
| break; | ||
| } | ||
| char c = token.charAt(0); | ||
|
|
||
| switch (c) { | ||
| case 'G': // era designator (text) | ||
| builder.appendEraText(); | ||
| break; | ||
| case 'C': // century of era (number) | ||
| builder.appendCenturyOfEra(tokenLen, tokenLen); | ||
| break; | ||
| case 'x': // weekyear (number) | ||
| case 'y': // year (number) | ||
| case 'Y': // year of era (number) | ||
| if (tokenLen == 2) { | ||
| boolean lenientParse = true; | ||
|
|
||
| // Peek ahead to next token. | ||
| if (i + 1 < length) { | ||
| indexRef[0]++; | ||
| if (isNumericToken(parseToken(pattern, indexRef))) { | ||
| // If next token is a number, cannot support | ||
| // lenient parse, because it will consume digits | ||
| // that it should not. | ||
| lenientParse = false; | ||
| } | ||
| indexRef[0]--; | ||
| } | ||
|
|
||
| // Use pivots which are compatible with SimpleDateFormat. | ||
| switch (c) { | ||
| case 'x': | ||
| builder.appendTwoDigitWeekyear | ||
| (new DateTime().getWeekyear() - 30, lenientParse); | ||
| break; | ||
| case 'y': | ||
| case 'Y': | ||
| default: | ||
| builder.appendTwoDigitYear(new DateTime().getYear() - 30, lenientParse); | ||
| break; | ||
| } | ||
| } else { | ||
| // Try to support long year values. | ||
| int maxDigits = 9; | ||
|
|
||
| // Peek ahead to next token. | ||
| if (i + 1 < length) { | ||
| indexRef[0]++; | ||
| if (isNumericToken(parseToken(pattern, indexRef))) { | ||
| // If next token is a number, cannot support long years. | ||
| maxDigits = tokenLen; | ||
| } | ||
| indexRef[0]--; | ||
| } | ||
|
|
||
| switch (c) { | ||
| case 'x': | ||
| builder.appendWeekyear(tokenLen, maxDigits); | ||
| break; | ||
| case 'y': | ||
| builder.appendYear(tokenLen, maxDigits); | ||
| break; | ||
| case 'Y': | ||
| builder.appendYearOfEra(tokenLen, maxDigits); | ||
| break; | ||
| } | ||
| } | ||
| break; | ||
| case 'M': // month of year (text and number) | ||
| if (tokenLen >= 3) { | ||
| if (tokenLen >= 4) { | ||
| builder.appendMonthOfYearText(); | ||
| } else { | ||
| builder.appendMonthOfYearShortText(); | ||
| } | ||
| } else { | ||
| builder.appendMonthOfYear(tokenLen); | ||
| } | ||
| break; | ||
| case 'd': // day of month (number) | ||
| builder.appendDayOfMonth(tokenLen); | ||
| break; | ||
| case 'a': // am/pm marker (text) | ||
| builder.appendHalfdayOfDayText(); | ||
| break; | ||
| case 'h': // clockhour of halfday (number, 1..12) | ||
| builder.appendClockhourOfHalfday(tokenLen); | ||
| break; | ||
| case 'H': // hour of day (number, 0..23) | ||
| builder.appendHourOfDay(tokenLen); | ||
| break; | ||
| case 'k': // clockhour of day (1..24) | ||
| builder.appendClockhourOfDay(tokenLen); | ||
| break; | ||
| case 'K': // hour of halfday (0..11) | ||
| builder.appendHourOfHalfday(tokenLen); | ||
| break; | ||
| case 'm': // minute of hour (number) | ||
| builder.appendMinuteOfHour(tokenLen); | ||
| break; | ||
| case 's': // second of minute (number) | ||
| builder.appendSecondOfMinute(tokenLen); | ||
| break; | ||
| case 'S': // fraction of second (number) | ||
| builder.appendFractionOfSecond(tokenLen, tokenLen); | ||
| break; | ||
| case 'e': // day of week (number) | ||
| builder.appendDayOfWeek(tokenLen); | ||
| break; | ||
| case 'E': // dayOfWeek (text) | ||
| if (tokenLen >= 4) { | ||
| builder.appendDayOfWeekText(); | ||
| } else { | ||
| builder.appendDayOfWeekShortText(); | ||
| } | ||
| break; | ||
| case 'D': // day of year (number) | ||
| builder.appendDayOfYear(tokenLen); | ||
| break; | ||
| case 'w': // week of weekyear (number) | ||
| builder.appendWeekOfWeekyear(tokenLen); | ||
| break; | ||
| case 'z': // time zone (text) | ||
| if (tokenLen >= 4) { | ||
| builder.appendTimeZoneName(); | ||
| } else { | ||
| builder.appendTimeZoneShortName(null); | ||
| } | ||
| break; | ||
| case 'Z': // time zone offset | ||
| if (tokenLen == 1) { | ||
| builder.appendTimeZoneOffset(null, "Z", false, 2, 2); | ||
| } else if (tokenLen == 2) { | ||
| builder.appendTimeZoneOffset(null, "Z", true, 2, 2); | ||
| } else { | ||
| builder.appendTimeZoneId(); | ||
| } | ||
| break; | ||
| case '\'': // literal text | ||
| String sub = token.substring(1); | ||
| if (sub.length() == 1) { | ||
| builder.appendLiteral(sub.charAt(0)); | ||
| } else { | ||
| // Create copy of sub since otherwise the temporary quoted | ||
| // string would still be referenced internally. | ||
| builder.appendLiteral(new String(sub)); | ||
| } | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException | ||
| ("Illegal pattern component: " + token); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses an individual token. | ||
| * | ||
| * @param pattern the pattern string | ||
| * @param indexRef a single element array, where the input is the start | ||
| * location and the output is the location after parsing the token | ||
| * @return the parsed token | ||
| */ | ||
| static String parseToken(String pattern, int[] indexRef) { | ||
| StringBuilder buf = new StringBuilder(); | ||
|
|
||
| int i = indexRef[0]; | ||
| int length = pattern.length(); | ||
|
|
||
| char c = pattern.charAt(i); | ||
| if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') { | ||
| // Scan a run of the same character, which indicates a time | ||
| // pattern. | ||
| buf.append(c); | ||
|
|
||
| while (i + 1 < length) { | ||
| char peek = pattern.charAt(i + 1); | ||
| if (peek == c) { | ||
| buf.append(c); | ||
| i++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } else { | ||
| // This will identify token as text. | ||
| buf.append('\''); | ||
|
|
||
| boolean inLiteral = false; | ||
|
|
||
| for (; i < length; i++) { | ||
| c = pattern.charAt(i); | ||
|
|
||
| if (c == '\'') { | ||
| if (i + 1 < length && pattern.charAt(i + 1) == '\'') { | ||
| // '' is treated as escaped ' | ||
| i++; | ||
| buf.append(c);//THIS IS THE CHANGE COMPARED TO JODA | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the only change needed in joda's DateTimeFormat |
||
| } else { | ||
| inLiteral = !inLiteral; | ||
| buf.append(c); | ||
| } | ||
| } else if (!inLiteral && | ||
| (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' )) { | ||
| i--; | ||
| break; | ||
| } else { | ||
| buf.append(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| indexRef[0] = i; | ||
| return buf.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if token should be parsed as a numeric field. | ||
| * | ||
| * @param token the token to parse | ||
| * @return true if numeric field | ||
| */ | ||
| private static boolean isNumericToken(String token) { | ||
| int tokenLen = token.length(); | ||
| if (tokenLen > 0) { | ||
| char c = token.charAt(0); | ||
| switch (c) { | ||
| case 'c': // century (number) | ||
| case 'C': // century of era (number) | ||
| case 'x': // weekyear (number) | ||
| case 'y': // year (number) | ||
| case 'Y': // year of era (number) | ||
| case 'd': // day of month (number) | ||
| case 'h': // hour of day (number, 1..12) | ||
| case 'H': // hour of day (number, 0..23) | ||
| case 'm': // minute of hour (number) | ||
| case 's': // second of minute (number) | ||
| case 'S': // fraction of second (number) | ||
| case 'e': // day of week (number) | ||
| case 'D': // day of year (number) | ||
| case 'F': // day of week in month (number) | ||
| case 'w': // week of year (number) | ||
| case 'W': // week of month (number) | ||
| case 'k': // hour of day (1..24) | ||
| case 'K': // hour of day (0..11) | ||
| return true; | ||
| case 'M': // month of year (text and number) | ||
| if (tokenLen <= 2) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy of required methods from DateTimeFormat (unable to extend)