Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)") {
Copy link
Contributor

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 format meant until I reviewed the PR.

Copy link
Contributor

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.

@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;
Expand All @@ -54,6 +61,12 @@ public enum FormatElementEnum implements FormatElement {
sb.append(work.eeeeFormat.format(date));
}
},
day("eeee", "The full lower case day name") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum constants should be all uppercase I believe.
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

@Override public void format(StringBuilder sb, Date date) {
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;
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports here should be in alphabetical order.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
import static org.apache.calcite.util.format.FormatElementEnum.*; ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't checkstyle complain if the imports are in the wrong order?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class FormatElementEnumTest {
assertThat(ts, hasToString("Tuesday"));
}

@Test void testday() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -63,6 +70,13 @@ class FormatElementEnumTest {
assertThat(ts, hasToString("Tue"));
}

@Test void testdy() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The 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"));
}
}