Skip to content

Commit

Permalink
[~] fix date conversion which added/subtracted time offset without co…
Browse files Browse the repository at this point in the history
…nsideration for time zone
  • Loading branch information
Red1tum committed Jul 23, 2024
1 parent 9285334 commit cb2f481
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package office.effective.features.booking.converters

import com.google.api.client.util.DateTime
import office.effective.common.constants.BookingConstants

/**
* Converts local time to Google [DateTime] in GMT.
* Converts local time to [DateTime].
*
* Use it for all requests to Google Calendar.
*/
fun Long.toGoogleDateTime(): DateTime {
return DateTime(this - BookingConstants.DEFAULT_TIMEZONE_OFFSET_MILLIS)
fun Long.toDateTime(): DateTime {
return DateTime(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import office.effective.features.workspace.repository.WorkspaceRepository
import office.effective.model.RecurrenceModel.Companion.toRecurrence
import org.slf4j.LoggerFactory
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.List as List

Expand Down Expand Up @@ -349,9 +351,12 @@ class GoogleCalendarConverter(
* @return [Instant]
*/
private fun toLocalInstant(googleDateTime: EventDateTime?) : Instant {
val gmtEpoch: Long? = googleDateTime?.dateTime?.value
val localEpoch = gmtEpoch?.let { it + BookingConstants.DEFAULT_TIMEZONE_OFFSET_MILLIS } ?: 0
return Instant.ofEpochMilli(localEpoch)
if (googleDateTime == null) return Instant.ofEpochMilli(0)

val instant = Instant.ofEpochMilli(googleDateTime.dateTime.value)
val correctedTime = ZonedDateTime.ofInstant(instant, ZoneId.of(googleDateTime.timeZone))
.withZoneSameInstant(ZoneId.of(BookingConstants.DEFAULT_TIMEZONE_ID))
return correctedTime.toInstant()
}

/**
Expand All @@ -361,7 +366,7 @@ class GoogleCalendarConverter(
*/
private fun Instant.toGoogleEventDateTime() : EventDateTime {
val googleEventDateTime = EventDateTime()
googleEventDateTime.dateTime = this.toEpochMilli().toGoogleDateTime()
googleEventDateTime.dateTime = this.toEpochMilli().toDateTime()
googleEventDateTime.timeZone = BookingConstants.DEFAULT_TIMEZONE_ID
return googleEventDateTime
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import office.effective.common.exception.InstanceNotFoundException
import office.effective.common.exception.MissingIdException
import office.effective.common.exception.WorkspaceUnavailableException
import office.effective.features.booking.converters.GoogleCalendarConverter
import office.effective.features.booking.converters.toGoogleDateTime
import office.effective.features.booking.converters.toDateTime
import office.effective.features.calendar.repository.CalendarIdsRepository
import office.effective.features.user.repository.UserEntity
import office.effective.features.user.repository.UserRepository
Expand Down Expand Up @@ -137,7 +137,7 @@ class BookingMeetingRepository(
/**
* Request template containing all required parameters
*
* @param timeMin lover bound for filtering bookings by start time.
* @param timeMin lower bound for filtering bookings by start time.
* Old Google calendar events may not appear correctly in the system and cause unexpected exceptions.
* Should be a time in the default timezone.
* @param timeMax upper bound (exclusive) for an event's start time to filter by.
Expand All @@ -154,8 +154,8 @@ class BookingMeetingRepository(
): Calendar.Events.List {
return calendarEvents.list(calendarId)
.setSingleEvents(singleEvents)
.setTimeMin(timeMin.toGoogleDateTime())
.setTimeMax(timeMax?.toGoogleDateTime())
.setTimeMin(timeMin.toDateTime())
.setTimeMax(timeMax?.toDateTime())
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package office.effective.features.booking.repository

import com.google.api.client.googleapis.json.GoogleJsonResponseException
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.Calendar
import com.google.api.services.calendar.model.Event
import office.effective.common.constants.BookingConstants
import office.effective.common.exception.InstanceNotFoundException
import office.effective.common.exception.MissingIdException
import office.effective.common.exception.WorkspaceUnavailableException
import office.effective.features.booking.converters.GoogleCalendarConverter
import office.effective.features.booking.converters.toGoogleDateTime
import office.effective.features.booking.converters.toDateTime
import office.effective.model.Booking
import org.slf4j.LoggerFactory
import java.time.Instant
Expand Down Expand Up @@ -105,8 +104,8 @@ class BookingRegularRepository(
): Calendar.Events.List {
return calendarEvents.list(calendarId)
.setSingleEvents(singleEvents)
.setTimeMin(timeMin.toGoogleDateTime())
.setTimeMax(timeMax?.toGoogleDateTime())
.setTimeMin(timeMin.toDateTime())
.setTimeMax(timeMax?.toDateTime())
}

/**
Expand Down

0 comments on commit cb2f481

Please sign in to comment.