Skip to content

Commit b417fed

Browse files
SilverDavmmoqui
authored andcommitted
bug #14769 : The start hour, the end Hour and the user timezone are now well processed
1 parent 826ea74 commit b417fed

File tree

1 file changed

+36
-33
lines changed
  • almanach/almanach-library/src/main/java/org/silverpeas/components/almanach/workflowextensions

1 file changed

+36
-33
lines changed

almanach/almanach-library/src/main/java/org/silverpeas/components/almanach/workflowextensions/SendInAlmanach.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@
4040
import javax.inject.Named;
4141
import java.time.LocalDate;
4242
import java.time.LocalDateTime;
43-
import java.time.OffsetDateTime;
4443
import java.time.ZoneId;
45-
import java.time.ZoneOffset;
4644
import java.time.format.DateTimeFormatter;
4745
import java.time.format.DateTimeParseException;
4846
import java.time.temporal.ChronoUnit;
4947
import java.time.temporal.Temporal;
50-
import java.time.zone.ZoneRules;
51-
import java.util.TimeZone;
5248

5349
import static java.time.format.DateTimeFormatter.ofPattern;
5450
import static org.silverpeas.core.SilverpeasExceptionMessages.unknown;
@@ -145,42 +141,39 @@ private Period getEventPeriod() {
145141
zoneId = ZoneId.systemDefault();
146142
}
147143

148-
ZoneOffset zoneOffset = zoneId.getRules().getOffset(LocalDateTime.now());
149-
150144
final String startDayValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.START_DATE);
151145
final String endDayValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.END_DATE);
152-
if (startDayValue != null) {
153-
final String startHourValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.START_HOUR);
154-
final String endHourValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.END_HOUR);
155-
try {
156-
final Temporal start;
157-
if (StringUtil.isValidHour(startHourValue)) {
158-
var localDateTime = LocalDateTime.parse(startDayValue + " " + startHourValue, DATE_TIME_FORMATTER);
159-
start = localDateTime.atOffset(zoneOffset);
146+
if (StringUtil.isNotDefined(startDayValue)) {
147+
return null;
148+
}
149+
150+
final String startHourValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.START_HOUR);
151+
final String endHourValue = getFolderValueFromTriggerParam(AlmanachTriggerParam.END_HOUR);
152+
try {
153+
final Temporal start;
154+
if (StringUtil.isValidHour(startHourValue)) {
155+
start = getTemporalWithOffset(startDayValue, startHourValue, zoneId);
156+
} else {
157+
start = LocalDate.parse(startDayValue, DATE_FORMATTER);
158+
}
159+
160+
final Temporal end;
161+
if (endDayValue != null) {
162+
if (StringUtil.isValidHour(endHourValue)) {
163+
end = getTemporalWithOffset(endDayValue, endHourValue, zoneId);
160164
} else {
161-
start = LocalDate.parse(startDayValue, DATE_FORMATTER);
165+
end = LocalDate.parse(endDayValue, DATE_FORMATTER).plusDays(1);
162166
}
163-
164-
final Temporal end;
165-
if (endDayValue != null) {
166-
if (StringUtil.isValidHour(endHourValue)) {
167-
var localDateTime = LocalDateTime.parse(endDayValue + " " + endHourValue, DATE_TIME_FORMATTER);
168-
end = localDateTime.atOffset(zoneOffset);
169-
} else {
170-
end = LocalDate.parse(endDayValue, DATE_FORMATTER).plusDays(1);
171-
}
167+
} else {
168+
if (StringUtil.isValidHour(endHourValue)) {
169+
end = getTemporalWithOffset(startDayValue, endHourValue, zoneId);
172170
} else {
173-
if (StringUtil.isValidHour(endHourValue)) {
174-
var localDateTime = LocalDateTime.parse(startDayValue + " " + endHourValue, DATE_TIME_FORMATTER);
175-
end = localDateTime.atOffset(zoneOffset);
176-
} else {
177-
end = start.plus(1, ChronoUnit.DAYS);
178-
}
171+
end = start.plus(1, ChronoUnit.DAYS);
179172
}
180-
return Period.between(start, end);
181-
} catch (DateTimeParseException e) {
182-
SilverLogger.getLogger(this).warn(e);
183173
}
174+
return Period.between(start, end);
175+
} catch (DateTimeParseException e) {
176+
SilverLogger.getLogger(this).warn(e);
184177
}
185178
return null;
186179
}
@@ -229,4 +222,14 @@ private OrganizationController getOrganizationController() {
229222
return organizationController;
230223
}
231224

225+
/** Get date with offset due to Timezone
226+
* @param day day of the event
227+
* @param hour of the event
228+
* @param zoneId Timezone of the user who has created the event
229+
**/
230+
private Temporal getTemporalWithOffset(String day, String hour, ZoneId zoneId) {
231+
var localDateTime = LocalDateTime.parse(day + " " + hour, DATE_TIME_FORMATTER);
232+
var zoneOffset = ZoneId.of(zoneId.getId()).getRules().getOffset(localDateTime.atZone(zoneId).toInstant());
233+
return localDateTime.atOffset(zoneOffset);
234+
}
232235
}

0 commit comments

Comments
 (0)