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>
This commit is contained in:
Yoan Blanc 2018-02-28 12:28:10 +01:00 committed by dahn
parent 5a5b1354bd
commit 2ad5202823
2 changed files with 29 additions and 14 deletions

View File

@ -38,10 +38,14 @@ public class DateUtil {
return new Date();
}
// yyyy-MM-ddTHH:mm:ssZxxxx
// yyyy-MM-ddTHH:mm:ssZZZZ or yyyy-MM-ddTHH:mm:ssZxxxx
public static Date parseTZDateString(String str) throws ParseException {
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
return dfParse.parse(str);
try {
return s_outputFormat.parse(str);
} catch (ParseException e) {
final DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
return dfParse.parse(str);
}
}
public static Date parseDateString(TimeZone tz, String dateString) {

View File

@ -27,6 +27,9 @@ import java.util.TimeZone;
import com.cloud.utils.DateUtil.IntervalType;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DateUtilTest {
@ -44,17 +47,25 @@ public class DateUtilTest {
if (args.length == 2) {
System.out.println("Next run time: " + DateUtil.getNextRunTime(IntervalType.getIntervalType(args[0]), args[1], "GMT", time).toString());
}
time = new Date();
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
String str = dfDate.format(time);
System.out.println("Formated TZ time string : " + str);
try {
Date dtParsed = DateUtil.parseTZDateString(str);
System.out.println("Parsed TZ time string : " + dtParsed.toString());
} catch (ParseException e) {
System.err.println("Parsing failed\n string : " + str + "\nexception :" + e.getLocalizedMessage());
}
}
@Test
public void zonedTimeFormatLegacy() throws ParseException {
Date time = new Date();
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
String str = dfDate.format(time);
Date dtParsed = DateUtil.parseTZDateString(str);
assertEquals(time.toString(), dtParsed.toString());
}
@Test
public void zonedTimeFormat() throws ParseException {
Date time = new Date();
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String str = dfDate.format(time);
Date dtParsed = DateUtil.parseTZDateString(str);
assertEquals(time.toString(), dtParsed.toString());
}
}