Skip to content

Commit 35290fa

Browse files
johnyjose30coffeys
authored andcommitted
8339644: Improve parsing of Day/Month in tzdata rules
Backport-of: 86a2f9c
1 parent ce81756 commit 35290fa

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

make/jdk/src/classes/build/tools/tzdb/TzdbZoneRulesProvider.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -364,33 +364,35 @@ int parseYear(String year, int defaultYear) {
364364
}
365365

366366
Month parseMonth(String mon) {
367-
switch (mon) {
368-
case "Jan": return Month.JANUARY;
369-
case "Feb": return Month.FEBRUARY;
370-
case "Mar": return Month.MARCH;
371-
case "Apr": return Month.APRIL;
372-
case "May": return Month.MAY;
373-
case "Jun": return Month.JUNE;
374-
case "Jul": return Month.JULY;
375-
case "Aug": return Month.AUGUST;
376-
case "Sep": return Month.SEPTEMBER;
377-
case "Oct": return Month.OCTOBER;
378-
case "Nov": return Month.NOVEMBER;
379-
case "Dec": return Month.DECEMBER;
380-
}
367+
int len = mon.length();
368+
369+
if (mon.regionMatches(true, 0, "January", 0, len)) return Month.JANUARY;
370+
if (mon.regionMatches(true, 0, "February", 0, len)) return Month.FEBRUARY;
371+
if (mon.regionMatches(true, 0, "March", 0, len)) return Month.MARCH;
372+
if (mon.regionMatches(true, 0, "April", 0, len)) return Month.APRIL;
373+
if (mon.regionMatches(true, 0, "May", 0, len)) return Month.MAY;
374+
if (mon.regionMatches(true, 0, "June", 0, len)) return Month.JUNE;
375+
if (mon.regionMatches(true, 0, "July", 0, len)) return Month.JULY;
376+
if (mon.regionMatches(true, 0, "August", 0, len)) return Month.AUGUST;
377+
if (mon.regionMatches(true, 0, "September", 0, len)) return Month.SEPTEMBER;
378+
if (mon.regionMatches(true, 0, "October", 0, len)) return Month.OCTOBER;
379+
if (mon.regionMatches(true, 0, "November", 0, len)) return Month.NOVEMBER;
380+
if (mon.regionMatches(true, 0, "December", 0, len)) return Month.DECEMBER;
381+
381382
throw new IllegalArgumentException("Unknown month: " + mon);
382383
}
383384

384385
DayOfWeek parseDayOfWeek(String dow) {
385-
switch (dow) {
386-
case "Mon": return DayOfWeek.MONDAY;
387-
case "Tue": return DayOfWeek.TUESDAY;
388-
case "Wed": return DayOfWeek.WEDNESDAY;
389-
case "Thu": return DayOfWeek.THURSDAY;
390-
case "Fri": return DayOfWeek.FRIDAY;
391-
case "Sat": return DayOfWeek.SATURDAY;
392-
case "Sun": return DayOfWeek.SUNDAY;
393-
}
386+
int len = dow.length();
387+
388+
if (dow.regionMatches(true, 0, "Monday", 0, len)) return DayOfWeek.MONDAY;
389+
if (dow.regionMatches(true, 0, "Tuesday", 0, len)) return DayOfWeek.TUESDAY;
390+
if (dow.regionMatches(true, 0, "Wednesday", 0, len)) return DayOfWeek.WEDNESDAY;
391+
if (dow.regionMatches(true, 0, "Thursday", 0, len)) return DayOfWeek.THURSDAY;
392+
if (dow.regionMatches(true, 0, "Friday", 0, len)) return DayOfWeek.FRIDAY;
393+
if (dow.regionMatches(true, 0, "Saturday", 0, len)) return DayOfWeek.SATURDAY;
394+
if (dow.regionMatches(true, 0, "Sunday", 0, len)) return DayOfWeek.SUNDAY;
395+
394396
throw new IllegalArgumentException("Unknown day-of-week: " + dow);
395397
}
396398

test/jdk/sun/util/calendar/zi/Month.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,11 +21,6 @@
2121
* questions.
2222
*/
2323

24-
import java.util.ArrayList;
25-
import java.util.HashMap;
26-
import java.util.List;
27-
import java.util.Map;
28-
2924
/**
3025
* Month enum handles month related manipulation.
3126
*
@@ -47,15 +42,6 @@ enum Month {
4742

4843
private final String abbr;
4944

50-
private static final Map<String,Month> abbreviations
51-
= new HashMap<String,Month>(12);
52-
53-
static {
54-
for (Month m : Month.values()) {
55-
abbreviations.put(m.abbr, m);
56-
}
57-
}
58-
5945
private Month(String abbr) {
6046
this.abbr = abbr;
6147
}
@@ -70,11 +56,22 @@ int value() {
7056
* @return the Month value
7157
*/
7258
static Month parse(String name) {
73-
Month m = abbreviations.get(name);
74-
if (m != null) {
75-
return m;
76-
}
77-
return null;
59+
int len = name.length();
60+
61+
if (name.regionMatches(true, 0, "January", 0, len)) return Month.JANUARY;
62+
if (name.regionMatches(true, 0, "February", 0, len)) return Month.FEBRUARY;
63+
if (name.regionMatches(true, 0, "March", 0, len)) return Month.MARCH;
64+
if (name.regionMatches(true, 0, "April", 0, len)) return Month.APRIL;
65+
if (name.regionMatches(true, 0, "May", 0, len)) return Month.MAY;
66+
if (name.regionMatches(true, 0, "June", 0, len)) return Month.JUNE;
67+
if (name.regionMatches(true, 0, "July", 0, len)) return Month.JULY;
68+
if (name.regionMatches(true, 0, "August", 0, len)) return Month.AUGUST;
69+
if (name.regionMatches(true, 0, "September", 0, len)) return Month.SEPTEMBER;
70+
if (name.regionMatches(true, 0, "October", 0, len)) return Month.OCTOBER;
71+
if (name.regionMatches(true, 0, "November", 0, len)) return Month.NOVEMBER;
72+
if (name.regionMatches(true, 0, "December", 0, len)) return Month.DECEMBER;
73+
74+
throw new IllegalArgumentException("Unknown month: " + name);
7875
}
7976

8077
/**

test/jdk/sun/util/calendar/zi/RuleDay.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,11 +21,6 @@
2121
* questions.
2222
*/
2323

24-
import java.util.ArrayList;
25-
import java.util.HashMap;
26-
import java.util.List;
27-
import java.util.Map;
28-
2924
/**
3025
* RuleDay class represents the value of the "ON" field. The day of
3126
* week values start from 1 following the {@link java.util.Calendar}
@@ -34,13 +29,6 @@
3429
* @since 1.4
3530
*/
3631
class RuleDay {
37-
private static final Map<String,DayOfWeek> abbreviations = new HashMap<String,DayOfWeek>(7);
38-
static {
39-
for (DayOfWeek day : DayOfWeek.values()) {
40-
abbreviations.put(day.getAbbr(), day);
41-
}
42-
}
43-
4432
private String dayName = null;
4533
private DayOfWeek dow;
4634
private boolean lastOne = false;
@@ -166,13 +154,23 @@ String getDayOfWeekForSimpleTimeZone() {
166154
return sign + toString(d);
167155
}
168156

169-
private static DayOfWeek getDOW(String abbr) {
170-
return abbreviations.get(abbr);
157+
private static DayOfWeek getDOW(String name) {
158+
int len = name.length();
159+
160+
if (name.regionMatches(true, 0, "Monday", 0, len)) return DayOfWeek.MONDAY;
161+
if (name.regionMatches(true, 0, "Tuesday", 0, len)) return DayOfWeek.TUESDAY;
162+
if (name.regionMatches(true, 0, "Wednesday", 0, len)) return DayOfWeek.WEDNESDAY;
163+
if (name.regionMatches(true, 0, "Thursday", 0, len)) return DayOfWeek.THURSDAY;
164+
if (name.regionMatches(true, 0, "Friday", 0, len)) return DayOfWeek.FRIDAY;
165+
if (name.regionMatches(true, 0, "Saturday", 0, len)) return DayOfWeek.SATURDAY;
166+
if (name.regionMatches(true, 0, "Sunday", 0, len)) return DayOfWeek.SUNDAY;
167+
168+
throw new IllegalArgumentException("Unknown day-of-week: " + name);
171169
}
172170

173171
/**
174172
* Converts the specified day of week value to the day-of-week
175-
* name defined in {@link java.util.Calenda}.
173+
* name defined in {@link java.util.Calendar}.
176174
* @param dow 1-based day of week value
177175
* @return the Calendar day of week name with "Calendar." prefix.
178176
* @throws IllegalArgumentException if the specified dow value is out of range.

0 commit comments

Comments
 (0)