Skip to content

Commit cc52752

Browse files
Allow users to inform timezones on APIs that have the date parameter (#7032)
Co-authored-by: dahn <daan.hoogland@gmail.com>
1 parent 9009dd1 commit cc52752

File tree

4 files changed

+37
-65
lines changed

4 files changed

+37
-65
lines changed

api/src/main/java/org/apache/cloudstack/api/BaseCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static enum HTTPMethod {
103103
GET, POST, PUT, DELETE
104104
}
105105
public static enum CommandType {
106-
BOOLEAN, DATE, FLOAT, DOUBLE, INTEGER, SHORT, LIST, LONG, OBJECT, MAP, STRING, TZDATE, UUID
106+
BOOLEAN, DATE, FLOAT, DOUBLE, INTEGER, SHORT, LIST, LONG, OBJECT, MAP, STRING, UUID
107107
}
108108

109109
private Object _responseObject;

api/src/main/java/org/apache/cloudstack/api/command/user/job/ListAsyncJobsCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ListAsyncJobsCmd extends BaseListAccountResourcesCmd {
3333
//////////////// API parameters /////////////////////
3434
/////////////////////////////////////////////////////
3535

36-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.TZDATE, description = "The start date of the async job (use format \"yyyy-MM-dd'T'HH:mm:ss'+'SSSS\")")
36+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The start date of the async job (use format \"yyyy-MM-dd'T'HH:mm:ss'+'SSSS\")")
3737
private Date startDate;
3838

3939
/////////////////////////////////////////////////////

server/src/main/java/com/cloud/api/dispatch/ParamProcessWorker.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@
6363
public class ParamProcessWorker implements DispatchWorker {
6464

6565
private static final Logger s_logger = Logger.getLogger(ParamProcessWorker.class.getName());
66-
public final DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
67-
public final DateFormat newInputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
66+
private static final String inputFormatString = "yyyy-MM-dd";
67+
private static final String newInputFormatString = "yyyy-MM-dd HH:mm:ss";
68+
public static final DateFormat inputFormat = new SimpleDateFormat(inputFormatString);
69+
public static final DateFormat newInputFormat = new SimpleDateFormat(newInputFormatString);
6870

6971
@Inject
7072
protected AccountManager _accountMgr;
@@ -333,26 +335,7 @@ private void setFieldValue(final Field field, final BaseCmd cmdObj, final Object
333335
field.set(cmdObj, Boolean.valueOf(paramObj.toString()));
334336
break;
335337
case DATE:
336-
// This piece of code is for maintaining backward compatibility
337-
// and support both the date formats(Bug 9724)
338-
final boolean isObjInNewDateFormat = isObjInNewDateFormat(paramObj.toString());
339-
if (isObjInNewDateFormat) {
340-
final DateFormat newFormat = newInputFormat;
341-
synchronized (newFormat) {
342-
field.set(cmdObj, newFormat.parse(paramObj.toString()));
343-
}
344-
} else {
345-
final DateFormat format = inputFormat;
346-
synchronized (format) {
347-
Date date = format.parse(paramObj.toString());
348-
if (field.getName().equals("startDate")) {
349-
date = messageDate(date, 0, 0, 0);
350-
} else if (field.getName().equals("endDate")) {
351-
date = messageDate(date, 23, 59, 59);
352-
}
353-
field.set(cmdObj, date);
354-
}
355-
}
338+
parseAndSetDate(field, cmdObj, paramObj);
356339
break;
357340
case FLOAT:
358341
// Assuming that the parameters have been checked for required before now,
@@ -428,9 +411,6 @@ private void setFieldValue(final Field field, final BaseCmd cmdObj, final Object
428411
}
429412
}
430413
break;
431-
case TZDATE:
432-
field.set(cmdObj, DateUtil.parseTZDateString(paramObj.toString()));
433-
break;
434414
case MAP:
435415
default:
436416
field.set(cmdObj, paramObj);
@@ -442,6 +422,33 @@ private void setFieldValue(final Field field, final BaseCmd cmdObj, final Object
442422
" is not accessible]");
443423
}
444424
}
425+
private void parseAndSetDate(Field field, BaseCmd cmdObj, Object paramObj) throws IllegalAccessException, ParseException {
426+
try {
427+
field.set(cmdObj, DateUtil.parseTZDateString(paramObj.toString()));
428+
return;
429+
} catch (ParseException parseException) {
430+
s_logger.debug(String.format("Could not parse date [%s] with timezone parser, trying to parse without timezone.", paramObj));
431+
}
432+
if (isObjInNewDateFormat(paramObj.toString())) {
433+
s_logger.debug(String.format("Parsing date [%s] using the [%s] format.", paramObj, newInputFormatString));
434+
final DateFormat newFormat = newInputFormat;
435+
synchronized (newFormat) {
436+
field.set(cmdObj, newFormat.parse(paramObj.toString()));
437+
}
438+
} else {
439+
s_logger.debug(String.format("Parsing date [%s] using the [%s] format.", paramObj, inputFormatString));
440+
final DateFormat format = inputFormat;
441+
synchronized (format) {
442+
Date date = format.parse(paramObj.toString());
443+
if (field.getName().equals("startDate")) {
444+
date = messageDate(date, 0, 0, 0);
445+
} else if (field.getName().equals("endDate")) {
446+
date = messageDate(date, 23, 59, 59);
447+
}
448+
field.set(cmdObj, date);
449+
}
450+
}
451+
}
445452

446453
private boolean isObjInNewDateFormat(final String string) {
447454
final Matcher matcher = BaseCmd.newInputDateFormat.matcher(string);

ui/src/components/view/StatsTab.vue

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -289,26 +289,12 @@ export default {
289289
mounted () {
290290
this.fetchData()
291291
},
292-
computed: {
293-
usebrowsertimezone: function () {
294-
return this.$store.getters.usebrowsertimezone
295-
}
296-
},
297292
watch: {
298293
resource: function (newItem) {
299294
if (!newItem || !newItem.id) {
300295
return
301296
}
302297
this.fetchData()
303-
},
304-
usebrowsertimezone: function () {
305-
if (this.startDate) {
306-
this.startDate = this.onToggleUseBrowserTimezone(new Date(this.startDate))
307-
}
308-
if (this.endDate) {
309-
this.endDate = this.onToggleUseBrowserTimezone(new Date(this.endDate))
310-
}
311-
this.fetchData()
312298
}
313299
},
314300
methods: {
@@ -355,43 +341,22 @@ export default {
355341
},
356342
getStartDate () {
357343
var now = new Date()
358-
if (!this.$store.getters.usebrowsertimezone) {
359-
var dateInUTC = new Date(now.getTime() + now.getTimezoneOffset() * 60000)
360-
return dateInUTC.setHours(dateInUTC.getHours() - 1)
361-
}
362344
now.setHours(now.getHours() - 1)
363345
return now
364346
},
365347
getEndDate () {
366-
var now = new Date()
367-
if (this.$store.getters.usebrowsertimezone) {
368-
return now
369-
}
370-
return new Date(now.getTime() + now.getTimezoneOffset() * 60000)
371-
},
372-
onToggleUseBrowserTimezone (date) {
373-
if (this.$store.getters.usebrowsertimezone) {
374-
return this.$toLocalDate(date)
375-
}
376-
return new Date(date.getTime() + date.getTimezoneOffset() * 60000)
377-
},
378-
convertAndFormatDateAppropriately (date) {
379-
if (this.$store.getters.usebrowsertimezone) {
380-
var dateInUTC = new Date(date).toISOString().split('T')
381-
return dateInUTC[0] + ' ' + dateInUTC[1].split('-')[0].split('.')[0]
382-
}
383-
return moment(date).format('YYYY-MM-DD HH:mm:ss')
348+
return new Date()
384349
},
385350
fetchData () {
386351
this.loaded = false
387352
this.showResourceInfoModal = false
388353
this.formatPeriod()
389354
var params = { id: this.resource.id }
390355
if (this.startDate) {
391-
params.startDate = this.convertAndFormatDateAppropriately(this.startDate)
356+
params.startDate = moment(this.startDate).format()
392357
}
393358
if (this.endDate) {
394-
params.endDate = this.convertAndFormatDateAppropriately(this.endDate)
359+
params.endDate = moment(this.endDate).format()
395360
}
396361
api('listVirtualMachinesUsageHistory', params).then(response => {
397362
this.handleStatsResponse(response)

0 commit comments

Comments
 (0)