Skip to content

Commit

Permalink
Reverted timezone consideration on appointment services dates
Browse files Browse the repository at this point in the history
  • Loading branch information
icrc-psousa committed Jan 3, 2024
1 parent e5a0a3b commit b50dd26
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.openmrs.module.appointments.model.AppointmentConflictType.SERVICE_UNAVAILABLE;
import static org.openmrs.module.appointments.util.DateUtil.getEpochTimeUTC;
import static org.openmrs.module.appointments.util.DateUtil.getEpochTime;

public class AppointmentServiceUnavailabilityConflict implements AppointmentConflict {

private final static String DAY_OF_WEEK_PATTERN = "EEEE";
private final SimpleDateFormat DayFormat = new SimpleDateFormat(DAY_OF_WEEK_PATTERN, Locale.ENGLISH);
private final SimpleDateFormat DayFormat = new SimpleDateFormat(DAY_OF_WEEK_PATTERN);

@Override
public AppointmentConflictType getType() {
Expand Down Expand Up @@ -50,8 +55,8 @@ private boolean checkConflicts(Appointment appointment, AppointmentServiceDefini
}
Time serviceStartTime = appointmentServiceDefinition.getStartTime();
Time serviceEndTime = appointmentServiceDefinition.getEndTime();
long serviceStartMillis = serviceStartTime != null ? serviceStartTime.getTime() : DateUtil.getStartOfDayUTC().getTime();
long serviceEndMillis = serviceEndTime != null ? serviceEndTime.getTime() : DateUtil.getEndOfDayUTC().getTime();
long serviceStartMillis = serviceStartTime != null ? serviceStartTime.getTime() : DateUtil.getStartOfDay().getTime();
long serviceEndMillis = serviceEndTime != null ? serviceEndTime.getTime() : DateUtil.getEndOfDay().getTime();
return checkTimeAvailability(appointment, serviceStartMillis, serviceEndMillis);
}

Expand All @@ -60,10 +65,10 @@ private boolean isObjectPresent(Collection<?> object) {
}

private boolean checkTimeAvailability(Appointment appointment, long serviceStartTime, long serviceEndTime) {
long appointmentStartTimeMilliSeconds = getEpochTimeUTC(appointment.getStartDateTime().getTime());
long appointmentEndTimeMilliSeconds = getEpochTimeUTC(appointment.getEndDateTime().getTime());
long serviceStartTimeMilliSeconds = getEpochTimeUTC(serviceStartTime);
long serviceEndTimeMilliSeconds = getEpochTimeUTC(serviceEndTime);
long appointmentStartTimeMilliSeconds = getEpochTime(appointment.getStartDateTime().getTime());
long appointmentEndTimeMilliSeconds = getEpochTime(appointment.getEndDateTime().getTime());
long serviceStartTimeMilliSeconds = getEpochTime(serviceStartTime);
long serviceEndTimeMilliSeconds = getEpochTime(serviceEndTime);
boolean isConflict = (appointmentStartTimeMilliSeconds >= appointmentEndTimeMilliSeconds)
|| ((appointmentStartTimeMilliSeconds < serviceStartTimeMilliSeconds)
|| (appointmentEndTimeMilliSeconds > serviceEndTimeMilliSeconds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ public static Date getStartOfDay() {
return calendar.getTime();
}

public static Date getStartOfDayUTC() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getMinimum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getMinimum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getMinimum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getMinimum(Calendar.MILLISECOND));
return calendar.getTime();
}

public static long getEpochTime(long date) {
Calendar calendar = getCalendar(new Date(date));
int hours = calendar.get(Calendar.HOUR_OF_DAY);
Expand All @@ -75,16 +65,6 @@ public static long getEpochTime(long date) {
return milliSeconds;
}

public static long getEpochTimeUTC(long date) {
Calendar calendar = getCalendar(new Date(date));
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
long milliSeconds = ((hours * 3600 + minutes * 60 + seconds) * 1000);
return milliSeconds;
}

public static Date getEndOfDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY));
Expand All @@ -93,14 +73,6 @@ public static Date getEndOfDay() {
calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));
return calendar.getTime();
}

public static Date getEndOfDayUTC() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));
return calendar.getTime();
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,17 @@ public void shouldNotHaveAnyServiceUnavailableConflicts() {
Appointment appointment = new Appointment();
appointment.setService(appointmentServiceDefinition);
//Tuesday Appointment
appointment.setStartDateTime(getDate(2019, Calendar.JANUARY, 24, 11, 30, 0));
appointment.setEndDateTime(getDate(2019, Calendar.JANUARY, 24, 12, 0, 0));
appointment.setStartDateTime(getDate(2019, 8, 24, 11, 30, 0));
appointment.setEndDateTime(getDate(2019, 8, 24, 12, 0, 0));
appointment.setAppointmentId(2);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date startTimeUTC = new Date(sdf.format(new Time(8, 30, 0)));
Date endTimeUTC = new Date(sdf.format(new Time(17, 30, 0)));
Date startTimeUTC2 = new Date(sdf.format(new Time(8, 30, 0)));
Date endTimeUTC2 = new Date(sdf.format(new Time(17, 30, 0)));
ServiceWeeklyAvailability day1 = new ServiceWeeklyAvailability();
day1.setStartTime(new Time(startTimeUTC.getHours(), startTimeUTC.getMinutes(), startTimeUTC.getSeconds()));
day1.setEndTime(new Time(endTimeUTC.getHours(), endTimeUTC.getMinutes(), endTimeUTC.getSeconds()));
day1.setDayOfWeek(DayOfWeek.THURSDAY);
day1.setStartTime(new Time(8, 30, 0));
day1.setEndTime(new Time(17, 30, 0));
day1.setDayOfWeek(DayOfWeek.MONDAY);
ServiceWeeklyAvailability day2 = new ServiceWeeklyAvailability();
day2.setStartTime(new Time(startTimeUTC2.getHours(), startTimeUTC2.getMinutes(), startTimeUTC2.getSeconds()));
day2.setEndTime(new Time(endTimeUTC2.getHours(), endTimeUTC2.getMinutes(), endTimeUTC2.getSeconds()));
day2.setDayOfWeek(DayOfWeek.FRIDAY);
day2.setStartTime(new Time(8, 30, 0));
day2.setEndTime(new Time(17, 30, 0));
day2.setDayOfWeek(DayOfWeek.TUESDAY);
Set<ServiceWeeklyAvailability> availabilities = new HashSet<>(Arrays.asList(day1, day2));
appointmentServiceDefinition.setWeeklyAvailability(availabilities);

Expand Down Expand Up @@ -100,20 +94,13 @@ public void shouldReturnServiceUnavailableTimeSlotConflict() {
appointmentThree.setStartDateTime(getDate(2019, 8, 23, 16, 30, 0));
appointmentThree.setEndDateTime(getDate(2019, 8, 23, 17, 1, 0));
appointmentThree.setAppointmentId(4);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date startTimeUTC = new Date(sdf.format(new Time(8, 30, 0)));
Date endTimeUTC = new Date(sdf.format(new Time(17, 0, 0)));
Date startTimeUTC2 = new Date(sdf.format(new Time(8, 30, 0)));
Date endTimeUTC2 = new Date(sdf.format(new Time(17, 0, 0)));
ServiceWeeklyAvailability day1 = new ServiceWeeklyAvailability();
day1.setStartTime(new Time(startTimeUTC.getHours(), startTimeUTC.getMinutes(), startTimeUTC.getSeconds()));
day1.setEndTime(new Time(endTimeUTC.getHours(), endTimeUTC.getMinutes(), endTimeUTC.getSeconds()));
day1.setStartTime(new Time(8, 30, 0));
day1.setEndTime(new Time(17, 0, 0));
day1.setDayOfWeek(DayOfWeek.MONDAY);
ServiceWeeklyAvailability day2 = new ServiceWeeklyAvailability();
day2.setStartTime(new Time(startTimeUTC2.getHours(), startTimeUTC2.getMinutes(), startTimeUTC2.getSeconds()));
day2.setEndTime(new Time(endTimeUTC2.getHours(), endTimeUTC2.getMinutes(), endTimeUTC2.getSeconds()));
day2.setStartTime(new Time(8, 30, 0));
day2.setEndTime(new Time(17, 0, 0));
day2.setDayOfWeek(DayOfWeek.TUESDAY);
Set<ServiceWeeklyAvailability> availabilities = new HashSet<>(Arrays.asList(day1, day2));
appointmentServiceDefinition.setWeeklyAvailability(availabilities);
Expand All @@ -134,27 +121,27 @@ public void shouldNotReturnServiceUnavailableConflictsForMoreSlotsInSingleDay()
// All Appointments are on Monday
Appointment appointmentOne = new Appointment();
appointmentOne.setService(appointmentServiceDefinition);
appointmentOne.setStartDateTime(getDate(2019, Calendar.JANUARY, 1, 6, 30, 0));
appointmentOne.setEndDateTime(getDate(2019, Calendar.JANUARY, 1, 7, 0, 0));
appointmentOne.setStartDateTime(getDate(2019, 8, 23, 6, 30, 0));
appointmentOne.setEndDateTime(getDate(2019, 8, 23, 7, 0, 0));
appointmentOne.setAppointmentId(2);
Appointment appointmentTwo = new Appointment();
appointmentTwo.setService(appointmentServiceDefinition);
appointmentTwo.setStartDateTime(getDate(2019, Calendar.JANUARY, 1, 16, 30, 0));
appointmentTwo.setEndDateTime(getDate(2019, Calendar.JANUARY, 1, 17, 30, 0));
appointmentTwo.setStartDateTime(getDate(2019, 8, 23, 16, 30, 0));
appointmentTwo.setEndDateTime(getDate(2019, 8, 23, 17, 30, 0));
appointmentTwo.setAppointmentId(3);
Appointment appointmentThree = new Appointment();
appointmentThree.setService(appointmentServiceDefinition);
appointmentThree.setStartDateTime(getDate(2019, Calendar.JANUARY, 1, 16, 30, 0));
appointmentThree.setEndDateTime(getDate(2019, Calendar.JANUARY, 1, 17, 0, 0));
appointmentThree.setStartDateTime(getDate(2019, 8, 23, 16, 30, 0));
appointmentThree.setEndDateTime(getDate(2019, 8, 23, 17, 0, 0));
appointmentThree.setAppointmentId(4);
ServiceWeeklyAvailability day1 = new ServiceWeeklyAvailability();
day1.setStartTime(new Time(6, 30, 0));
day1.setEndTime(new Time(14, 0, 0));
day1.setDayOfWeek(DayOfWeek.TUESDAY);
day1.setDayOfWeek(DayOfWeek.MONDAY);
ServiceWeeklyAvailability day2 = new ServiceWeeklyAvailability();
day2.setStartTime(new Time(16, 30, 0));
day2.setEndTime(new Time(19, 0, 0));
day2.setDayOfWeek(DayOfWeek.TUESDAY);
day2.setDayOfWeek(DayOfWeek.MONDAY);
Set<ServiceWeeklyAvailability> availabilities = new HashSet<>(Arrays.asList(day1, day2));
appointmentServiceDefinition.setWeeklyAvailability(availabilities);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -41,8 +40,8 @@ public AppointmentServiceDefinition fromDescription(AppointmentServiceDescriptio
appointmentServiceDefinition.setName(appointmentServiceDescription.getName());
appointmentServiceDefinition.setDescription(appointmentServiceDescription.getDescription());
appointmentServiceDefinition.setDurationMins(appointmentServiceDescription.getDurationMins());
appointmentServiceDefinition.setStartTime(utcTimeToServerTime(appointmentServiceDescription.getStartTime()));
appointmentServiceDefinition.setEndTime(utcTimeToServerTime(appointmentServiceDescription.getEndTime()));
appointmentServiceDefinition.setStartTime(appointmentServiceDescription.getStartTime());
appointmentServiceDefinition.setEndTime(appointmentServiceDescription.getEndTime());
appointmentServiceDefinition.setMaxAppointmentsLimit(appointmentServiceDescription.getMaxAppointmentsLimit());
appointmentServiceDefinition.setColor(appointmentServiceDescription.getColor());

Expand Down Expand Up @@ -107,8 +106,7 @@ private ServiceWeeklyAvailability constructServiceWeeklyAvailability(ServiceWeek
availability = new ServiceWeeklyAvailability();
availability.setDayOfWeek(avb.getDayOfWeek());
availability.setStartTime(avb.getStartTime());
availability.setStartTime(utcTimeToServerTime(avb.getStartTime()));
availability.setEndTime(utcTimeToServerTime(avb.getEndTime()));
availability.setEndTime(avb.getEndTime());
availability.setMaxAppointmentsLimit(avb.getMaxAppointmentsLimit());
availability.setService(appointmentServiceDefinition);
availability.setVoided(avb.isVoided());
Expand Down Expand Up @@ -206,30 +204,6 @@ private Map constructAvailabilityResponse(ServiceWeeklyAvailability availability
}

private String convertTimeToString(Time time) {
if (time == null) {
return new String();
}

// Use today's date for the returned time so that hour is adjusted considering daylight saving time
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, time.getHours());
calendar.set(Calendar.MINUTE, time.getMinutes());
calendar.set(Calendar.SECOND, time.getSeconds());
calendar.set(Calendar.MILLISECOND, 0);

return new SimpleDateFormat("HH:mm").format(calendar.getTime());
}

private Time utcTimeToServerTime(Time time) {
if (time == null) {
return null;
}

Calendar serviceEndTimeUtc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
serviceEndTimeUtc.set(Calendar.HOUR_OF_DAY, time.getHours());
serviceEndTimeUtc.set(Calendar.MINUTE, time.getMinutes());
serviceEndTimeUtc.set(Calendar.SECOND, time.getSeconds());
serviceEndTimeUtc.set(Calendar.MILLISECOND, 0);
return new Time(serviceEndTimeUtc.getTime().getHours(), serviceEndTimeUtc.getTime().getMinutes(), serviceEndTimeUtc.getTime().getSeconds());
return time != null ? time.toString() : new String();
}
}
Loading

0 comments on commit b50dd26

Please sign in to comment.