Use correct timezone for auto-generated activity title

This commit is contained in:
Niklas Deutschmann 2026-04-18 20:04:45 +02:00
parent a9249dded4
commit 616b033042
3 changed files with 34 additions and 8 deletions

View file

@ -386,7 +386,8 @@ public class ActivityFileService {
activityTitle = parsedData.getTitle(); activityTitle = parsedData.getTitle();
} else { } else {
// Generate title if not provided // Generate title if not provided
activityTitle = ActivityFormatter.generateActivityTitle(parsedData.getStartTime(), parsedData.getActivityType()); activityTitle = ActivityFormatter.generateActivityTitle(parsedData.getStartTime(), parsedData.getTimezone(),
parsedData.getActivityType());
} }
// Default to PUBLIC if visibility not specified // Default to PUBLIC if visibility not specified

View file

@ -24,7 +24,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -257,7 +256,7 @@ public class FitFileService {
private String generateTitle(ParsedActivityData parsedData) { private String generateTitle(ParsedActivityData parsedData) {
return ActivityFormatter.generateActivityTitle( return ActivityFormatter.generateActivityTitle(
parsedData.getStartTime(), parsedData.getStartTime(),
parsedData.getActivityType() parsedData.getTimezone(), parsedData.getActivityType()
); );
} }

View file

@ -1,13 +1,14 @@
package net.javahippie.fitpub.util; package net.javahippie.fitpub.util;
import lombok.extern.slf4j.Slf4j;
import net.javahippie.fitpub.model.entity.Activity; import net.javahippie.fitpub.model.entity.Activity;
import java.time.LocalDateTime; import java.time.*;
import java.time.LocalTime;
/** /**
* Utility class for formatting activity-related data for display. * Utility class for formatting activity-related data for display.
*/ */
@Slf4j
public class ActivityFormatter { public class ActivityFormatter {
/** /**
@ -48,20 +49,24 @@ public class ActivityFormatter {
* Format: "[Time of Day] [Activity Type]" (e.g., "Morning Run", "Evening Ride") * Format: "[Time of Day] [Activity Type]" (e.g., "Morning Run", "Evening Ride")
* *
* @param startedAt the activity start time * @param startedAt the activity start time
* @param timezone the timezone ID of the activity
* @param activityType the activity type * @param activityType the activity type
* @return generated title * @return generated title
*/ */
public static String generateActivityTitle(LocalDateTime startedAt, Activity.ActivityType activityType) { public static String generateActivityTitle(LocalDateTime startedAt, String timezone, Activity.ActivityType activityType) {
if (startedAt == null || activityType == null) { if (startedAt == null || activityType == null) {
return "Activity"; return "Activity";
} }
String timeOfDay = getTimeOfDay(startedAt.toLocalTime()); LocalDateTime startedAtLocal = getUtcDateTimeInZone(startedAt, timezone);
String timeOfDay = getTimeOfDay(startedAtLocal.toLocalTime());
String formattedType = formatActivityType(activityType); String formattedType = formatActivityType(activityType);
return timeOfDay + " " + formattedType; return timeOfDay + " " + formattedType;
} }
/** /**
* Determines the time of day based on the hour. * Determines the time of day based on the hour.
* *
@ -81,4 +86,25 @@ public class ActivityFormatter {
return "Night"; return "Night";
} }
} }
/**
* Attempts to convert the given LocalDateTime (which is assumed to be UTC) into a LocalDateTime in the given
* timezone
*
* @param utcDateTime The original date and time (UTC)
* @param timezone A timezone ID
* @return The original date and time adjusted to the timezone, if the zone ID could be parsed. The original date
* and time otherwise
*
*/
private static LocalDateTime getUtcDateTimeInZone(LocalDateTime utcDateTime, String timezone) {
try {
return utcDateTime.atZone(ZoneOffset.UTC)
.withZoneSameInstant(ZoneId.of(timezone))
.toLocalDateTime();
} catch (DateTimeException e) {
log.warn("Invalid time zone ID: {}", timezone);
return utcDateTime;
}
}
} }