Use correct timezone for auto-generated activity title
This commit is contained in:
parent
5f85417c80
commit
583a0a4112
3 changed files with 34 additions and 8 deletions
|
|
@ -386,7 +386,8 @@ public class ActivityFileService {
|
|||
activityTitle = parsedData.getTitle();
|
||||
} else {
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -257,7 +256,7 @@ public class FitFileService {
|
|||
private String generateTitle(ParsedActivityData parsedData) {
|
||||
return ActivityFormatter.generateActivityTitle(
|
||||
parsedData.getStartTime(),
|
||||
parsedData.getActivityType()
|
||||
parsedData.getTimezone(), parsedData.getActivityType()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
package net.javahippie.fitpub.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.javahippie.fitpub.model.entity.Activity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.*;
|
||||
|
||||
/**
|
||||
* Utility class for formatting activity-related data for display.
|
||||
*/
|
||||
@Slf4j
|
||||
public class ActivityFormatter {
|
||||
|
||||
/**
|
||||
|
|
@ -47,21 +48,25 @@ public class ActivityFormatter {
|
|||
* Generates a default activity title based on the time of day and activity type.
|
||||
* 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
|
||||
* @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) {
|
||||
return "Activity";
|
||||
}
|
||||
|
||||
String timeOfDay = getTimeOfDay(startedAt.toLocalTime());
|
||||
LocalDateTime startedAtLocal = getUtcDateTimeInZone(startedAt, timezone);
|
||||
String timeOfDay = getTimeOfDay(startedAtLocal.toLocalTime());
|
||||
String formattedType = formatActivityType(activityType);
|
||||
|
||||
return timeOfDay + " " + formattedType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Determines the time of day based on the hour.
|
||||
*
|
||||
|
|
@ -81,4 +86,25 @@ public class ActivityFormatter {
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue