roundTimestampToSecond function

String roundTimestampToSecond(
  1. String timestamp
)

Rounds a timestamp string to the nearest second by removing subsecond precision.

Takes an ISO 8601 formatted timestamp string and returns it truncated to seconds. If parsing fails, returns the original timestamp string unchanged.

Implementation

String roundTimestampToSecond(String timestamp) {
  try {
    final DateTime dt = DateTime.parse(timestamp);
    return dt.toIso8601String().split('.')[0];
  } catch (e) {
    return timestamp;
  }
}