Skip to content

Commit 02e22ad

Browse files
committed
Refactor date formatting in MetricsHelper to use a dedicated method for Azure Monitor Metrics API
1 parent c19067c commit 02e22ad

File tree

1 file changed

+23
-10
lines changed
  • sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/metrics/models

1 file changed

+23
-10
lines changed

sdk/monitor/azure-monitor-query/src/main/java/com/azure/monitor/query/implementation/metrics/models/MetricsHelper.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,33 +330,46 @@ public static String toMetricsTimespan(QueryTimeInterval timeInterval) {
330330

331331
// If we have both start and end times, use them directly
332332
if (timeInterval.getStartTime() != null && timeInterval.getEndTime() != null) {
333-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(timeInterval.getStartTime()) + "/"
334-
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(timeInterval.getEndTime());
333+
return formatForMetrics(timeInterval.getStartTime()) + "/" + formatForMetrics(timeInterval.getEndTime());
335334
}
336335

337336
// If we have start time and duration, calculate end time
338337
if (timeInterval.getStartTime() != null && timeInterval.getDuration() != null) {
339-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(timeInterval.getStartTime()) + "/"
340-
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME
341-
.format(timeInterval.getStartTime().plus(timeInterval.getDuration()));
338+
return formatForMetrics(timeInterval.getStartTime()) + "/"
339+
+ formatForMetrics(timeInterval.getStartTime().plus(timeInterval.getDuration()));
342340
}
343341

344342
// If we have duration and end time, calculate start time
345343
if (timeInterval.getDuration() != null && timeInterval.getEndTime() != null) {
346-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME
347-
.format(timeInterval.getEndTime().minus(timeInterval.getDuration())) + "/"
348-
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(timeInterval.getEndTime());
344+
return formatForMetrics(timeInterval.getEndTime().minus(timeInterval.getDuration())) + "/"
345+
+ formatForMetrics(timeInterval.getEndTime());
349346
}
350347

351348
// If we only have duration, calculate absolute start and end times based on current time
352349
if (timeInterval.getDuration() != null) {
353350
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
354351
OffsetDateTime startTime = now.minus(timeInterval.getDuration());
355-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(startTime) + "/"
356-
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now);
352+
return formatForMetrics(startTime) + "/" + formatForMetrics(now);
357353
}
358354

359355
return null;
360356
}
361357

358+
/**
359+
* Formats an OffsetDateTime for Azure Monitor Metrics API.
360+
* Uses ISO format but omits seconds when they are zero.
361+
*
362+
* @param dateTime The datetime to format.
363+
* @return Formatted string.
364+
*/
365+
private static String formatForMetrics(OffsetDateTime dateTime) {
366+
if (dateTime.getSecond() == 0 && dateTime.getNano() == 0) {
367+
// Format without seconds when they are zero: 2025-01-01T00:00Z
368+
return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmXXX"));
369+
} else {
370+
// Format with full precision when seconds are non-zero
371+
return dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
372+
}
373+
}
374+
362375
}

0 commit comments

Comments
 (0)