-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-6185] Support date format #3611
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,13 @@ | |
| * @see FormatModels#DEFAULT | ||
| */ | ||
| public enum FormatElementEnum implements FormatElement { | ||
| CC("cc", "century (2 digits) (the twenty-first century starts on 2001-01-01)") { | ||
| @Override public void format(StringBuilder sb, Date date) { | ||
| final Calendar calendar = Work.get().calendar; | ||
| calendar.setTime(date); | ||
| sb.append(String.format(Locale.ROOT, "%2d", calendar.get(Calendar.YEAR) / 100 + 1)); | ||
| } | ||
| }, | ||
| D("F", "The weekday (Monday as the first day of the week) as a decimal number (1-7)") { | ||
| @Override public void format(StringBuilder sb, Date date) { | ||
| final Calendar calendar = Work.get().calendar; | ||
|
|
@@ -54,6 +61,12 @@ public enum FormatElementEnum implements FormatElement { | |
| sb.append(work.eeeeFormat.format(date)); | ||
| } | ||
| }, | ||
| day("eeee", "The full lower case day name") { | ||
|
Contributor
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. Enum constants should be all uppercase I believe. |
||
| @Override public void format(StringBuilder sb, Date date) { | ||
caicancai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final Work work = Work.get(); | ||
| sb.append(work.eeeeFormat.format(date).toLowerCase(Locale.ROOT)); | ||
| } | ||
| }, | ||
| DD("dd", "The day of the month as a decimal number (01-31)") { | ||
| @Override public void format(StringBuilder sb, Date date) { | ||
| final Calendar calendar = Work.get().calendar; | ||
|
|
@@ -74,6 +87,12 @@ public enum FormatElementEnum implements FormatElement { | |
| sb.append(work.eeeFormat.format(date)); | ||
| } | ||
| }, | ||
| dy("eee", "The abbreviated lower weekday name") { | ||
| @Override public void format(StringBuilder sb, Date date) { | ||
| final Work work = Work.get(); | ||
| sb.append(work.eeeFormat.format(date).toLowerCase(Locale.ROOT)); | ||
| } | ||
| }, | ||
| FF1("S", "Fractional seconds to 1 digit") { | ||
| @Override public void format(StringBuilder sb, Date date) { | ||
| final Work work = Work.get(); | ||
|
|
@@ -171,7 +190,7 @@ public enum FormatElementEnum implements FormatElement { | |
| sb.append(meridian); | ||
| } | ||
| }, | ||
| Q("", "The quarter as a decimal number (1-4)") { | ||
| Q("Q", "The quarter as a decimal number (1-4)") { | ||
| // TODO: Allow parsing of quarters. | ||
| @Override public void toPattern(StringBuilder sb) throws UnsupportedOperationException { | ||
| throw new UnsupportedOperationException("Cannot convert 'Q' FormatElement to Java pattern"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static org.apache.calcite.util.format.FormatElementEnum.CC; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.D; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.DAY; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.DD; | ||
|
|
@@ -55,6 +56,8 @@ | |
| import static org.apache.calcite.util.format.FormatElementEnum.WW; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.YY; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.YYYY; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.day; | ||
| import static org.apache.calcite.util.format.FormatElementEnum.dy; | ||
|
Contributor
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 imports here should be in alphabetical order.
Member
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. I tried it, but it will report an error. It seems that there is a restriction on the upper and lower case order.
Contributor
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. Wouldn't checkstyle complain if the imports are in the wrong order?
Contributor
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. Possibly checkstyle is using a different collation (e.g. different treatment of upper/lower case, spaces, underscores). Do whatever makes checkstyle happy.
Contributor
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. Easiest thing is to make the enum constants upper-case. |
||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
|
|
@@ -137,12 +140,16 @@ MI, literalElement(":"), SS, literalElement(" "), | |
| map.put("%x", | ||
| compositeElement("The date representation in MM/DD/YY format", | ||
| MM, literalElement("/"), DD, literalElement("/"), YY)); | ||
| map.put("%X", | ||
| compositeElement("The time representation in HH:MM:SS format", | ||
| HH24, literalElement(":"), MI, literalElement(":"), SS)); | ||
| map.put("%Y", YYYY); | ||
| map.put("%y", YY); | ||
| map.put("%Z", TZR); | ||
| BIG_QUERY = create(map); | ||
|
|
||
| map.clear(); | ||
| map.put("CC", CC); | ||
| map.put("HH12", HH12); | ||
| map.put("HH24", HH24); | ||
| map.put("MI", MI); | ||
|
|
@@ -157,7 +164,9 @@ MI, literalElement(":"), SS, literalElement(" "), | |
| map.put("YYYY", YYYY); | ||
| map.put("YY", YY); | ||
| map.put("Day", DAY); | ||
| map.put("day", day); | ||
| map.put("DY", DY); | ||
| map.put("dy", dy); | ||
| map.put("Month", MONTH); | ||
| map.put("Mon", MON); | ||
| map.put("MM", MM); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,13 @@ class FormatElementEnumTest { | |
| assertThat(ts, hasToString("Tuesday")); | ||
| } | ||
|
|
||
| @Test void testday() { | ||
|
Contributor
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. Test name should be camelcase |
||
| final String date = "2014-09-30T10:00:00Z"; | ||
| StringBuilder ts = new StringBuilder(); | ||
| FormatElementEnum.day.format(ts, Date.from(Instant.parse(date))); | ||
| assertThat(ts, hasToString("tuesday")); | ||
| } | ||
|
|
||
| @Test void testD() { | ||
| final String date = "2014-09-30T10:00:00Z"; | ||
| StringBuilder ts = new StringBuilder(); | ||
|
|
@@ -63,6 +70,13 @@ class FormatElementEnumTest { | |
| assertThat(ts, hasToString("Tue")); | ||
| } | ||
caicancai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test void testdy() { | ||
|
Contributor
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. same comment as above |
||
| final String date = "2014-09-30T10:00:00Z"; | ||
| StringBuilder ts = new StringBuilder(); | ||
| FormatElementEnum.dy.format(ts, Date.from(Instant.parse(date))); | ||
| assertThat(ts, hasToString("tue")); | ||
| } | ||
|
|
||
| @Test void testFF1() { | ||
| final String date = "2014-09-30T10:00:00.123456Z"; | ||
| StringBuilder ts = new StringBuilder(); | ||
|
|
@@ -174,4 +188,11 @@ class FormatElementEnumTest { | |
| FormatElementEnum.YYYY.format(ts, Date.from(Instant.parse(date))); | ||
| assertThat(ts, hasToString("2014")); | ||
| } | ||
|
|
||
| @Test void testCC() { | ||
| final String date = "2014-09-30T10:00:00Z"; | ||
|
Contributor
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. Instead of adding to the end of the file why don't we make these alphabetical |
||
| StringBuilder ts = new StringBuilder(); | ||
| FormatElementEnum.CC.format(ts, Date.from(Instant.parse(date))); | ||
| assertThat(ts, hasToString("21")); | ||
| } | ||
| } | ||
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.
I think the commit message should probably be more descriptive, I wasn't sure what
Support date formatmeant until I reviewed the PR.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.
+1
I won't even review a PR until the jira case has a good description of its goals. Sounds pedantic but it's very pragmatic.
This jira case also needs to say which functions these date formats are supported in. Changes to FormatElementEnum are not useful unless they are surfaced in functions.