Skip to content

Commit 2ad5202

Browse files
greutDaanHoogland
authored andcommitted
dateutil: constistency of tzdate input and output (#2392)
Signed-off-by: Yoan Blanc <yoan.blanc@exoscale.ch> Signed-off-by: Daan Hoogland <daan.hoogland@shapeblue.com>
1 parent 5a5b135 commit 2ad5202

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

utils/src/main/java/com/cloud/utils/DateUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ public static Date currentGMTTime() {
3838
return new Date();
3939
}
4040

41-
// yyyy-MM-ddTHH:mm:ssZxxxx
41+
// yyyy-MM-ddTHH:mm:ssZZZZ or yyyy-MM-ddTHH:mm:ssZxxxx
4242
public static Date parseTZDateString(String str) throws ParseException {
43-
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
44-
return dfParse.parse(str);
43+
try {
44+
return s_outputFormat.parse(str);
45+
} catch (ParseException e) {
46+
final DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
47+
return dfParse.parse(str);
48+
}
4549
}
4650

4751
public static Date parseDateString(TimeZone tz, String dateString) {

utils/src/test/java/com/cloud/utils/DateUtilTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
import com.cloud.utils.DateUtil.IntervalType;
2929

30+
import org.junit.Test;
31+
32+
import static org.junit.Assert.assertEquals;
3033

3134
public class DateUtilTest {
3235

@@ -44,17 +47,25 @@ public static void main(String[] args) {
4447
if (args.length == 2) {
4548
System.out.println("Next run time: " + DateUtil.getNextRunTime(IntervalType.getIntervalType(args[0]), args[1], "GMT", time).toString());
4649
}
50+
}
4751

48-
time = new Date();
52+
@Test
53+
public void zonedTimeFormatLegacy() throws ParseException {
54+
Date time = new Date();
4955
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
5056
String str = dfDate.format(time);
51-
System.out.println("Formated TZ time string : " + str);
52-
try {
53-
Date dtParsed = DateUtil.parseTZDateString(str);
54-
System.out.println("Parsed TZ time string : " + dtParsed.toString());
55-
} catch (ParseException e) {
56-
System.err.println("Parsing failed\n string : " + str + "\nexception :" + e.getLocalizedMessage());
57-
}
57+
Date dtParsed = DateUtil.parseTZDateString(str);
58+
59+
assertEquals(time.toString(), dtParsed.toString());
5860
}
5961

62+
@Test
63+
public void zonedTimeFormat() throws ParseException {
64+
Date time = new Date();
65+
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
66+
String str = dfDate.format(time);
67+
Date dtParsed = DateUtil.parseTZDateString(str);
68+
69+
assertEquals(time.toString(), dtParsed.toString());
70+
}
6071
}

0 commit comments

Comments
 (0)